mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-22 10:11:13 -05:00
feat: Add operator is owner checkbox
This commit is contained in:
@@ -29,6 +29,7 @@ import type { TempCompanyData } from '@/types/company';
|
|||||||
import { LocationSearch } from './LocationSearch';
|
import { LocationSearch } from './LocationSearch';
|
||||||
import { OperatorForm } from './OperatorForm';
|
import { OperatorForm } from './OperatorForm';
|
||||||
import { PropertyOwnerForm } from './PropertyOwnerForm';
|
import { PropertyOwnerForm } from './PropertyOwnerForm';
|
||||||
|
import { Checkbox } from '@/components/ui/checkbox';
|
||||||
|
|
||||||
const parkSchema = z.object({
|
const parkSchema = z.object({
|
||||||
name: z.string().min(1, 'Park name is required'),
|
name: z.string().min(1, 'Park name is required'),
|
||||||
@@ -55,8 +56,8 @@ const parkSchema = z.object({
|
|||||||
website_url: z.string().url().optional().or(z.literal('')),
|
website_url: z.string().url().optional().or(z.literal('')),
|
||||||
phone: z.string().optional(),
|
phone: z.string().optional(),
|
||||||
email: z.string().email().optional().or(z.literal('')),
|
email: z.string().email().optional().or(z.literal('')),
|
||||||
operator_id: z.string().uuid().optional(),
|
operator_id: z.string().uuid().optional().or(z.literal('')).transform(val => val || undefined),
|
||||||
property_owner_id: z.string().uuid().optional(),
|
property_owner_id: z.string().uuid().optional().or(z.literal('')).transform(val => val || undefined),
|
||||||
images: z.object({
|
images: z.object({
|
||||||
uploaded: z.array(z.object({
|
uploaded: z.array(z.object({
|
||||||
url: z.string(),
|
url: z.string(),
|
||||||
@@ -148,6 +149,12 @@ export function ParkForm({ onSubmit, onCancel, initialData, isEditing = false }:
|
|||||||
const [tempNewPropertyOwner, setTempNewPropertyOwner] = useState<TempCompanyData | null>(null);
|
const [tempNewPropertyOwner, setTempNewPropertyOwner] = useState<TempCompanyData | null>(null);
|
||||||
const [isPropertyOwnerModalOpen, setIsPropertyOwnerModalOpen] = useState(false);
|
const [isPropertyOwnerModalOpen, setIsPropertyOwnerModalOpen] = useState(false);
|
||||||
|
|
||||||
|
// Operator is Owner checkbox state
|
||||||
|
const [operatorIsOwner, setOperatorIsOwner] = useState<boolean>(
|
||||||
|
!!(initialData?.operator_id && initialData?.property_owner_id &&
|
||||||
|
initialData?.operator_id === initialData?.property_owner_id)
|
||||||
|
);
|
||||||
|
|
||||||
// Fetch data
|
// Fetch data
|
||||||
const { operators, loading: operatorsLoading } = useOperators();
|
const { operators, loading: operatorsLoading } = useOperators();
|
||||||
const { propertyOwners, loading: ownersLoading } = usePropertyOwners();
|
const { propertyOwners, loading: ownersLoading } = usePropertyOwners();
|
||||||
@@ -178,6 +185,14 @@ export function ParkForm({ onSubmit, onCancel, initialData, isEditing = false }:
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Sync property owner with operator when checkbox is enabled
|
||||||
|
useEffect(() => {
|
||||||
|
if (operatorIsOwner && selectedOperatorId) {
|
||||||
|
setSelectedPropertyOwnerId(selectedOperatorId);
|
||||||
|
setValue('property_owner_id', selectedOperatorId);
|
||||||
|
}
|
||||||
|
}, [operatorIsOwner, selectedOperatorId, setValue]);
|
||||||
|
|
||||||
|
|
||||||
const handleFormSubmit = async (data: ParkFormData) => {
|
const handleFormSubmit = async (data: ParkFormData) => {
|
||||||
try {
|
try {
|
||||||
@@ -198,10 +213,15 @@ export function ParkForm({ onSubmit, onCancel, initialData, isEditing = false }:
|
|||||||
submissionContent.park.property_owner_id = null;
|
submissionContent.park.property_owner_id = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const finalOperatorId = tempNewOperator ? undefined : (selectedOperatorId || undefined);
|
||||||
|
const finalPropertyOwnerId = operatorIsOwner
|
||||||
|
? finalOperatorId
|
||||||
|
: (tempNewPropertyOwner ? undefined : (selectedPropertyOwnerId || undefined));
|
||||||
|
|
||||||
await onSubmit({
|
await onSubmit({
|
||||||
...data,
|
...data,
|
||||||
operator_id: tempNewOperator ? undefined : (selectedOperatorId || undefined),
|
operator_id: finalOperatorId,
|
||||||
property_owner_id: tempNewPropertyOwner ? undefined : (selectedPropertyOwnerId || undefined),
|
property_owner_id: finalPropertyOwnerId,
|
||||||
_compositeSubmission: (tempNewOperator || tempNewPropertyOwner) ? submissionContent : undefined
|
_compositeSubmission: (tempNewOperator || tempNewPropertyOwner) ? submissionContent : undefined
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -363,6 +383,24 @@ export function ParkForm({ onSubmit, onCancel, initialData, isEditing = false }:
|
|||||||
<div className="space-y-4">
|
<div className="space-y-4">
|
||||||
<h3 className="text-lg font-semibold">Operator & Property Owner</h3>
|
<h3 className="text-lg font-semibold">Operator & Property Owner</h3>
|
||||||
|
|
||||||
|
<div className="flex items-center space-x-2 mb-4">
|
||||||
|
<Checkbox
|
||||||
|
id="operator-is-owner"
|
||||||
|
checked={operatorIsOwner}
|
||||||
|
onCheckedChange={(checked) => {
|
||||||
|
setOperatorIsOwner(checked as boolean);
|
||||||
|
if (checked && selectedOperatorId) {
|
||||||
|
setSelectedPropertyOwnerId(selectedOperatorId);
|
||||||
|
setValue('property_owner_id', selectedOperatorId);
|
||||||
|
setTempNewPropertyOwner(null);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<Label htmlFor="operator-is-owner" className="text-sm font-normal cursor-pointer">
|
||||||
|
Operator is also the property owner
|
||||||
|
</Label>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
||||||
{/* Operator Column */}
|
{/* Operator Column */}
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
@@ -384,10 +422,11 @@ export function ParkForm({ onSubmit, onCancel, initialData, isEditing = false }:
|
|||||||
) : (
|
) : (
|
||||||
<Combobox
|
<Combobox
|
||||||
options={operators}
|
options={operators}
|
||||||
value={watch('operator_id')}
|
value={watch('operator_id') || ''}
|
||||||
onValueChange={(value) => {
|
onValueChange={(value) => {
|
||||||
setValue('operator_id', value);
|
const cleanValue = value || undefined;
|
||||||
setSelectedOperatorId(value);
|
setValue('operator_id', cleanValue);
|
||||||
|
setSelectedOperatorId(cleanValue || '');
|
||||||
}}
|
}}
|
||||||
placeholder="Select operator"
|
placeholder="Select operator"
|
||||||
searchPlaceholder="Search operators..."
|
searchPlaceholder="Search operators..."
|
||||||
@@ -411,6 +450,7 @@ export function ParkForm({ onSubmit, onCancel, initialData, isEditing = false }:
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Property Owner Column */}
|
{/* Property Owner Column */}
|
||||||
|
{!operatorIsOwner && (
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<Label>Property Owner</Label>
|
<Label>Property Owner</Label>
|
||||||
|
|
||||||
@@ -430,10 +470,11 @@ export function ParkForm({ onSubmit, onCancel, initialData, isEditing = false }:
|
|||||||
) : (
|
) : (
|
||||||
<Combobox
|
<Combobox
|
||||||
options={propertyOwners}
|
options={propertyOwners}
|
||||||
value={watch('property_owner_id')}
|
value={watch('property_owner_id') || ''}
|
||||||
onValueChange={(value) => {
|
onValueChange={(value) => {
|
||||||
setValue('property_owner_id', value);
|
const cleanValue = value || undefined;
|
||||||
setSelectedPropertyOwnerId(value);
|
setValue('property_owner_id', cleanValue);
|
||||||
|
setSelectedPropertyOwnerId(cleanValue || '');
|
||||||
}}
|
}}
|
||||||
placeholder="Select property owner"
|
placeholder="Select property owner"
|
||||||
searchPlaceholder="Search property owners..."
|
searchPlaceholder="Search property owners..."
|
||||||
@@ -455,6 +496,7 @@ export function ParkForm({ onSubmit, onCancel, initialData, isEditing = false }:
|
|||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -46,8 +46,8 @@ export const parkValidationSchema = z.object({
|
|||||||
if (!val || val === '') return true;
|
if (!val || val === '') return true;
|
||||||
return z.string().email().safeParse(val).success;
|
return z.string().email().safeParse(val).success;
|
||||||
}, 'Invalid email format'),
|
}, 'Invalid email format'),
|
||||||
operator_id: z.string().uuid().optional().nullable(),
|
operator_id: z.string().uuid().optional().nullable().or(z.literal('')).transform(val => val || undefined),
|
||||||
property_owner_id: z.string().uuid().optional().nullable(),
|
property_owner_id: z.string().uuid().optional().nullable().or(z.literal('')).transform(val => val || undefined),
|
||||||
banner_image_id: z.string().optional(),
|
banner_image_id: z.string().optional(),
|
||||||
banner_image_url: z.string().optional(),
|
banner_image_url: z.string().optional(),
|
||||||
card_image_id: z.string().optional(),
|
card_image_id: z.string().optional(),
|
||||||
|
|||||||
Reference in New Issue
Block a user