Tightened frontend zod schemas where backend required fields the frontend marked optional, and matched enum/format constraints. Schemas: - vehicle-document: document_type required - parts: shop_type, category, unit_type, department, sku required - inspections: customer, vehicle, department, inspection_category, employee, order_number, date, time required - appointments: service_writer required, cross-field to_time > from_time - vendor-credits: vendor + vendor_credit_date required - job-cards: documents[].document_type_id required - expense-items: category, unit_type, department, sku required - inventory-adjustments: reference_number, date required - tasks: task_type, task_section required - shop-timings: in_time, out_time enforce HH:MM:SS regex - vendor-credit: subject + vendor + vendor_credit_date required Settings: - shop-type: shop_type + note required - insurance-types: description field added, required - make-and-models: shop_type required, year 1900..2100 - departments: assignment_type required enum (AssignmentType) - company: website URL validation, latitude/longitude range, first_day_of_work enum, string max lengths - configurations: 4 fields enum-typed (was raw strings) Inline forms: - job-card service/part/expense-item: relations required (part/service/expense_item/department) - job-card recommendation: max 255 - vehicles/inline-forms/shop-type: shop_type + note required - vehicles/inline-forms/body-type: shop_type_id pulled from parent form context, guard on submit - vehicles/inline-forms/color: code field added, required - services/inline-forms/department: assignment_type required enum - tasks/task-section: arrangement required integer - invoices/invoice-edit: status + discount enum-typed Auth: - login: password min 6 (was 8; backend allows 6) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
84 lines
2.9 KiB
TypeScript
84 lines
2.9 KiB
TypeScript
"use client"
|
|
|
|
import { AlertTriangle, Save } from "lucide-react"
|
|
import { useForm } from "react-hook-form"
|
|
import { zodResolver } from "@hookform/resolvers/zod"
|
|
import { toast } from "sonner"
|
|
|
|
import { Button } from "@/shared/components/ui/button"
|
|
import { Alert, AlertTitle } from "@/shared/components/ui/alert"
|
|
import { FieldGroup } from "@/shared/components/ui/field"
|
|
import { Rhform, RhfSelectField } from "@/shared/components/form"
|
|
import { useAuthApi } from "@/shared/useApi"
|
|
import { useFormMutation } from "@/shared/hooks/use-form-mutation"
|
|
|
|
import { TaxInclusive, DiscountType } from "@garage/api"
|
|
import { salesConfigFormSchema, type SalesConfigFormValues } from "./configurations.schema"
|
|
|
|
const TAX_INCLUSIVE_OPTIONS = TaxInclusive.map((v) => ({ value: v, label: v }))
|
|
|
|
const DISCOUNT_OPTIONS = DiscountType.map((v) => ({
|
|
value: v,
|
|
label: v === "no" ? "No Discount" : v === "line_item_level" ? "Line Item Level" : "Transaction Level",
|
|
}))
|
|
|
|
const DEFAULT_VALUES: SalesConfigFormValues = {
|
|
sell_rates_tax_inclusive: undefined,
|
|
give_discounts: undefined,
|
|
}
|
|
|
|
export function SalesConfigForm() {
|
|
const api = useAuthApi()
|
|
|
|
const form = useForm<SalesConfigFormValues>({
|
|
resolver: zodResolver(salesConfigFormSchema) as any,
|
|
defaultValues: DEFAULT_VALUES,
|
|
})
|
|
|
|
const { mutate, error, isPending } = useFormMutation(form, {
|
|
mutationFn: (values: SalesConfigFormValues) => {
|
|
const promise = api.configurations.updateSalesTaxDiscount(values)
|
|
toast.promise(promise, {
|
|
loading: "Saving sales configuration...",
|
|
success: "Sales configuration saved",
|
|
error: "Failed to save sales configuration",
|
|
})
|
|
return promise
|
|
},
|
|
})
|
|
|
|
return (
|
|
<Rhform form={form} onSubmit={(values) => mutate(values)}>
|
|
{error && (
|
|
<Alert variant="destructive" className="mb-4">
|
|
<AlertTriangle className="me-2 h-4 w-4" />
|
|
<AlertTitle>Failed to save sales configuration</AlertTitle>
|
|
{error.message}
|
|
</Alert>
|
|
)}
|
|
|
|
<FieldGroup>
|
|
<RhfSelectField
|
|
name="sell_rates_tax_inclusive"
|
|
label="Sell Rates Tax"
|
|
placeholder="Select tax type"
|
|
options={TAX_INCLUSIVE_OPTIONS}
|
|
/>
|
|
<RhfSelectField
|
|
name="give_discounts"
|
|
label="Give Discounts"
|
|
placeholder="Select discount type"
|
|
options={DISCOUNT_OPTIONS}
|
|
/>
|
|
|
|
<div className="flex justify-end">
|
|
<Button type="submit" disabled={isPending}>
|
|
<Save />
|
|
{isPending ? "Saving..." : "Save"}
|
|
</Button>
|
|
</div>
|
|
</FieldGroup>
|
|
</Rhform>
|
|
)
|
|
}
|