'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 { 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 SectionDialogProps { open: boolean; onOpenChange: (open: boolean) => void; projectId: string; domainId: string; onCreated: () => void; } export function SectionDialog({ open, onOpenChange, projectId, domainId, onCreated, }: SectionDialogProps) { 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(''); useEffect(() => { if (open) { setName(''); setKind('section'); setStatus('planned'); setTargetDate(''); setError(''); } }, [open]); 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 { const 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 create section'); } toast.success('Section created'); onOpenChange(false); onCreated(); } catch (err) { setError(err instanceof Error ? err.message : 'Unable to create section'); } finally { setSubmitting(false); } } return ( New Section 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}

}
); }