"use client"; import { useState, useEffect } from "react"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { AddTaskDialog } from "@/components/add-task-dialog"; import { TaskDetailDialog } from "@/components/task-detail-dialog"; import { useToast } from "@/components/ui/use-toast"; import { Plus, CheckCircle2, Clock, AlertCircle, Trash2 } from "lucide-react"; interface Task { id: number; title: string; description: string; status: string; priority: string; createdAt: string; updatedAt: string; } const statusConfig: Record = { pending: { label: "Pending", icon: , variant: "secondary" }, in_progress: { label: "In Progress", icon: , variant: "default" }, completed: { label: "Completed", icon: , variant: "outline" }, }; const priorityColors: Record = { low: "bg-green-500/20 text-green-400 border-green-500/30", medium: "bg-yellow-500/20 text-yellow-400 border-yellow-500/30", high: "bg-red-500/20 text-red-400 border-red-500/30", }; export default function Dashboard() { const [tasks, setTasks] = useState([]); const [loading, setLoading] = useState(true); const [addDialogOpen, setAddDialogOpen] = useState(false); const [selectedTask, setSelectedTask] = useState(null); const { toast } = useToast(); const fetchTasks = async () => { try { const res = await fetch("/api/tasks"); const data = await res.json(); if (data.success) { setTasks(data.data || []); } } catch (error) { toast({ title: "Error", description: "Failed to fetch tasks", variant: "destructive", }); } finally { setLoading(false); } }; useEffect(() => { fetchTasks(); }, []); const handleAddTask = async (task: { title: string; description: string; priority: string }) => { try { const res = await fetch("/api/tasks", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(task), }); const data = await res.json(); if (data.success) { toast({ title: "Success", description: "Task created successfully", }); fetchTasks(); setAddDialogOpen(false); } else { throw new Error(data.error); } } catch (error) { toast({ title: "Error", description: "Failed to create task", variant: "destructive", }); } }; const handleDeleteTask = async (id: number, e: React.MouseEvent) => { e.stopPropagation(); try { const res = await fetch(`/api/tasks/${id}`, { method: "DELETE" }); const data = await res.json(); if (data.success) { toast({ title: "Success", description: "Task deleted successfully", }); fetchTasks(); } else { throw new Error(data.error); } } catch (error) { toast({ title: "Error", description: "Failed to delete task", variant: "destructive", }); } }; const stats = { total: tasks.length, pending: tasks.filter((t) => t.status === "pending").length, inProgress: tasks.filter((t) => t.status === "in_progress").length, completed: tasks.filter((t) => t.status === "completed").length, }; return (

Task Manager

Manage and track your tasks efficiently

{/* Stats */}
Total Tasks {stats.total} Pending {stats.pending} In Progress {stats.inProgress} Completed {stats.completed}
{/* Task List */} All Tasks Click on a task to view details {loading ? (
Loading tasks...
) : tasks.length === 0 ? (
No tasks yet. Create your first task to get started.
) : (
{tasks.map((task) => { const status = statusConfig[task.status] || statusConfig.pending; return (
setSelectedTask(task)} className="flex items-center justify-between p-4 rounded-lg border bg-card hover:bg-accent/50 cursor-pointer transition-colors" >
{task.title} {task.description || "No description"}
{task.priority} {status.icon} {status.label}
); })}
)}
!open && setSelectedTask(null)} />
); }