2020-01-21 21:34:59 +03:00
|
|
|
import React from 'react'
|
|
|
|
|
2020-01-23 18:31:00 +03:00
|
|
|
import Task from '@wasp/entities/task/Task'
|
2020-01-21 21:34:59 +03:00
|
|
|
import NewTaskForm from '@wasp/entities/task/components/NewTaskForm'
|
|
|
|
import TaskList from '@wasp/entities/task/components/List'
|
|
|
|
|
|
|
|
import * as config from './config'
|
|
|
|
|
|
|
|
|
|
|
|
export default class Todo extends React.Component {
|
2020-01-23 18:31:00 +03:00
|
|
|
toggleIsDoneForAllTasks = () => {
|
|
|
|
const areAllDone = this.props.taskList.every(t => t.isDone)
|
|
|
|
{/* TODO: This feels clumsy / complicated. Is there a better way than using id (maybe not)?
|
|
|
|
Should we consider passing just data to update, not the whole object, so we don't have to
|
|
|
|
create new object here? Maybe we can change this update, or have a second update method. */}
|
|
|
|
this.props.taskList.map(
|
|
|
|
(t) => this.props.updateTask(t.id, new Task ({ ...t.toData(), isDone: !areAllDone }))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteCompletedTasks = () => {
|
|
|
|
this.props.taskList.map((t) => { if (t.isDone) this.props.removeTask(t.id) })
|
|
|
|
}
|
|
|
|
|
2020-01-21 21:34:59 +03:00
|
|
|
render = () => {
|
2020-01-23 18:31:00 +03:00
|
|
|
return (
|
|
|
|
<div className="mainContainer">
|
|
|
|
<h1> { config.appName } </h1>
|
2020-01-21 21:34:59 +03:00
|
|
|
|
2020-01-23 18:31:00 +03:00
|
|
|
<button onClick={this.toggleIsDoneForAllTasks}>
|
|
|
|
Toggle completion {/* TODO: Use icon (but we need to either install @material-ui/icons or add font awesome to the index.html. */}
|
|
|
|
</button>
|
2020-01-21 21:34:59 +03:00
|
|
|
|
2020-01-23 18:31:00 +03:00
|
|
|
<NewTaskForm
|
|
|
|
onCreate={task => this.props.addTask(task)}
|
|
|
|
submitButtonLabel={'Create new task'}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<div className="taskListContainer">
|
|
|
|
<TaskList editable />
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="footer">
|
|
|
|
{ this.props.taskList.filter(task => !task.isDone).length } items left
|
2020-01-21 21:34:59 +03:00
|
|
|
|
2020-01-23 18:31:00 +03:00
|
|
|
{ this.props.taskList.some(t => t.isDone) &&
|
|
|
|
<button onClick={this.deleteCompletedTasks}>Clear completed</button>
|
|
|
|
}
|
|
|
|
</div>
|
2020-01-21 21:34:59 +03:00
|
|
|
</div>
|
2020-01-23 18:31:00 +03:00
|
|
|
)
|
2020-01-21 21:34:59 +03:00
|
|
|
}
|
|
|
|
}
|