Skip to content

Instantly share code, notes, and snippets.

@siljanoskam
Last active December 3, 2019 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siljanoskam/ecb0941d2988f81543d975f9d1882cc9 to your computer and use it in GitHub Desktop.
Save siljanoskam/ecb0941d2988f81543d975f9d1882cc9 to your computer and use it in GitHub Desktop.
Task List
import React from 'react';
import '../TaskList.css';
class TaskList extends React.Component {
constructor(props) {
super(props);
this.displayTask = this.displayTask.bind(this);
this.finishTask = this.finishTask.bind(this);
}
displayTask(task) {
return <li
className={`font-weight-bold ${task.completed ? 'strikethrough' : ''}`}
key={`${task.id}`}
onClick={this.finishTask}
title="Click to finish this task"
id={task.id}
>
{task.title}
</li>
};
updateTaskStatus(selectedTask) {
return fetch(`https://jsonplaceholder.typicode.com/todos/${selectedTask.id}`, {
method: 'PUT',
body: JSON.stringify({
completed: true
}),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
.then(() => {
selectedTask.classList.add('strikethrough')
})
.catch(console.error);
};
finishTask(event) {
const selectedTask = event.currentTarget;
this.updateTaskStatus(selectedTask);
};
render() {
return <div className="card card-body bg-info mb-2">
<ul className="task-list text-white">
{
this.props.items.length !== 0 ?
this.props.items.map(this.displayTask) :
<label className="font-weight-bold">You have no tasks at the moment!</label>
}
</ul>
</div>
}
}
export default TaskList;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment