'use client' import React, { useState } from 'react' import { Card, CardContent } from './ui/card' import { Tabs, TabsContent, TabsList, TabsTrigger } from './ui/tabs' import EventTaskCalendar from './events/EventTaskCalendar' interface Todo { id: string name: string complete: boolean dueDate?: string | null notes?: string | null eventId: string createdAt: string updatedAt: string } interface Props { eventId: string initialTodos: Todo[] onUpdate: () => void } export default function ToDoList({ eventId, initialTodos, onUpdate }: Props) { const [todos, setTodos] = useState(initialTodos) const [newName, setNewName] = useState('') const [newDueDate, setNewDueDate] = useState('') const [editingNoteId, setEditingNoteId] = useState(null) const [noteDraft, setNoteDraft] = useState('') async function handleAdd() { if (!newName.trim()) return const res = await fetch(`/api/events/${eventId}/todo`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: newName, dueDate: newDueDate || null }), }) const data = await res.json() if (res.ok) { setTodos(prev => [...prev, data]) setNewName('') setNewDueDate('') } if (onUpdate) await onUpdate() } async function toggleComplete(id: string, complete: boolean) { const res = await fetch(`/api/events/${eventId}/todo/${id}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ complete }), }) if (res.ok) { setTodos(prev => prev.map(todo => todo.id === id ? { ...todo, complete } : todo ) ) } if (onUpdate) await onUpdate() } async function handleDelete(id: string) { const res = await fetch(`/api/events/${eventId}/todo/${id}`, { method: 'DELETE', }) if (res.ok) { setTodos(prev => prev.filter(todo => todo.id !== id)) } if (onUpdate) await onUpdate() } async function saveNote(id: string) { const res = await fetch(`/api/events/${eventId}/todo/${id}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ notes: noteDraft }), }) if (res.ok) { setTodos(prev => prev.map(todo => todo.id === id ? { ...todo, notes: noteDraft } : todo ) ) setEditingNoteId(null) setNoteDraft('') } if (onUpdate) await onUpdate() } return (

To-Do List

setNewName(e.target.value)} /> setNewDueDate(e.target.value)} />
List View Calendar View
{todos.map(todo => (
toggleComplete(todo.id, e.target.checked)} /> {todo.name} {todo.dueDate && ( (Due {new Date(todo.dueDate).toLocaleDateString()}) )}
{/* Notes Section */}
{editingNoteId === todo.id ? (