import React, { useState, useEffect } from 'react'; import { api } from '../services/api'; import { Todo } from '../types'; import { Plus, CheckCircle, Circle, Trash2, CalendarCheck } from 'lucide-react'; export const TodoList: React.FC = () => { const [todos, setTodos] = useState([]); const [inputValue, setInputValue] = useState(''); const [loading, setLoading] = useState(false); useEffect(() => { loadTodos(); }, []); const loadTodos = async () => { try { const list = await api.todos.getAll(); setTodos(list); } catch (e) { console.error(e); } }; const handleAdd = async (e?: React.FormEvent) => { e?.preventDefault(); if (!inputValue.trim()) return; setLoading(true); await api.todos.add(inputValue); setInputValue(''); setLoading(false); loadTodos(); }; const handleToggle = async (t: Todo) => { // Optimistic update const newStatus = !t.isCompleted; setTodos(todos.map(item => item._id === t._id ? { ...item, isCompleted: newStatus } : item)); await api.todos.update(t._id!, { isCompleted: newStatus }); }; const handleDelete = async (id: string) => { setTodos(todos.filter(t => t._id !== id)); await api.todos.delete(id); }; return (

个人备忘录

{todos.length === 0 ? (

暂无待办事项

记录今天的教学/学习任务吧!

) : (
    {todos.map(t => (
  • {t.content}
  • ))}
)}
setInputValue(e.target.value)} disabled={loading} />
); };