'use client'; import { useState, useEffect } from 'react'; import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from '@/components/ui/alert-dialog'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { toast } from 'sonner'; interface Section { id: string; name: string; projectId: string; kind: 'section' | 'milestone'; status: 'planned' | 'in_progress' | 'complete'; targetDate: string | null; sortOrder: number; } interface SectionDialogProps { open: boolean; onOpenChange: (open: boolean) => void; projectId: string; domainId: string; onCreated: () => void; existingSection?: Section; } export function SectionDialog({ open, onOpenChange, projectId, domainId, onCreated, existingSection, }: SectionDialogProps) { const isEdit = !!existingSection; const [name, setName] = useState(''); const [kind, setKind] = useState<'section' | 'milestone'>('section'); const [status, setStatus] = useState<'planned' | 'in_progress' | 'complete'>('planned'); const [targetDate, setTargetDate] = useState(''); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(''); const [deleteOpen, setDeleteOpen] = useState(false); const [deleting, setDeleting] = useState(false); useEffect(() => { if (open) { if (existingSection) { setName(existingSection.name); setKind(existingSection.kind); setStatus(existingSection.status); setTargetDate(existingSection.targetDate ? existingSection.targetDate.split('T')[0] : ''); } else { setName(''); setKind('section'); setStatus('planned'); setTargetDate(''); } setError(''); } }, [open, existingSection]); async function handleSubmit(event: React.FormEvent) { event.preventDefault(); if (!name) { setError('Name is required'); return; } setSubmitting(true); setError(''); const body: Record = { name, kind, status }; if (targetDate) body.targetDate = new Date(targetDate).toISOString(); try { let response: Response; if (isEdit && existingSection) { response = await fetch(`/api/domains/${domainId}/projects/${projectId}/sections/${existingSection.id}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }); } else { response = await fetch(`/api/domains/${domainId}/projects/${projectId}/sections`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }); } if (!response.ok) { const err = await response.json(); throw new Error(err.error?.message || `Unable to ${isEdit ? 'update' : 'create'} section`); } toast.success(isEdit ? 'Section updated' : 'Section created'); onOpenChange(false); onCreated(); } catch (err) { setError(err instanceof Error ? err.message : `Unable to ${isEdit ? 'update' : 'create'} section`); } finally { setSubmitting(false); } } async function handleDelete() { if (!existingSection) return; setDeleting(true); try { const response = await fetch(`/api/domains/${domainId}/projects/${projectId}/sections/${existingSection.id}`, { method: 'DELETE', }); if (!response.ok) { const err = await response.json(); throw new Error(err.error?.message || 'Unable to delete section'); } toast.success('Section deleted'); setDeleteOpen(false); onOpenChange(false); onCreated(); } catch (err) { toast.error(err instanceof Error ? err.message : 'Unable to delete section'); } finally { setDeleting(false); } } return ( <> {isEdit ? 'Edit Section' : 'New Section'} {isEdit ? 'Update this section or milestone.' : 'Add a section or milestone to organize tasks.'}
setName(e.target.value)} placeholder="e.g. Backend, Design, Launch" autoFocus required />
setTargetDate(e.target.value)} />
{error &&

{error}

} {isEdit && ( )}
Delete Section Are you sure you want to delete "{existingSection?.name}"? This action cannot be undone. Cancel {deleting ? 'Deleting...' : 'Delete'} ); }