import React, { useState } from 'react' import { Link } from "react-router-dom" import { useQuery } from '@wasp/queries' import getTasks from '@wasp/queries/getTasks.js' import createTask from '@wasp/actions/createTask.js' import updateTaskIsDone from '@wasp/actions/updateTaskIsDone.js' import deleteCompletedTasks from '@wasp/actions/deleteCompletedTasks.js' import toggleAllTasks from '@wasp/actions/toggleAllTasks.js' const Todo = (props) => { const defaultNewTaskDescription = '' const [newTaskDescription, setNewTaskDescription] = useState(defaultNewTaskDescription) const { data: tasks, isError, error: tasksError } = useQuery(getTasks) const isAnyTaskCompleted = () => tasks?.some(t => t.isDone) const isThereAnyTask = () => tasks?.length > 0 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
{ props.tasks.map((task, idx) => ) }
} const TasksError = (props) => { return 'Error during fetching tasks: ' + (tasksError?.message || '') } 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) } } const Task = (props) => { return
{ props.task.description }
} 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) } } return (

Todos

setNewTaskDescription(e.target.value)} />
{ isError && } { isThereAnyTask() && (<>
{ tasks.filter(t => !t.isDone).length } items left
)}
) } export default Todo