"use client" import { AlertTriangle, Plus, Save } from "lucide-react" import { Button } from "@/shared/components/ui/button" import { Alert, AlertTitle } from "@/shared/components/ui/alert" import { FieldGroup } from "@/shared/components/ui/field" import { Rhform, RhfTextField, RhfTextareaField, RhfAsyncSelectField, RhfImageField, } from "@/shared/components/form" import { ShopTypeInlineForm } from "./inline-forms/shop-type-inline-form" import { BodyTypeInlineForm } from "./inline-forms/body-type-inline-form" import { FuelTypeInlineForm } from "./inline-forms/fuel-type-inline-form" import { TransmissionInlineForm } from "./inline-forms/transmission-inline-form" import { ColorInlineForm } from "./inline-forms/color-inline-form" import { RhfVehicleIdentityField } from "./rhf-vehicle-identity-field" import { toast } from "sonner" import { useAuthApi } from "@/shared/useApi" import { useResourceForm } from "@/shared/hooks/use-resource-form" import { useFormMutation } from "@/shared/hooks/use-form-mutation" import { toRelation, toId } from "@/shared/lib/utils" import { vehicleFormSchema, type VehicleFormValues } from "./vehicle.schema" import { VEHICLE_ROUTES } from "@garage/api" // ── Props ── export type VehicleFormProps = { resourceId?: string | null initialData?: unknown onSuccess?: (data?: any) => void } // ── Default values ── const DEFAULT_VALUES: VehicleFormValues = { shop_type: null, vehicle_body_type: null, vehicle_fuel_type: null, vehicle_transmission: null, vehicle_color: null, make: "", model: "", year: "", sub_model: "", license_plate: "", vin_number: "", engine_size: "", drivetrain: "", mileage: "", owners_number: "", note: "", image: null, } // ── Mapping helpers ── const mapLookupOption = (item: any) => ({ value: String(item.id), label: item.name ?? item.title ?? String(item.id), }) const STORE_OBJECT = { getOptionValue: (o: any) => o, getOptionLabel: (o: any) => o.label } function mapToFormValues(data: unknown): VehicleFormValues { const d = (data as any)?.data ?? data ?? {} return { shop_type: toRelation(d.shop_type_id, d.shop_type?.title), vehicle_body_type: toRelation(d.vehicle_body_type_id, d.vehicle_body_type?.title), vehicle_fuel_type: toRelation(d.vehicle_fuel_type_id, d.vehicle_fuel_type?.title), vehicle_transmission: toRelation(d.vehicle_transmission_id, d.vehicle_transmission?.title), vehicle_color: toRelation(d.vehicle_color_id, d.vehicle_color?.title), make: d.make || "", model: d.model || "", year: d.year || "", sub_model: d.sub_model || "", license_plate: d.license_plate || "", vin_number: d.vin_number || "", engine_size: d.engine_size || "", drivetrain: d.drivetrain || "", mileage: d.mileage || "", owners_number: d.owners_number || "", note: d.note || "", image: null, } } function mapToPayload(values: VehicleFormValues) { return { shop_type_id: toId(values.shop_type), vehicle_body_type_id: toId(values.vehicle_body_type), vehicle_fuel_type_id: toId(values.vehicle_fuel_type), vehicle_transmission_id: toId(values.vehicle_transmission), vehicle_color_id: toId(values.vehicle_color), make: values.make, model: values.model, year: values.year, sub_model: values.sub_model || undefined, license_plate: values.license_plate || undefined, vin_number: values.vin_number || undefined, engine_size: values.engine_size || undefined, drivetrain: values.drivetrain || undefined, mileage: values.mileage || undefined, owners_number: values.owners_number || undefined, note: values.note || undefined, image: values.image instanceof File ? values.image : undefined, } } // ── Component ── export function VehicleForm({ resourceId, initialData, onSuccess }: VehicleFormProps) { const api = useAuthApi() const { form, isEditing } = useResourceForm({ schema: vehicleFormSchema, defaultValues: DEFAULT_VALUES, resourceId, initialData, mapToFormValues, }) const { mutate, error, isPending } = useFormMutation(form, { mutationFn: (values: VehicleFormValues) => { const payload = mapToPayload(values) const promise = isEditing && resourceId ? api.vehicles.update(resourceId, payload as any) : api.vehicles.create(payload as any) toast.promise(promise, { loading: isEditing ? "Updating vehicle..." : "Creating vehicle...", success: isEditing ? "Vehicle updated successfully" : "Vehicle created successfully", error: isEditing ? "Failed to update vehicle" : "Failed to create vehicle", }) return promise }, onSuccess: (data) => { form.reset() onSuccess?.(data) }, }) return ( mutate(values)}> {error && ( {isEditing ? "Failed to update vehicle" : "Failed to create vehicle"} {error.message} )} {/* Vehicle identity — cascading make/model/year/sub_model autocomplete */} {/* Associations */}
api.shopTypes.list()} mapOption={mapLookupOption} createForm={(props) => } createLabel="Shop Type" {...STORE_OBJECT} /> api.vehicleAttributes.listBodyTypes()} mapOption={mapLookupOption} createForm={(props) => } createLabel="Body Type" {...STORE_OBJECT} />
api.vehicleAttributes.listFuelTypes()} mapOption={mapLookupOption} createForm={(props) => } createLabel="Fuel Type" {...STORE_OBJECT} /> api.vehicleAttributes.listTransmissions()} mapOption={mapLookupOption} createForm={(props) => } createLabel="Transmission" {...STORE_OBJECT} />
api.vehicleAttributes.listColors()} mapOption={mapLookupOption} createForm={(props) => } createLabel="Color" {...STORE_OBJECT} />
{/* Technical specs */}
) }