import {useState, useEffect, FormEvent} from 'react'; import {hc} from 'hono/client' import type {TodoApp} from "../api/TodoAPI.ts"; import type {Todo} from "../api/TodoService.ts"; import {withLoginRequired} from "./Auth"; const client = hc(`${window.location.origin}/api`) const createTodo = (todoText: string) => client.todos.$post({json: {todoText}}) .then(res => res.json()) .then(res => res.todos) const getTodos = () => client.todos.$get() .then(res => res.json()) .then(res => res.todos) const deleteTodo = (id: string) => client.todos[':id'].$delete({param: {id}}) .then(res => res.json()) .then(res => res.todos) const markComplete = (id: string) => client.todos[':id'].complete.$post({param: {id}}) .then(res => res.json()) .then(res => res.todos) const TodoEditor = withLoginRequired(() => { const [todos, setTodos] = useState([]); const [newTodoText, setNewTodoText] = useState(''); // Fetch todos on component mount useEffect(() => { getTodos().then(todos => setTodos(todos)); }, []); const onAddTodo = (evt: FormEvent) => { evt.preventDefault(); createTodo(newTodoText).then(todos => setTodos(todos)); setNewTodoText(''); }; const onCompleteTodo = (id: string) => { markComplete(id).then(todos => setTodos(todos)); }; const onDeleteTodo = (id: string) => { deleteTodo(id).then(todos => setTodos(todos)); }; return (
  • setNewTodoText(e.target.value)} />
  • {todos.map((todo) => (
  • {todo.completed ? <>✔️ {todo.text} : todo.text}
    {!todo.completed && }
  • ))}
); }); export default TodoEditor;