'use client' import { useState, useEffect } from 'react' import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, } from '@/components/ui/dialog' import { Button } from '@/components/ui/button' 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 { Deadline, DeadlineType, DeadlinePriority } from '@/types' import { formatDateISO, DEADLINE_TYPE_LABELS, PRIORITY_LABELS } from '@/lib/calendar/utils' import { useCanWrite } from '@/lib/hooks/use-can-write' import { Lock } from 'lucide-react' /** * Only the fields the form actually manages. Both API routes whitelist to * this set; the form must never fabricate values for system fields (source, * status, reminder_offsets, tax_*), or editing a system-generated tax * deadline would depend on the server whitelist alone to avoid data loss. */ export type DeadlineFormValues = Pick< Deadline, 'title' | 'due_date' | 'due_time' | 'deadline_type' | 'priority' | 'customer_id' | 'notes' > interface DeadlineFormProps { open: boolean onOpenChange: (open: boolean) => void onSubmit: (data: DeadlineFormValues) => Promise onDelete?: (deadline: Partial) => void initialData?: Partial initialDate?: Date | null customers: { id: string; name: string }[] } export function DeadlineForm({ open, onOpenChange, onSubmit, onDelete, initialData, initialDate, customers, }: DeadlineFormProps) { const { canWrite } = useCanWrite() const [confirmDelete, setConfirmDelete] = useState(false) const [isLoading, setIsLoading] = useState(false) const [formData, setFormData] = useState({ title: '', due_date: '', due_time: '', deadline_type: 'other' as DeadlineType, priority: 'normal' as DeadlinePriority, customer_id: '', notes: '', }) // Reset form when dialog opens with new data useEffect(() => { if (open) { setConfirmDelete(false) if (initialData) { setFormData({ title: initialData.title || '', due_date: initialData.due_date || formatDateISO(new Date()), due_time: initialData.due_time || '', deadline_type: initialData.deadline_type || 'other', priority: initialData.priority || 'normal', customer_id: initialData.customer_id || '', notes: initialData.notes || '', }) } else if (initialDate) { setFormData({ title: '', due_date: formatDateISO(initialDate), due_time: '', deadline_type: 'other', priority: 'normal', customer_id: '', notes: '', }) } else { setFormData({ title: '', due_date: formatDateISO(new Date()), due_time: '', deadline_type: 'other', priority: 'normal', customer_id: '', notes: '', }) } } }, [open, initialData, initialDate]) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setIsLoading(true) try { await onSubmit({ title: formData.title, due_date: formData.due_date, due_time: formData.due_time || null, deadline_type: formData.deadline_type, priority: formData.priority, customer_id: formData.customer_id || null, notes: formData.notes || null, }) } finally { setIsLoading(false) } } const updateField = ( field: K, value: (typeof formData)[K] ) => { setFormData((prev) => ({ ...prev, [field]: value })) } return ( {initialData?.id ? 'Redigera deadline' : 'Ny deadline'}
{/* Title */}
updateField('title', e.target.value)} required />
{/* Date and Time */}
updateField('due_date', e.target.value)} required />
updateField('due_time', e.target.value)} />
{/* Type and Priority */}
{/* Customer */} {customers.length > 0 && (
)} {/* Notes */}