"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, RhfSelectField, RhfTextareaField, RhfAsyncSelectField, RhfAutoGenerateField, } from "@/shared/components/form" 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 { invoiceFormSchema, type InvoiceFormValues, } from "./invoice.schema" import { INVOICE_ROUTES, DEPARTMENT_ROUTES, InvoiceStatus } from "@garage/api" import { RhfCustomerSelectField } from "@/modules/customers/rhf-customer-select-field" import { RhfVehicleSelectField } from "@/modules/vehicles/rhf-vehicle-select-field" // ── Constants ── const STATUS_OPTIONS = InvoiceStatus.map((v) => ({ value: v, label: v.replace(/_/g, " ").replace(/\b\w/g, (c) => c.toUpperCase()), })) // ── Props ── export type InvoiceFormProps = { resourceId?: string | null initialData?: unknown onSuccess?: () => void } // ── Default values ── const DEFAULT_VALUES: InvoiceFormValues = { subject: "", customer: null, vehicle: null, department: null, invoice_number: "", invoice_date: "", due_date: "", status: "draft", notes: "", } // ── Mapping helpers ── function mapToFormValues(data: unknown): InvoiceFormValues { const d = (data as any)?.data ?? data ?? {} return { subject: d.subject || "", customer: toRelation(d.customer_id, d.customer_name), vehicle: toRelation(d.vehicle_id, d.vehicle_name), department: toRelation(d.department_id, d.department_name), invoice_number: d.invoice_number || "", invoice_date: d.invoice_date || "", due_date: d.due_date || "", status: d.status || "draft", notes: d.notes || "", } } function mapFormToPayload(values: InvoiceFormValues) { return { subject: values.subject, customer_id: toId(values.customer), vehicle_id: toId(values.vehicle), department_id: toId(values.department), invoice_number: values.invoice_number || undefined, invoice_date: values.invoice_date || undefined, due_date: values.due_date || undefined, status: values.status || undefined, notes: values.notes || undefined, } } // ── Shared mapOption for async selects ── const mapLookupOption = (item: any) => ({ value: String(item.id), label: item.name, }) const STORE_OBJECT = { getOptionValue: (o: any) => o, getOptionLabel: (o: any) => o.label } // ── Component ── export function InvoiceForm({ resourceId, initialData, onSuccess }: InvoiceFormProps) { const api = useAuthApi() const { form, isEditing } = useResourceForm({ schema: invoiceFormSchema, defaultValues: DEFAULT_VALUES, resourceId, initialData, queryKey: [INVOICE_ROUTES.BY_ID, resourceId], mapToFormValues, }) const { mutate, error, isPending } = useFormMutation(form, { mutationFn: (values: InvoiceFormValues) => { const payload = mapFormToPayload(values) const promise = (isEditing && resourceId ? api.invoices.update(resourceId, payload) : api.invoices.create(payload)) as Promise toast.promise(promise, { loading: isEditing ? "Updating invoice..." : "Creating invoice...", success: isEditing ? "Invoice updated successfully" : "Invoice created successfully", error: isEditing ? "Failed to update invoice" : "Failed to create invoice", }) return promise }, onSuccess: () => { form.reset() onSuccess?.() }, }) return ( mutate(values)}> {error && ( {isEditing ? "Failed to update invoice" : "Failed to create invoice"} {error.message} )}
api.departments.list()} mapOption={mapLookupOption} {...STORE_OBJECT} />
) }