import { useState } from 'react'; import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import * as z from 'zod'; import { entitySchemas } from '@/lib/entityValidationSchemas'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Textarea } from '@/components/ui/textarea'; import { Label } from '@/components/ui/label'; import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { SlugField } from '@/components/ui/slug-field'; import { Ruler, Save, X } from 'lucide-react'; import { Combobox } from '@/components/ui/combobox'; import { useCompanyHeadquarters } from '@/hooks/useAutocompleteData'; import { useUserRole } from '@/hooks/useUserRole'; import { EntityMultiImageUploader, ImageAssignments } from '@/components/upload/EntityMultiImageUploader'; import { FlexibleDateInput, type DatePrecision } from '@/components/ui/flexible-date-input'; import { submitDesignerCreation, submitDesignerUpdate } from '@/lib/entitySubmissionHelpers'; import { useAuth } from '@/hooks/useAuth'; import { toast } from 'sonner'; import { useNavigate } from 'react-router-dom'; type DesignerFormData = z.infer; // Input type for the form (before transformation) type DesignerFormInput = { name: string; slug: string; description?: string; person_type: 'company' | 'individual' | 'firm' | 'organization'; website_url?: string; founded_year?: string; headquarters_location?: string; images?: { uploaded: Array<{ url: string; cloudflare_id?: string; file?: any; isLocal?: boolean; caption?: string; }>; banner_assignment?: number | null; card_assignment?: number | null; }; }; interface DesignerFormProps { onSubmit: (data: DesignerFormData) => void; onCancel: () => void; initialData?: Partial; } export function DesignerForm({ onSubmit, onCancel, initialData }: DesignerFormProps) { const { isModerator } = useUserRole(); const { headquarters } = useCompanyHeadquarters(); const { user } = useAuth(); const navigate = useNavigate(); const [isSubmitting, setIsSubmitting] = useState(false); const { register, handleSubmit, setValue, watch, formState: { errors } } = useForm({ resolver: zodResolver(entitySchemas.designer), defaultValues: { name: initialData?.name || '', slug: initialData?.slug || '', description: initialData?.description || '', person_type: initialData?.person_type || 'company', website_url: initialData?.website_url || '', founded_year: initialData?.founded_year ? String(initialData.founded_year) : '', headquarters_location: initialData?.headquarters_location || '', images: initialData?.images || { uploaded: [] } } }); return ( {initialData ? 'Edit Designer' : 'Create New Designer'}
{ if (!user) { toast.error('You must be logged in to submit'); return; } setIsSubmitting(true); try { if (initialData?.id) { await submitDesignerUpdate(initialData.id, data as unknown as DesignerFormData, user.id); toast.success('Designer update submitted for review'); } else { await submitDesignerCreation(data as unknown as DesignerFormData, user.id); toast.success('Designer submitted for review'); } onCancel(); } catch (error) { console.error('Submission error:', error); toast.error(error instanceof Error ? error.message : 'Failed to submit designer'); } finally { setIsSubmitting(false); } })} className="space-y-6"> {/* Basic Information */}
{errors.name && (

{errors.name.message}

)}
setValue('slug', slug)} isModerator={isModerator()} />
{/* Description */}