'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 { Textarea } from '@/components/ui/textarea'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { toast } from 'sonner'; interface TaskCreateDialogProps { open: boolean; onOpenChange: (open: boolean) => void; domainId: string; defaultStatus?: 'todo' | 'in_progress' | 'done' | 'cancelled'; onCreated: () => void; } export function TaskCreateDialog({ open, onOpenChange, domainId, defaultStatus = 'todo', onCreated, }: TaskCreateDialogProps) { const [title, setTitle] = useState(''); const [description, setDescription] = useState(''); const [status, setStatus] = useState(defaultStatus); const [priority, setPriority] = useState<'low' | 'medium' | 'high' | 'urgent'>('medium'); const [dueDate, setDueDate] = useState(''); const [estimatedMinutes, setEstimatedMinutes] = useState(''); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(''); // Reset form when dialog opens useEffect(() => { if (open) { setTitle(''); setDescription(''); setStatus(defaultStatus); setPriority('medium'); setDueDate(''); setEstimatedMinutes(''); setError(''); } }, [open, defaultStatus]); async function handleSubmit(event: React.FormEvent) { event.preventDefault(); if (!domainId) { setError('No domain selected'); return; } setSubmitting(true); setError(''); const body: Record = { title, status, priority, }; if (description) body.description = description; if (dueDate) body.dueDate = new Date(dueDate).toISOString(); if (estimatedMinutes) body.estimatedMinutes = parseInt(estimatedMinutes, 10); try { const response = await fetch(`/api/domains/${domainId}/tasks`, { 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 task'); } toast.success('Task created'); onOpenChange(false); onCreated(); } catch (err) { setError(err instanceof Error ? err.message : 'Unable to create task'); } finally { setSubmitting(false); } } return ( New Task Create a new task to track your work.
setTitle(e.target.value)} placeholder="What needs to be done?" autoFocus required />