"use client"; import { useState, useCallback } from "react"; import { motion, AnimatePresence } from "framer-motion"; import { CheckCircle2, Circle, Plus, ListTodo } from "lucide-react"; import { createTask, toggleTask } from "@/lib/api"; import AddTaskModal from "../AddTaskModal"; interface Task { id: number; title: string; done: boolean; priority?: number; } interface Props { tasks: Task[]; onUpdate: () => void; } export default function TasksCard({ tasks, onUpdate }: Props) { const [modalOpen, setModalOpen] = useState(false); const [localTasks, setLocalTasks] = useState(tasks); const handleToggle = useCallback( async (task: Task) => { setLocalTasks((prev) => prev.map((t) => (t.id === task.id ? { ...t, done: !t.done } : t)) ); await toggleTask(task.id, !task.done); onUpdate(); }, [onUpdate] ); const handleAdd = useCallback( async (title: string) => { const newTask = { id: Date.now(), title, done: false }; setLocalTasks((prev) => [newTask, ...prev]); await createTask(title); onUpdate(); }, [onUpdate] ); const pending = localTasks.filter((t) => !t.done); const done = localTasks.filter((t) => t.done); return ( <> {/* Header */}
Задачи
{pending.length > 0 ? `${pending.length} из ${localTasks.length} осталось` : "Всё готово!"}
{/* Add button */} setModalOpen(true)} className="w-10 h-10 rounded-xl flex items-center justify-center" style={{ background: "linear-gradient(135deg, #8b5cf6, #6366f1)", boxShadow: "0 0 18px rgba(139,92,246,0.45)", }} whileTap={{ scale: 0.82 }} >
{/* Task list */}
{localTasks.length === 0 && (
🎉
Всё сделано на сегодня!
setModalOpen(true)} className="mt-2 px-5 py-2.5 rounded-xl text-sm font-semibold" style={{ background: "linear-gradient(135deg, rgba(139,92,246,0.25), rgba(99,102,241,0.2))", border: "1px solid rgba(139,92,246,0.35)", color: "#8b5cf6", }} whileTap={{ scale: 0.9 }} > + Добавить задачу
)} {localTasks.map((task) => ( handleToggle(task)} whileTap={{ scale: 0.97 }} > {task.done ? ( ) : ( )} {task.title} ))}
setModalOpen(false)} onAdd={handleAdd} /> ); }