feat: 更新待办事项操作并重置状态

- 更新待办事项创建、切换和删除操作,确保在成功后重新获取数据并重置输入状态。
This commit is contained in:
2026-01-18 01:44:39 +08:00
parent f2481c31c7
commit 6a2f2e1b00

View File

@@ -19,7 +19,15 @@ function Todo() {
const handleCreateTodo = (e: FormEvent<HTMLFormElement>) => { const handleCreateTodo = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault() e.preventDefault()
if (newTodoTitle.trim()) { if (newTodoTitle.trim()) {
createMutation.mutate(newTodoTitle.trim()) createMutation.mutate(
{ title: newTodoTitle.trim() },
{
onSuccess: () => {
setNewTodoTitle('')
listQuery.refetch()
},
},
)
} }
} }
@@ -28,13 +36,31 @@ function Todo() {
} }
const handleToggleTodo = (id: string, currentCompleted: boolean) => { const handleToggleTodo = (id: string, currentCompleted: boolean) => {
updateMutation.mutate({ id, completed: !currentCompleted }) updateMutation.mutate(
{
id,
data: { completed: !currentCompleted },
},
{
onSuccess: () => {
listQuery.refetch()
},
},
)
} }
const handleDeleteTodo = (id: string) => { const handleDeleteTodo = (id: string) => {
deleteMutation.mutate(id) deleteMutation.mutate(
{ id },
{
onSuccess: () => {
listQuery.refetch()
},
},
)
} }
const todos = listQuery.data
const completedCount = todos.filter((todo) => todo.completed).length const completedCount = todos.filter((todo) => todo.completed).length
const totalCount = todos.length const totalCount = todos.length
const progress = totalCount > 0 ? (completedCount / totalCount) * 100 : 0 const progress = totalCount > 0 ? (completedCount / totalCount) * 100 : 0