mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-24 17:51:13 -05:00
Fix designer form defaultValues
This commit is contained in:
@@ -2,6 +2,7 @@ import { useState } from 'react';
|
|||||||
import { useForm } from 'react-hook-form';
|
import { useForm } from 'react-hook-form';
|
||||||
import { zodResolver } from '@hookform/resolvers/zod';
|
import { zodResolver } from '@hookform/resolvers/zod';
|
||||||
import * as z from 'zod';
|
import * as z from 'zod';
|
||||||
|
import { entitySchemas } from '@/lib/entityValidationSchemas';
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
import { Input } from '@/components/ui/input';
|
import { Input } from '@/components/ui/input';
|
||||||
import { Textarea } from '@/components/ui/textarea';
|
import { Textarea } from '@/components/ui/textarea';
|
||||||
@@ -24,6 +25,7 @@ import { useNavigate } from 'react-router-dom';
|
|||||||
interface PropertyOwnerFormInput {
|
interface PropertyOwnerFormInput {
|
||||||
name: string;
|
name: string;
|
||||||
slug: string;
|
slug: string;
|
||||||
|
company_type: 'designer' | 'manufacturer' | 'operator' | 'property_owner';
|
||||||
description?: string;
|
description?: string;
|
||||||
person_type: 'company' | 'individual' | 'firm' | 'organization';
|
person_type: 'company' | 'individual' | 'firm' | 'organization';
|
||||||
founded_year?: string;
|
founded_year?: string;
|
||||||
@@ -38,37 +40,8 @@ interface PropertyOwnerFormInput {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const propertyOwnerSchema = z.object({
|
// Zod output type (after transformation)
|
||||||
name: z.string().min(1, 'Name is required'),
|
type PropertyOwnerFormData = z.infer<typeof entitySchemas.property_owner>;
|
||||||
slug: z.string().min(1, 'Slug is required'),
|
|
||||||
description: z.string().optional(),
|
|
||||||
person_type: z.enum(['company', 'individual', 'firm', 'organization']),
|
|
||||||
website_url: z.string().url().optional().or(z.literal('')),
|
|
||||||
founded_year: z.string()
|
|
||||||
.optional()
|
|
||||||
.transform(val => {
|
|
||||||
if (!val || val.trim() === '') return undefined;
|
|
||||||
const num = Number(val);
|
|
||||||
return isNaN(num) ? undefined : num;
|
|
||||||
})
|
|
||||||
.refine(val => val === undefined || (typeof val === 'number' && val >= 1800 && val <= new Date().getFullYear()), {
|
|
||||||
message: "Founded year must be between 1800 and current year"
|
|
||||||
}),
|
|
||||||
headquarters_location: z.string().optional(),
|
|
||||||
images: z.object({
|
|
||||||
uploaded: z.array(z.object({
|
|
||||||
url: z.string(),
|
|
||||||
cloudflare_id: z.string().optional(),
|
|
||||||
file: z.any().optional(),
|
|
||||||
isLocal: z.boolean().optional(),
|
|
||||||
caption: z.string().optional()
|
|
||||||
})),
|
|
||||||
banner_assignment: z.number().nullable().optional(),
|
|
||||||
card_assignment: z.number().nullable().optional()
|
|
||||||
}).optional()
|
|
||||||
});
|
|
||||||
|
|
||||||
type PropertyOwnerFormData = z.infer<typeof propertyOwnerSchema>;
|
|
||||||
|
|
||||||
interface PropertyOwnerFormProps {
|
interface PropertyOwnerFormProps {
|
||||||
onSubmit: (data: PropertyOwnerFormData) => void;
|
onSubmit: (data: PropertyOwnerFormData) => void;
|
||||||
@@ -94,10 +67,11 @@ export function PropertyOwnerForm({ onSubmit, onCancel, initialData }: PropertyO
|
|||||||
watch,
|
watch,
|
||||||
formState: { errors }
|
formState: { errors }
|
||||||
} = useForm({
|
} = useForm({
|
||||||
resolver: zodResolver(propertyOwnerSchema),
|
resolver: zodResolver(entitySchemas.property_owner),
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
name: initialData?.name || '',
|
name: initialData?.name || '',
|
||||||
slug: initialData?.slug || '',
|
slug: initialData?.slug || '',
|
||||||
|
company_type: 'property_owner' as const,
|
||||||
description: initialData?.description || '',
|
description: initialData?.description || '',
|
||||||
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 || '',
|
||||||
|
|||||||
Reference in New Issue
Block a user