-1
import React, { Component } from 'react'
import TaskOneServices from '../services/TaskOneServices'
import circle from '../assets/icons/dry-clean.png'
import tick from '../assets/icons/check-mark.png'

async function showImage (taskId)  {

    var getStatus = (await TaskOneServices.getStatus(taskId))?.data;

    JSON.stringify(getStatus);

    console.log(typeof(getStatus)); //prints object

    console.log(getStatus === "CREATED"); //prints false

    if(getStatus === "CREATED") {
        return (
            <img className="img-lm" src={circle} alt="bullets" onClick={(ev) => {taskCompleted(ev, taskId)}}/>
        )
    } else {
        return (
            <img className="img-lm" src={tick} alt="checked" onClick={(ev) => {taskCompleted(ev, taskId)}}/>
        )
    } 
    
}

function taskCompleted(e, taskid) {
    if(e.target.getAttribute('src') === circle) {
        e.target.setAttribute( 'src', tick);
        e.target.setAttribute( 'alt', 'checked');
        updateTask(taskid);
        // strikeThroughFn(taskid);
    } else {
        e.target.setAttribute( 'src', circle);
        e.target.setAttribute( 'alt', 'bullets');
        updateTask(taskid);
    }
    }

    function updateTask(id) {

        JSON.stringify(id);
        TaskOneServices.updateTask(id).then((response) => {
            console.log(response)
        });
    }

export default class TaskListComponent extends Component {

    constructor(props) {
        super(props)
        this.state = {
            task_one:[],
        }

        this.deleteTask = this.deleteTask.bind(this)
        // this.updateTask = this.updateTask.bind(this)
    }
    //------- showing error -------------
    componentDidMount() {
        TaskOneServices.getTasks().then((response) => {
            this.setState({task_one: response.data})
        });
    }
    //------- showing error -------------

    deleteTask = (id) => {
        
        JSON.stringify(id);
        TaskOneServices.deleteTask(id).then((response) => {
            this.refreshPage();
        });
    }
    
    refreshPage = () => {
        window.location.reload();
    }

    strikeThroughFn(taskid) {
        // JSON.stringify(taskString);
        console.log(taskid);
        var fetchTask = document.getElementById('strikeTask');
        fetchTask.innerHTML = <strike> + {taskid} + </strike>
    }

    render() {
        return (
            <div className="container-fluid bg-white">
                <div className="row">
                    <table className="table border-bottom table-pad">
                        <tbody>
                        {
                            this.state.task_one.map(
                                task =>
                                <tr key = {task.id}>
                                    <td width="60px">
                                         {showImage(task.id)}
                                    </td>
                                    <td id="strikeTask">{task.task}</td>
                                    <td width="60px">
                                        <button type="button" className="close text-danger btn-close-style" onClick={() => this.deleteTask(task.id)}>
                                            <span aria-hidden="true">&times;</span>
                                        </button>
                                    </td>
                                </tr>
                            )
                        }
                        </tbody>
                    </table>
                </div>
            </div>
        )
    }
}

I want to compare the getStatus and string "CREATED" and if both are equal I want to return the inner block of code. But getStatus has a type of object and "CREATED" is a string. So it returns false. I tried JSON.stringify() and it still returns false. Is there any way that I could convert that object to string?

1 Answers1

2

You are using a promise so the .then() will be triggered after your function, you want to await the result of the promise and so use the await keyword with an async function. REMEMBEr that async function return also a promise

async function showImage (taskId)  {

    var getStatus = (await TaskOneServices.getStatus(taskId))?.data;

    JSON.stringify(getStatus);

    console.log(typeof(getStatus)); //prints object

    console.log(getStatus === "CREATED"); //prints false

    if(getStatus === "CREATED") {
        return (
            <img className="img-lm" src={circle} alt="bullets" onClick={(ev) => {taskCompleted(ev, taskId)}}/>
        )
    } else {
        return (
            <img className="img-lm" src={tick} alt="checked" onClick={(ev) => {taskCompleted(ev, taskId)}}/>
        )
    } 
    
}
Thibaud
  • 1,059
  • 4
  • 14
  • 27
  • 1
    If you're using `async/await` there's little point in using `then` too. – Andy Nov 08 '21 at 17:03
  • @Andy true ! I will update this post – Thibaud Nov 08 '21 at 17:04
  • @Andy This worked and now it has the type of string. But another part of the code is showing some errors. I will edit my quesetion. – Aneesh Edavalath S Nov 08 '21 at 17:17
  • @AneeshEdavalathS did you just copy Saren's answer as an edit to your question? – Andy Nov 08 '21 at 17:19
  • https://reactjs.org/docs/getting-started.html. You need to understand that you can't use traditional JS (like `querySelector`, for example) with React. React has its own process for updating the DOM. – Andy Nov 08 '21 at 17:21
  • @Andy Yeah! Could you please explain to me what that async and await does? I'm a beginner in ReactJS. – Aneesh Edavalath S Nov 08 '21 at 17:22
  • [`async/await`](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await) won't fix the problems with your code. Take another look at the "Getting started" link I posted. – Andy Nov 08 '21 at 17:32