import { api } from "@code/backend/convex/_generated/api"; import type { Doc, Id } from "@code/backend/convex/_generated/dataModel"; import { Ionicons } from "@expo/vector-icons"; import { useMutation, useQuery } from "convex/react"; import { Button, Checkbox, Chip, Spinner, Surface, Input, TextField, useThemeColor, } from "heroui-native"; import { useState } from "react"; import { View, Text, ScrollView, Alert } from "react-native"; import { Container } from "@/components/container"; export default function TodosScreen() { const [newTodoText, setNewTodoText] = useState(""); const todos = useQuery(api.todos.getAll); const createTodoMutation = useMutation(api.todos.create); const toggleTodoMutation = useMutation(api.todos.toggle); const deleteTodoMutation = useMutation(api.todos.deleteTodo); const mutedColor = useThemeColor("muted"); const dangerColor = useThemeColor("danger"); const foregroundColor = useThemeColor("foreground"); const handleAddTodo = async () => { const text = newTodoText.trim(); if (!text) return; await createTodoMutation({ text }); setNewTodoText(""); }; const handleToggleTodo = (id: Id<"todos">, currentCompleted: boolean) => { toggleTodoMutation({ id, completed: !currentCompleted }); }; const handleDeleteTodo = (id: Id<"todos">) => { Alert.alert("Delete Todo", "Are you sure you want to delete this todo?", [ { text: "Cancel", style: "cancel" }, { text: "Delete", style: "destructive", onPress: () => deleteTodoMutation({ id }), }, ]); }; const isLoading = !todos; const completedCount = todos?.filter((t: Doc<"todos">) => t.completed).length || 0; const totalCount = todos?.length || 0; return ( Tasks {totalCount > 0 && ( {completedCount}/{totalCount} )} {isLoading && ( Loading tasks... )} {todos && todos.length === 0 && !isLoading && ( No tasks yet Add your first task to get started )} {todos && todos.length > 0 && ( {todos.map((todo: Doc<"todos">) => ( handleToggleTodo(todo._id, todo.completed)} /> {todo.text} ))} )} ); }