import { useState, useEffect } from "react"; import { Pencil, Trash2, Check, X, Plus } from "lucide-react"; export default function TodoList() { const [todos, setTodos] = useState([]); const [newTodo, setNewTodo] = useState(""); const [editingId, setEditingId] = useState(null); const [editText, setEditText] = useState(""); useEffect(() => { fetchTodos(); }, []); const fetchTodos = async () => { try { const response = await fetch('http://localhost:3000/api/LUTdMX/todos'); const data = await response.json(); setTodos(data.document || []); } catch (error) { console.error('Error fetching todos:', error); } }; const addTodo = async (e) => { e.preventDefault(); if (!newTodo.trim()) return; const todo = { text: newTodo, notes: "", completed: false, }; try { await fetch('http://localhost:3000/api/LUTdMX/todos', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ document: todo }), }); fetchTodos(); setNewTodo(""); } catch (error) { console.error('Error adding todo:', error); } }; const toggleComplete = async (todo) => { try { await fetch(`http://localhost:3000/api/LUTdMX/todos`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ document: { _id: todo._id, ...todo, completed: !todo.completed } }), }); fetchTodos(); } catch (error) { console.error('Error updating todo:', error); } }; const deleteTodo = async (id) => { try { await fetch(`http://localhost:3000/api/LUTdMX/todos`, { method: 'DELETE', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ document: { _id: id } }), }); fetchTodos(); } catch (error) { console.error('Error deleting todo:', error); } }; const updateTodo = async (id) => { const todo = todos.find(t => t._id === id); if (!todo) return; try { await fetch(`http://localhost:3000/api/LUTdMX/todos`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ document: { ...todo, text: editText } }), }); setEditingId(null); fetchTodos(); } catch (error) { console.error('Error updating todo:', error); } }; const updateNotes = async (todo, notes) => { try { await fetch(`http://localhost:3000/api/LUTdMX/todos`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ document: { ...todo, notes } }), }); fetchTodos(); } catch (error) { console.error('Error updating notes:', error); } }; return (

Todo List

setNewTodo(e.target.value)} placeholder="Add a new todo..." className="flex-1 rounded-lg border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 p-2" />
{todos.map((todo) => (
{editingId === todo._id ? (
setEditText(e.target.value)} className="flex-1 rounded border-gray-300 p-1" />
) : (
{todo.text}
)}