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 { getErrorMessage } from '@/lib/errorHandler'; 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 { Badge } from '@/components/ui/badge'; import { SlugField } from '@/components/ui/slug-field'; import { Ruler, Save, X } from 'lucide-react'; import { useUserRole } from '@/hooks/useUserRole'; import { HeadquartersLocationInput } from './HeadquartersLocationInput'; 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 { handleError } from '@/lib/errorHandler'; 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; source_url?: string; submission_notes?: 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 { user } = useAuth(); const navigate = useNavigate(); 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 || '', source_url: initialData?.source_url || '', submission_notes: initialData?.submission_notes || '', images: initialData?.images || { uploaded: [] } } }); return ( {initialData ? 'Edit Designer' : 'Create New Designer'}
{ if (!user) { toast.error('You must be logged in to submit'); return; } 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: unknown) { handleError(error, { action: initialData?.id ? 'Update Designer' : 'Create Designer', metadata: { companyName: data.name } }); } })} className="space-y-6"> {/* Basic Information */}
{errors.name && (

{errors.name.message}

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