"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, } 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 { creditNoteFormSchema, type CreditNoteFormValues, } from "./credit-note.schema" import { CREDIT_NOTE_ROUTES, DEPARTMENT_ROUTES, CreditNoteStatus } from "@garage/api" import { RhfCustomerSelectField } from "@/modules/customers/rhf-customer-select-field" // ── 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 } // ── Constants ── const STATUS_OPTIONS = CreditNoteStatus.map((v) => ({ value: v, label: v.replace(/_/g, " ").replace(/\b\w/g, (c) => c.toUpperCase()), })) // ── Props ── export type CreditNoteFormProps = { resourceId?: string | null initialData?: unknown onSuccess?: () => void } // ── Default values ── const DEFAULT_VALUES: CreditNoteFormValues = { subject: "", customer: null, department: null, date: "", status: "draft", notes: "", } // ── Mapping helpers ── function mapToFormValues(data: unknown): CreditNoteFormValues { const d = (data as any)?.data ?? data ?? {} return { subject: d.subject || "", customer: toRelation(d.customer_id, d.customer_name), department: toRelation(d.department_id, d.department_name), date: d.date || "", status: d.status || "draft", notes: d.notes || "", } } function mapFormToPayload(values: CreditNoteFormValues) { return { subject: values.subject, customer_id: toId(values.customer), department_id: toId(values.department), date: values.date || undefined, status: values.status, notes: values.notes || undefined, } } // ── Component ── export function CreditNoteForm({ resourceId, initialData, onSuccess }: CreditNoteFormProps) { const api = useAuthApi() const { form, isEditing } = useResourceForm({ schema: creditNoteFormSchema, defaultValues: DEFAULT_VALUES, resourceId: resourceId ?? null, initialData, queryKey: [CREDIT_NOTE_ROUTES.BY_ID, resourceId], initialize: (id) => api.creditNotes.show(id), mapToFormValues, }) const { mutate, error, isPending } = useFormMutation(form, { mutationFn: (values: CreditNoteFormValues) => { const payload = mapFormToPayload(values) const promise = (isEditing && resourceId ? api.creditNotes.update(resourceId, payload) : api.creditNotes.create(payload)) as Promise toast.promise(promise, { loading: isEditing ? "Updating credit note..." : "Creating credit note...", success: isEditing ? "Credit note updated successfully" : "Credit note created successfully", error: isEditing ? "Failed to update credit note" : "Failed to create credit note", }) return promise }, onSuccess: () => { form.reset() onSuccess?.() }, }) return ( mutate(values)}> {error && ( {isEditing ? "Failed to update credit note" : "Failed to create credit note"} {error.message} )}
api.departments.list()} mapOption={mapLookupOption} {...STORE_OBJECT} />
) }