2020-08-31 15:41:49 +03:00
|
|
|
import React, { useState } from 'react'
|
2020-10-01 12:25:59 +03:00
|
|
|
import { Link } from "react-router-dom"
|
2020-08-31 15:41:49 +03:00
|
|
|
|
|
|
|
import { useQuery } from '@wasp/queries'
|
|
|
|
import getTasks from '@wasp/queries/getTasks.js'
|
2020-09-04 13:44:05 +03:00
|
|
|
import createTask from '@wasp/actions/createTask.js'
|
2020-09-29 11:44:27 +03:00
|
|
|
import updateTaskIsDone from '@wasp/actions/updateTaskIsDone.js'
|
2020-09-30 11:08:20 +03:00
|
|
|
import deleteCompletedTasks from '@wasp/actions/deleteCompletedTasks.js'
|
|
|
|
import toggleAllTasks from '@wasp/actions/toggleAllTasks.js'
|
2020-08-31 15:41:49 +03:00
|
|
|
|
|
|
|
const Todo = (props) => {
|
|
|
|
const defaultNewTaskDescription = ''
|
|
|
|
|
|
|
|
const [newTaskDescription, setNewTaskDescription] = useState(defaultNewTaskDescription)
|
|
|
|
|
2020-10-05 22:57:12 +03:00
|
|
|
const { data: tasks, isError, error: tasksError } = useQuery(getTasks)
|
2020-08-31 15:41:49 +03:00
|
|
|
|
|
|
|
const isAnyTaskCompleted = () => tasks?.some(t => t.isDone)
|
|
|
|
|
|
|
|
const isThereAnyTask = () => tasks?.length > 0
|
2020-02-05 13:47:58 +03:00
|
|
|
|
2020-10-19 15:45:54 +03:00
|
|
|
|
2020-08-31 15:41:49 +03:00
|
|
|
const createNewTask = async (description) => {
|
|
|
|
const task = { isDone: false, description }
|
|
|
|
await createTask(task)
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleNewTaskSubmit = async (event) => {
|
|
|
|
event.preventDefault()
|
|
|
|
try {
|
|
|
|
await createNewTask(newTaskDescription)
|
|
|
|
setNewTaskDescription(defaultNewTaskDescription)
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err)
|
|
|
|
window.alert('Error:' + err.message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Tasks = (props) => {
|
|
|
|
return <div>
|
|
|
|
{ props.tasks.map((task, idx) => <Task task={task} key={idx}/>) }
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
|
|
|
|
const TasksError = (props) => {
|
|
|
|
return 'Error during fetching tasks: ' + (tasksError?.message || '')
|
|
|
|
}
|
|
|
|
|
2020-09-29 11:44:27 +03:00
|
|
|
const handleTaskIsDoneChange = async (event) => {
|
|
|
|
const taskId = parseInt(event.target.id)
|
|
|
|
const newIsDoneVal = event.target.checked
|
|
|
|
|
|
|
|
try {
|
|
|
|
await updateTaskIsDone({ taskId, newIsDoneVal })
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err)
|
|
|
|
window.alert('Error:' + err.message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 15:41:49 +03:00
|
|
|
const Task = (props) => {
|
|
|
|
return <div>
|
2020-09-29 11:44:27 +03:00
|
|
|
<input
|
|
|
|
type="checkbox"
|
|
|
|
id={props.task.id}
|
|
|
|
checked={props.task.isDone}
|
|
|
|
onChange={handleTaskIsDoneChange}
|
|
|
|
/>
|
2020-10-01 12:25:59 +03:00
|
|
|
<Link to={`/task/${props.task.id}`}> { props.task.description } </Link>
|
2020-08-31 15:41:49 +03:00
|
|
|
</div>
|
|
|
|
}
|
|
|
|
|
2020-09-30 11:08:20 +03:00
|
|
|
const handleDeleteCompletedTasks = async () => {
|
|
|
|
try {
|
|
|
|
await deleteCompletedTasks()
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err)
|
|
|
|
window.alert('Error:' + err.message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleToggleAllTasks = async () => {
|
|
|
|
try {
|
|
|
|
await toggleAllTasks()
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err)
|
|
|
|
window.alert('Error:' + err.message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 15:41:49 +03:00
|
|
|
return (
|
|
|
|
<div className="todos">
|
|
|
|
<div className="todos__container">
|
|
|
|
<h1> Todos </h1>
|
|
|
|
|
|
|
|
<div className="todos__toggleAndInput">
|
|
|
|
<button
|
|
|
|
disabled={!isThereAnyTask()}
|
2020-09-30 11:08:20 +03:00
|
|
|
onClick={handleToggleAllTasks}
|
|
|
|
>
|
|
|
|
✓
|
|
|
|
</button>
|
2020-08-31 15:41:49 +03:00
|
|
|
|
|
|
|
<form onSubmit={handleNewTaskSubmit}>
|
|
|
|
<input type="text"
|
|
|
|
value={newTaskDescription}
|
|
|
|
onChange={e => setNewTaskDescription(e.target.value)}
|
2020-02-05 13:47:58 +03:00
|
|
|
/>
|
2020-08-31 15:41:49 +03:00
|
|
|
<input type="submit" value="Create new task"/>
|
|
|
|
</form>
|
|
|
|
</div>
|
2020-02-05 12:52:54 +03:00
|
|
|
|
2020-08-31 15:41:49 +03:00
|
|
|
{ isError && <TasksError/> }
|
2020-02-05 13:47:58 +03:00
|
|
|
|
2020-08-31 15:41:49 +03:00
|
|
|
{ isThereAnyTask() && (<>
|
|
|
|
<Tasks tasks={tasks}/>
|
|
|
|
|
|
|
|
<div className="todos__footer">
|
|
|
|
<div className="todos__footer__itemsLeft">
|
|
|
|
{ tasks.filter(t => !t.isDone).length } items left
|
2020-02-05 13:47:58 +03:00
|
|
|
</div>
|
2020-08-31 15:41:49 +03:00
|
|
|
|
|
|
|
<div className="todos__footer__clearCompleted">
|
2020-09-30 11:08:20 +03:00
|
|
|
<button
|
|
|
|
className={isAnyTaskCompleted() ? '' : 'hidden' }
|
|
|
|
onClick={handleDeleteCompletedTasks}
|
|
|
|
>
|
|
|
|
Delete completed
|
|
|
|
</button>
|
2020-08-31 15:41:49 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</>)}
|
2020-01-21 21:34:59 +03:00
|
|
|
</div>
|
2020-08-31 15:41:49 +03:00
|
|
|
</div>
|
|
|
|
)
|
2020-01-21 21:34:59 +03:00
|
|
|
}
|
2020-02-14 14:30:16 +03:00
|
|
|
|
2020-08-31 15:41:49 +03:00
|
|
|
export default Todo
|