"use client" import { z } from "zod" import { useForm } from "react-hook-form" import { zodResolver } from "@hookform/resolvers/zod" import { toast } from "sonner" import { useAuthApi } from "@/shared/useApi" import { Rhform, RhfTextField, RhfSelectField, RhfDateField, RhfTimeField, RhfAsyncSelectField, } from "@/shared/components/form" import { FieldGroup } from "@/shared/components/ui/field" import { Button } from "@/shared/components/ui/button" import { Dialog, DialogContent, DialogHeader, DialogTitle, } from "@/shared/components/ui/dialog" import { DEPARTMENT_ROUTES } from "@garage/api" import { FUEL_LEVEL_OPTIONS } from "./job-card.schema" import { toId } from "@/shared/lib/utils" // ── Schema ── const checkInSchema = z.object({ check_in_date: z.string().optional(), check_in_time: z.string().optional(), km_in: z.string().optional(), fuel_level: z.string().optional(), start_date: z.string().optional(), start_time: z.string().optional(), department: z.object({ value: z.any(), label: z.string() }).nullable().optional(), delivery_date: z.string().optional(), delivery_time: z.string().optional(), }) type CheckInFormValues = z.infer const mapLookupOption = (item: any) => ({ value: String(item.id), label: item.name, }) const STORE_OBJECT = { storeObject: true } as const // ── Props ── type JobCardCheckInDialogProps = { jobCardId: string open: boolean onOpenChange: (open: boolean) => void onSuccess?: () => void } // ── Component ── export function JobCardCheckInDialog({ jobCardId, open, onOpenChange, onSuccess, }: JobCardCheckInDialogProps) { const api = useAuthApi() const now = new Date() const todayStr = now.toISOString().split("T")[0] const currentTime = `${String(now.getHours()).padStart(2, "0")}:${String(now.getMinutes()).padStart(2, "0")}:${String(now.getSeconds()).padStart(2, "0")}` const form = useForm({ resolver: zodResolver(checkInSchema), defaultValues: { check_in_date: todayStr, check_in_time: currentTime, km_in: "", fuel_level: "", start_date: "", start_time: "", department: null, delivery_date: "", delivery_time: "", }, }) const handleSubmit = async (values: CheckInFormValues) => { try { await api.jobCards.checkIn(jobCardId, { check_in_date: values.check_in_date || undefined, check_in_time: values.check_in_time || undefined, km_in: values.km_in ? Number(values.km_in) : undefined, fuel_level: values.fuel_level || undefined, start_date: values.start_date || undefined, start_time: values.start_time || undefined, department_id: values.department ? Number(toId(values.department)) : undefined, delivery_date: values.delivery_date || undefined, delivery_time: values.delivery_time || undefined, }) toast.success("Job card checked in successfully") form.reset() onSuccess?.() } catch { toast.error("Failed to check in job card") } } return ( Check In
api.departments.list()} mapOption={(op:any)=> ({value: op.id, label: op.name})} {...STORE_OBJECT} getOptionLabel={op=>op.label} getOptionValue={op=>op} />
) }