diff --git a/src/components/admin/ManufacturerForm.tsx b/src/components/admin/ManufacturerForm.tsx index b413c3dd..a8127b51 100644 --- a/src/components/admin/ManufacturerForm.tsx +++ b/src/components/admin/ManufacturerForm.tsx @@ -19,7 +19,7 @@ import { FlexibleDateInput, type DatePrecision } from '@/components/ui/flexible- import { useAuth } from '@/hooks/useAuth'; import { toast } from 'sonner'; import { handleError } from '@/lib/errorHandler'; -import { toDateOnly, parseDateOnly } from '@/lib/dateUtils'; +import { toDateOnly, parseDateOnly, toDateWithPrecision } from '@/lib/dateUtils'; import type { UploadedImage } from '@/types/company'; // Zod output type (after transformation) @@ -56,7 +56,7 @@ export function ManufacturerForm({ onSubmit, onCancel, initialData }: Manufactur person_type: initialData?.person_type || ('company' as const), website_url: initialData?.website_url || '', founded_year: initialData?.founded_year ? String(initialData.founded_year) : '', - founded_date: initialData?.founded_date || (initialData?.founded_year ? `${initialData.founded_year}-01-01` : ''), + founded_date: initialData?.founded_date || (initialData?.founded_year ? `${initialData.founded_year}-01-01` : undefined), founded_date_precision: initialData?.founded_date_precision || (initialData?.founded_year ? ('year' as const) : ('day' as const)), headquarters_location: initialData?.headquarters_location || '', source_url: initialData?.source_url || '', @@ -178,11 +178,7 @@ export function ManufacturerForm({ onSubmit, onCancel, initialData }: Manufactur })()} precision={(watch('founded_date_precision') as DatePrecision) || 'year'} onChange={(date, precision) => { - if (date && typeof date === 'string') { - setValue('founded_date', toDateOnly(date), { shouldValidate: true }); - } else { - setValue('founded_date', '', { shouldValidate: true }); - } + setValue('founded_date', date ? toDateWithPrecision(date, precision) : undefined, { shouldValidate: true }); setValue('founded_date_precision', precision); }} label="Founded Date" diff --git a/src/components/admin/ParkForm.tsx b/src/components/admin/ParkForm.tsx index 1b194379..01a726c0 100644 --- a/src/components/admin/ParkForm.tsx +++ b/src/components/admin/ParkForm.tsx @@ -18,7 +18,7 @@ import { SlugField } from '@/components/ui/slug-field'; import { toast } from '@/hooks/use-toast'; import { handleError } from '@/lib/errorHandler'; import { MapPin, Save, X, Plus } from 'lucide-react'; -import { toDateOnly, parseDateOnly } from '@/lib/dateUtils'; +import { toDateOnly, parseDateOnly, toDateWithPrecision } from '@/lib/dateUtils'; import { Badge } from '@/components/ui/badge'; import { Combobox } from '@/components/ui/combobox'; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog'; @@ -37,9 +37,9 @@ const parkSchema = z.object({ description: z.string().optional(), park_type: z.string().min(1, 'Park type is required'), status: z.string().min(1, 'Status is required'), - opening_date: z.string().optional(), + opening_date: z.string().optional().transform(val => val || undefined), opening_date_precision: z.enum(['day', 'month', 'year']).optional(), - closing_date: z.string().optional(), + closing_date: z.string().optional().transform(val => val || undefined), closing_date_precision: z.enum(['day', 'month', 'year']).optional(), location: z.object({ name: z.string(), @@ -176,8 +176,8 @@ export function ParkForm({ onSubmit, onCancel, initialData, isEditing = false }: description: initialData?.description || '', park_type: initialData?.park_type || '', status: initialData?.status || 'operating' as const, // Store DB value - opening_date: initialData?.opening_date || '', - closing_date: initialData?.closing_date || '', + opening_date: initialData?.opening_date || undefined, + closing_date: initialData?.closing_date || undefined, location_id: initialData?.location_id || undefined, website_url: initialData?.website_url || '', phone: initialData?.phone || '', @@ -380,7 +380,7 @@ export function ParkForm({ onSubmit, onCancel, initialData, isEditing = false }: value={watch('opening_date') ? parseDateOnly(watch('opening_date')!) : undefined} precision={(watch('opening_date_precision') as DatePrecision) || 'day'} onChange={(date, precision) => { - setValue('opening_date', date ? toDateOnly(date) : undefined); + setValue('opening_date', date ? toDateWithPrecision(date, precision) : undefined); setValue('opening_date_precision', precision); }} label="Opening Date" @@ -393,7 +393,7 @@ export function ParkForm({ onSubmit, onCancel, initialData, isEditing = false }: value={watch('closing_date') ? parseDateOnly(watch('closing_date')!) : undefined} precision={(watch('closing_date_precision') as DatePrecision) || 'day'} onChange={(date, precision) => { - setValue('closing_date', date ? toDateOnly(date) : undefined); + setValue('closing_date', date ? toDateWithPrecision(date, precision) : undefined); setValue('closing_date_precision', precision); }} label="Closing Date (if applicable)" diff --git a/src/components/admin/RideForm.tsx b/src/components/admin/RideForm.tsx index 2720eafb..62d79f04 100644 --- a/src/components/admin/RideForm.tsx +++ b/src/components/admin/RideForm.tsx @@ -24,7 +24,7 @@ import { Checkbox } from '@/components/ui/checkbox'; import { toast } from '@/hooks/use-toast'; import { handleError } from '@/lib/errorHandler'; import { Plus, Zap, Save, X, Building2 } from 'lucide-react'; -import { toDateOnly, parseDateOnly } from '@/lib/dateUtils'; +import { toDateOnly, parseDateOnly, toDateWithPrecision } from '@/lib/dateUtils'; import { useUnitPreferences } from '@/hooks/useUnitPreferences'; import { useManufacturers, useRideModels } from '@/hooks/useAutocompleteData'; import { useUserRole } from '@/hooks/useUserRole'; @@ -224,9 +224,9 @@ export function RideForm({ onSubmit, onCancel, initialData, isEditing = false }: category: initialData?.category || '', ride_sub_type: initialData?.ride_sub_type || '', status: initialData?.status || 'operating' as const, // Store DB value directly - opening_date: initialData?.opening_date || '', + opening_date: initialData?.opening_date || undefined, opening_date_precision: initialData?.opening_date_precision || 'day', - closing_date: initialData?.closing_date || '', + closing_date: initialData?.closing_date || undefined, closing_date_precision: initialData?.closing_date_precision || 'day', // Convert metric values to user's preferred unit for display height_requirement: initialData?.height_requirement @@ -605,7 +605,7 @@ export function RideForm({ onSubmit, onCancel, initialData, isEditing = false }: value={watch('opening_date') ? parseDateOnly(watch('opening_date')!) : undefined} precision={(watch('opening_date_precision') as DatePrecision) || 'day'} onChange={(date, precision) => { - setValue('opening_date', date ? toDateOnly(date) : undefined); + setValue('opening_date', date ? toDateWithPrecision(date, precision) : undefined); setValue('opening_date_precision', precision); }} label="Opening Date" @@ -618,7 +618,7 @@ export function RideForm({ onSubmit, onCancel, initialData, isEditing = false }: value={watch('closing_date') ? parseDateOnly(watch('closing_date')!) : undefined} precision={(watch('closing_date_precision') as DatePrecision) || 'day'} onChange={(date, precision) => { - setValue('closing_date', date ? toDateOnly(date) : undefined); + setValue('closing_date', date ? toDateWithPrecision(date, precision) : undefined); setValue('closing_date_precision', precision); }} label="Closing Date (if applicable)" diff --git a/src/lib/entityTransformers.ts b/src/lib/entityTransformers.ts index 257c8843..fa4a72d5 100644 --- a/src/lib/entityTransformers.ts +++ b/src/lib/entityTransformers.ts @@ -23,8 +23,8 @@ export function transformParkData(submissionData: ParkSubmissionData): ParkInser description: submissionData.description || null, park_type: submissionData.park_type, status: normalizeStatus(submissionData.status), - opening_date: submissionData.opening_date || null, - closing_date: submissionData.closing_date || null, + opening_date: submissionData.opening_date?.trim() || null, + closing_date: submissionData.closing_date?.trim() || null, website_url: submissionData.website_url || null, phone: submissionData.phone || null, email: submissionData.email || null, @@ -62,8 +62,8 @@ export function transformRideData(submissionData: RideSubmissionData): RideInser ride_model_id: submissionData.ride_model_id || null, manufacturer_id: submissionData.manufacturer_id || null, designer_id: submissionData.designer_id || null, - opening_date: submissionData.opening_date || null, - closing_date: submissionData.closing_date || null, + opening_date: submissionData.opening_date?.trim() || null, + closing_date: submissionData.closing_date?.trim() || null, height_requirement: submissionData.height_requirement || null, age_requirement: submissionData.age_requirement || null, capacity_per_hour: submissionData.capacity_per_hour || null,