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'; import type { UploadedImage } from '@/types/company'; // Raw form input state (before Zod transformation) interface DesignerFormInput { name: string; slug: string; company_type: 'designer' | 'manufacturer' | 'operator' | 'property_owner'; description?: string; person_type: 'company' | 'individual' | 'firm' | 'organization'; founded_year?: string; founded_date?: string; founded_date_precision?: 'day' | 'month' | 'year'; headquarters_location?: string; website_url?: string; images?: { uploaded: UploadedImage[]; banner_assignment?: number | null; card_assignment?: number | null; }; } // Zod output type (after transformation) type DesignerFormData = z.infer; 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 || '', company_type: 'designer' as const, description: initialData?.description || '', person_type: initialData?.person_type || ('company' as const), 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 { const formData = { ...data, company_type: 'designer' as const, founded_year: data.founded_year ? parseInt(String(data.founded_year)) : undefined, }; await onSubmit(formData); // Only show success toast and close if not editing through moderation queue if (!initialData?.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 */}