update forms

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
2026-05-07 21:02:15 +03:00
co-authored by Copilot
parent dd32658500
commit 38565298fc
32 changed files with 797 additions and 236 deletions
@@ -11,6 +11,8 @@ import {
RhfAsyncSelectField,
RhfDateField,
RhfAutoGenerateField,
RhfSelectField,
RhfTextareaField,
} from "@/shared/components/form"
import { toast } from "sonner"
import { useAuthApi } from "@/shared/useApi"
@@ -22,11 +24,12 @@ import {
estimateFormSchema,
type EstimateFormValues,
} from "./estimate.schema"
import { ESTIMATE_ROUTES, DEPARTMENT_ROUTES } from "@garage/api"
import { ESTIMATE_ROUTES, DEPARTMENT_ROUTES, INSURANCE_TYPE_ROUTES, DiscountType } from "@garage/api"
import { RhfCustomerSelectField } from "@/modules/customers/rhf-customer-select-field"
import { RhfVehicleSelectField } from "@/modules/vehicles/rhf-vehicle-select-field"
import { RhfLabelPickerField, type LabelItem } from "@/modules/labels/rhf-label-picker-field"
import { RhfCustomerRemarksField } from "./rhf-customer-remarks-field"
import { RhfEmployeeSelectField } from "@/modules/employees/rhf-employee-select-field"
// ── Props ──
@@ -44,9 +47,16 @@ const DEFAULT_VALUES: EstimateFormValues = {
customer: null,
vehicle: null,
department: null,
insurance_type: null,
insurer: null,
service_writer: null,
estimate_number: "",
date: "",
has_insurance: false,
enable_digital_authorisation: false,
footer: "",
discount: "no",
discount_amount: undefined,
remarks: [],
labels: [],
}
@@ -61,9 +71,24 @@ function mapToFormValues(data: unknown): EstimateFormValues {
customer: toRelation(d.customer_id, d.customer_name),
vehicle: toRelation(d.vehicle_id, d.vehicle_name),
department: toRelation(d.department_id, d.department_name),
insurance_type: toRelation(d.insurance_type_id, d.insurance_type_title ?? d.insurance_type_name ?? d.insurance_type?.title),
insurer: toRelation(
d.insurer_id,
d.insurer_name
?? [d.insurer?.first_name, d.insurer?.last_name].filter(Boolean).join(" ")
?? d.insurer?.company_name,
),
service_writer: toRelation(
d.service_writer_id,
d.service_writer_name ?? [d.service_writer?.first_name, d.service_writer?.last_name].filter(Boolean).join(" "),
),
estimate_number: d.estimate_number || "",
date: d.date ? d.date.split("T")[0] : "",
has_insurance: d.has_insurance ?? false,
enable_digital_authorisation: d.enable_digital_authorisation ?? false,
footer: d.footer ?? "",
discount: d.discount ?? d.discount_type ?? "no",
discount_amount: d.discount_amount != null ? Number(d.discount_amount) : undefined,
remarks: Array.isArray(d.remarks)
? d.remarks.map((r: any) => (typeof r === "string" ? r : (r?.remark ?? ""))).filter(Boolean)
: [],
@@ -81,9 +106,16 @@ function mapFormToPayload(values: EstimateFormValues) {
customer_id: toId(values.customer),
vehicle_id: toId(values.vehicle),
department_id: toId(values.department),
insurance_type_id: values.insurance_type ? String(toId(values.insurance_type)) : null,
insurer_id: toId(values.insurer),
service_writer_id: toId(values.service_writer),
estimate_number: values.estimate_number || undefined,
date: values.date || undefined,
has_insurance: values.has_insurance,
enable_digital_authorisation: values.enable_digital_authorisation,
footer: values.footer || undefined,
discount: values.discount || undefined,
discount_amount: values.discount_amount,
remarks: values.remarks?.filter(Boolean) ?? [],
label_ids: values.labels?.map((l) => l.id) ?? [],
}
@@ -98,6 +130,11 @@ const mapLookupOption = (item: any) => ({
const STORE_OBJECT = { getOptionValue: (o: any) => o, getOptionLabel: (o: any) => o.label }
const DISCOUNT_OPTIONS = DiscountType.map((value) => ({
value,
label: value.replace(/_/g, " ").replace(/\b\w/g, (c) => c.toUpperCase()),
}))
// ── Component ──
export function EstimateForm({ resourceId, initialData, onSuccess }: EstimateFormProps) {
@@ -116,8 +153,8 @@ export function EstimateForm({ resourceId, initialData, onSuccess }: EstimateFor
mutationFn: (values: EstimateFormValues) => {
const payload = mapFormToPayload(values)
const promise = (isEditing && resourceId
? api.estimates.update(resourceId, payload)
: api.estimates.create(payload)) as Promise<any>
? api.estimates.update(resourceId, payload as any)
: api.estimates.create(payload as any)) as Promise<any>
toast.promise(promise, {
loading: isEditing ? "Updating estimate..." : "Creating estimate...",
success: isEditing ? "Estimate updated successfully" : "Estimate created successfully",
@@ -149,8 +186,8 @@ export function EstimateForm({ resourceId, initialData, onSuccess }: EstimateFor
<RhfTextField name="title" label="Title" placeholder="Estimate title" required />
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
<RhfCustomerSelectField name="customer" />
<RhfVehicleSelectField name="vehicle" />
<RhfCustomerSelectField name="customer" required />
<RhfVehicleSelectField name="vehicle" required />
</div>
<div className="grid grid-cols-1 gap-4 sm:grid-cols-3">
@@ -164,11 +201,39 @@ export function EstimateForm({ resourceId, initialData, onSuccess }: EstimateFor
{...STORE_OBJECT}
/>
<RhfDateField name="date" label="Date" />
<RhfAutoGenerateField autoFetch name="estimate_number" label="Estimate#" placeholder="EST-001" table="estimates" />
<RhfAutoGenerateField autoFetch name="estimate_number" label="Estimate#" placeholder="EST-001" table="estimates" required />
</div>
<RhfCheckboxField name="has_insurance" label="Has Insurance" />
<div className="grid grid-cols-1 gap-4 sm:grid-cols-3">
<RhfAsyncSelectField
name="insurance_type"
label="Insurance Type"
placeholder="Select insurance type"
queryKey={[INSURANCE_TYPE_ROUTES.INDEX]}
listFn={() => api.insuranceTypes.list()}
mapOption={mapLookupOption}
{...STORE_OBJECT}
/>
<RhfCustomerSelectField
name="insurer"
label="Insurer"
placeholder="Search insurer..."
customerType="Insurer"
/>
<RhfEmployeeSelectField name="service_writer" label="Service Writer" />
</div>
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
<RhfSelectField name="discount" label="Discount" options={DISCOUNT_OPTIONS} />
<RhfTextField name="discount_amount" label="Discount Amount" type="number" placeholder="0.00" />
</div>
<RhfCheckboxField name="enable_digital_authorisation" label="Enable Digital Authorisation" />
<RhfTextareaField name="footer" label="Footer" placeholder="Footer notes for this estimate" />
<RhfCustomerRemarksField name="remarks" />
<Button type="submit" variant="default" disabled={isPending}>
@@ -1,22 +1,49 @@
import { z } from "zod"
import { DiscountType } from "@garage/api"
const relationFieldSchema = z
.object({ value: z.string(), label: z.string() })
.nullable()
const requiredRelationFieldSchema = relationFieldSchema.superRefine((value, ctx) => {
if (!value) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "This field is required",
})
}
})
const optionalNumberSchema = z.preprocess(
(value) => (value === "" || value == null ? undefined : value),
z.coerce.number().min(0).optional(),
)
const optionalDateSchema = z.preprocess(
(value) => (value === "" || value == null ? undefined : value),
z.string().date().optional(),
)
const estimateFormSchema = z.object({
// ── Required fields ──
title: z.string().min(1, "Title is required"),
title: z.string().trim().min(1, "Title is required").max(255, "Title must be at most 255 characters"),
// ── Relations ──
customer: relationFieldSchema,
vehicle: relationFieldSchema,
department: relationFieldSchema,
customer: requiredRelationFieldSchema,
vehicle: requiredRelationFieldSchema,
department: requiredRelationFieldSchema,
insurance_type: relationFieldSchema,
insurer: relationFieldSchema,
service_writer: relationFieldSchema,
// ── Optional fields ──
estimate_number: z.string().optional(),
date: z.string().optional(),
has_insurance: z.boolean().default(false),
estimate_number: z.string().trim().min(1, "Estimate number is required").max(255, "Estimate number must be at most 255 characters"),
date: optionalDateSchema,
has_insurance: z.boolean().optional(),
enable_digital_authorisation: z.boolean().optional(),
footer: z.string().optional(),
discount: z.enum(DiscountType).optional(),
discount_amount: optionalNumberSchema,
// ── Remarks (array of strings) ──
remarks: z.array(z.string()).optional(),