mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 11:51:14 -05:00
Fix date input normalization
This commit is contained in:
@@ -19,7 +19,7 @@ import { FlexibleDateInput, type DatePrecision } from '@/components/ui/flexible-
|
|||||||
import { useAuth } from '@/hooks/useAuth';
|
import { useAuth } from '@/hooks/useAuth';
|
||||||
import { toast } from 'sonner';
|
import { toast } from 'sonner';
|
||||||
import { handleError } from '@/lib/errorHandler';
|
import { handleError } from '@/lib/errorHandler';
|
||||||
import { toDateOnly, parseDateOnly } from '@/lib/dateUtils';
|
import { toDateOnly, parseDateOnly, toDateWithPrecision } from '@/lib/dateUtils';
|
||||||
import type { UploadedImage } from '@/types/company';
|
import type { UploadedImage } from '@/types/company';
|
||||||
|
|
||||||
// Zod output type (after transformation)
|
// Zod output type (after transformation)
|
||||||
@@ -56,7 +56,7 @@ export function ManufacturerForm({ onSubmit, onCancel, initialData }: Manufactur
|
|||||||
person_type: initialData?.person_type || ('company' as const),
|
person_type: initialData?.person_type || ('company' as const),
|
||||||
website_url: initialData?.website_url || '',
|
website_url: initialData?.website_url || '',
|
||||||
founded_year: initialData?.founded_year ? String(initialData.founded_year) : '',
|
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)),
|
founded_date_precision: initialData?.founded_date_precision || (initialData?.founded_year ? ('year' as const) : ('day' as const)),
|
||||||
headquarters_location: initialData?.headquarters_location || '',
|
headquarters_location: initialData?.headquarters_location || '',
|
||||||
source_url: initialData?.source_url || '',
|
source_url: initialData?.source_url || '',
|
||||||
@@ -178,11 +178,7 @@ export function ManufacturerForm({ onSubmit, onCancel, initialData }: Manufactur
|
|||||||
})()}
|
})()}
|
||||||
precision={(watch('founded_date_precision') as DatePrecision) || 'year'}
|
precision={(watch('founded_date_precision') as DatePrecision) || 'year'}
|
||||||
onChange={(date, precision) => {
|
onChange={(date, precision) => {
|
||||||
if (date && typeof date === 'string') {
|
setValue('founded_date', date ? toDateWithPrecision(date, precision) : undefined, { shouldValidate: true });
|
||||||
setValue('founded_date', toDateOnly(date), { shouldValidate: true });
|
|
||||||
} else {
|
|
||||||
setValue('founded_date', '', { shouldValidate: true });
|
|
||||||
}
|
|
||||||
setValue('founded_date_precision', precision);
|
setValue('founded_date_precision', precision);
|
||||||
}}
|
}}
|
||||||
label="Founded Date"
|
label="Founded Date"
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import { SlugField } from '@/components/ui/slug-field';
|
|||||||
import { toast } from '@/hooks/use-toast';
|
import { toast } from '@/hooks/use-toast';
|
||||||
import { handleError } from '@/lib/errorHandler';
|
import { handleError } from '@/lib/errorHandler';
|
||||||
import { MapPin, Save, X, Plus } from 'lucide-react';
|
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 { Badge } from '@/components/ui/badge';
|
||||||
import { Combobox } from '@/components/ui/combobox';
|
import { Combobox } from '@/components/ui/combobox';
|
||||||
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog';
|
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog';
|
||||||
@@ -37,9 +37,9 @@ const parkSchema = z.object({
|
|||||||
description: z.string().optional(),
|
description: z.string().optional(),
|
||||||
park_type: z.string().min(1, 'Park type is required'),
|
park_type: z.string().min(1, 'Park type is required'),
|
||||||
status: z.string().min(1, 'Status 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(),
|
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(),
|
closing_date_precision: z.enum(['day', 'month', 'year']).optional(),
|
||||||
location: z.object({
|
location: z.object({
|
||||||
name: z.string(),
|
name: z.string(),
|
||||||
@@ -176,8 +176,8 @@ export function ParkForm({ onSubmit, onCancel, initialData, isEditing = false }:
|
|||||||
description: initialData?.description || '',
|
description: initialData?.description || '',
|
||||||
park_type: initialData?.park_type || '',
|
park_type: initialData?.park_type || '',
|
||||||
status: initialData?.status || 'operating' as const, // Store DB value
|
status: initialData?.status || 'operating' as const, // Store DB value
|
||||||
opening_date: initialData?.opening_date || '',
|
opening_date: initialData?.opening_date || undefined,
|
||||||
closing_date: initialData?.closing_date || '',
|
closing_date: initialData?.closing_date || undefined,
|
||||||
location_id: initialData?.location_id || undefined,
|
location_id: initialData?.location_id || undefined,
|
||||||
website_url: initialData?.website_url || '',
|
website_url: initialData?.website_url || '',
|
||||||
phone: initialData?.phone || '',
|
phone: initialData?.phone || '',
|
||||||
@@ -380,7 +380,7 @@ export function ParkForm({ onSubmit, onCancel, initialData, isEditing = false }:
|
|||||||
value={watch('opening_date') ? parseDateOnly(watch('opening_date')!) : undefined}
|
value={watch('opening_date') ? parseDateOnly(watch('opening_date')!) : undefined}
|
||||||
precision={(watch('opening_date_precision') as DatePrecision) || 'day'}
|
precision={(watch('opening_date_precision') as DatePrecision) || 'day'}
|
||||||
onChange={(date, precision) => {
|
onChange={(date, precision) => {
|
||||||
setValue('opening_date', date ? toDateOnly(date) : undefined);
|
setValue('opening_date', date ? toDateWithPrecision(date, precision) : undefined);
|
||||||
setValue('opening_date_precision', precision);
|
setValue('opening_date_precision', precision);
|
||||||
}}
|
}}
|
||||||
label="Opening Date"
|
label="Opening Date"
|
||||||
@@ -393,7 +393,7 @@ export function ParkForm({ onSubmit, onCancel, initialData, isEditing = false }:
|
|||||||
value={watch('closing_date') ? parseDateOnly(watch('closing_date')!) : undefined}
|
value={watch('closing_date') ? parseDateOnly(watch('closing_date')!) : undefined}
|
||||||
precision={(watch('closing_date_precision') as DatePrecision) || 'day'}
|
precision={(watch('closing_date_precision') as DatePrecision) || 'day'}
|
||||||
onChange={(date, precision) => {
|
onChange={(date, precision) => {
|
||||||
setValue('closing_date', date ? toDateOnly(date) : undefined);
|
setValue('closing_date', date ? toDateWithPrecision(date, precision) : undefined);
|
||||||
setValue('closing_date_precision', precision);
|
setValue('closing_date_precision', precision);
|
||||||
}}
|
}}
|
||||||
label="Closing Date (if applicable)"
|
label="Closing Date (if applicable)"
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ import { Checkbox } from '@/components/ui/checkbox';
|
|||||||
import { toast } from '@/hooks/use-toast';
|
import { toast } from '@/hooks/use-toast';
|
||||||
import { handleError } from '@/lib/errorHandler';
|
import { handleError } from '@/lib/errorHandler';
|
||||||
import { Plus, Zap, Save, X, Building2 } from 'lucide-react';
|
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 { useUnitPreferences } from '@/hooks/useUnitPreferences';
|
||||||
import { useManufacturers, useRideModels } from '@/hooks/useAutocompleteData';
|
import { useManufacturers, useRideModels } from '@/hooks/useAutocompleteData';
|
||||||
import { useUserRole } from '@/hooks/useUserRole';
|
import { useUserRole } from '@/hooks/useUserRole';
|
||||||
@@ -224,9 +224,9 @@ export function RideForm({ onSubmit, onCancel, initialData, isEditing = false }:
|
|||||||
category: initialData?.category || '',
|
category: initialData?.category || '',
|
||||||
ride_sub_type: initialData?.ride_sub_type || '',
|
ride_sub_type: initialData?.ride_sub_type || '',
|
||||||
status: initialData?.status || 'operating' as const, // Store DB value directly
|
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',
|
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',
|
closing_date_precision: initialData?.closing_date_precision || 'day',
|
||||||
// Convert metric values to user's preferred unit for display
|
// Convert metric values to user's preferred unit for display
|
||||||
height_requirement: initialData?.height_requirement
|
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}
|
value={watch('opening_date') ? parseDateOnly(watch('opening_date')!) : undefined}
|
||||||
precision={(watch('opening_date_precision') as DatePrecision) || 'day'}
|
precision={(watch('opening_date_precision') as DatePrecision) || 'day'}
|
||||||
onChange={(date, precision) => {
|
onChange={(date, precision) => {
|
||||||
setValue('opening_date', date ? toDateOnly(date) : undefined);
|
setValue('opening_date', date ? toDateWithPrecision(date, precision) : undefined);
|
||||||
setValue('opening_date_precision', precision);
|
setValue('opening_date_precision', precision);
|
||||||
}}
|
}}
|
||||||
label="Opening Date"
|
label="Opening Date"
|
||||||
@@ -618,7 +618,7 @@ export function RideForm({ onSubmit, onCancel, initialData, isEditing = false }:
|
|||||||
value={watch('closing_date') ? parseDateOnly(watch('closing_date')!) : undefined}
|
value={watch('closing_date') ? parseDateOnly(watch('closing_date')!) : undefined}
|
||||||
precision={(watch('closing_date_precision') as DatePrecision) || 'day'}
|
precision={(watch('closing_date_precision') as DatePrecision) || 'day'}
|
||||||
onChange={(date, precision) => {
|
onChange={(date, precision) => {
|
||||||
setValue('closing_date', date ? toDateOnly(date) : undefined);
|
setValue('closing_date', date ? toDateWithPrecision(date, precision) : undefined);
|
||||||
setValue('closing_date_precision', precision);
|
setValue('closing_date_precision', precision);
|
||||||
}}
|
}}
|
||||||
label="Closing Date (if applicable)"
|
label="Closing Date (if applicable)"
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ export function transformParkData(submissionData: ParkSubmissionData): ParkInser
|
|||||||
description: submissionData.description || null,
|
description: submissionData.description || null,
|
||||||
park_type: submissionData.park_type,
|
park_type: submissionData.park_type,
|
||||||
status: normalizeStatus(submissionData.status),
|
status: normalizeStatus(submissionData.status),
|
||||||
opening_date: submissionData.opening_date || null,
|
opening_date: submissionData.opening_date?.trim() || null,
|
||||||
closing_date: submissionData.closing_date || null,
|
closing_date: submissionData.closing_date?.trim() || null,
|
||||||
website_url: submissionData.website_url || null,
|
website_url: submissionData.website_url || null,
|
||||||
phone: submissionData.phone || null,
|
phone: submissionData.phone || null,
|
||||||
email: submissionData.email || null,
|
email: submissionData.email || null,
|
||||||
@@ -62,8 +62,8 @@ export function transformRideData(submissionData: RideSubmissionData): RideInser
|
|||||||
ride_model_id: submissionData.ride_model_id || null,
|
ride_model_id: submissionData.ride_model_id || null,
|
||||||
manufacturer_id: submissionData.manufacturer_id || null,
|
manufacturer_id: submissionData.manufacturer_id || null,
|
||||||
designer_id: submissionData.designer_id || null,
|
designer_id: submissionData.designer_id || null,
|
||||||
opening_date: submissionData.opening_date || null,
|
opening_date: submissionData.opening_date?.trim() || null,
|
||||||
closing_date: submissionData.closing_date || null,
|
closing_date: submissionData.closing_date?.trim() || null,
|
||||||
height_requirement: submissionData.height_requirement || null,
|
height_requirement: submissionData.height_requirement || null,
|
||||||
age_requirement: submissionData.age_requirement || null,
|
age_requirement: submissionData.age_requirement || null,
|
||||||
capacity_per_hour: submissionData.capacity_per_hour || null,
|
capacity_per_hour: submissionData.capacity_per_hour || null,
|
||||||
|
|||||||
Reference in New Issue
Block a user