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>
111 lines
3.7 KiB
TypeScript
111 lines
3.7 KiB
TypeScript
"use client"
|
|
|
|
import { z } from "zod"
|
|
import { useForm } from "react-hook-form"
|
|
import { zodResolver } from "@hookform/resolvers/zod"
|
|
import { Plus, Save } from "lucide-react"
|
|
import { Button } from "@/shared/components/ui/button"
|
|
import { FieldGroup } from "@/shared/components/ui/field"
|
|
import { Rhform, RhfTextField, RhfCheckboxField } from "@/shared/components/form"
|
|
import { toast } from "sonner"
|
|
import { useAuthApi } from "@/shared/useApi"
|
|
import { useEffect } from "react"
|
|
|
|
// ── Schema ──
|
|
|
|
const taskSectionSchema = z.object({
|
|
title: z.string().min(1, "Title is required").max(255, "Title cannot exceed 255 characters"),
|
|
arrangement: z
|
|
.string()
|
|
.min(1, "Arrangement is required")
|
|
.refine((v) => /^-?\d+$/.test(v), "Arrangement must be an integer"),
|
|
is_default: z.boolean().optional(),
|
|
})
|
|
|
|
type TaskSectionFormValues = z.infer<typeof taskSectionSchema>
|
|
|
|
// ── Props ──
|
|
|
|
type TaskSectionFormProps = {
|
|
resourceId?: string | null
|
|
initialData?: any
|
|
onSuccess?: () => void
|
|
}
|
|
|
|
// ── Component ──
|
|
|
|
export function TaskSectionForm({ resourceId, initialData, onSuccess }: TaskSectionFormProps) {
|
|
const api = useAuthApi()
|
|
const isEditing = !!resourceId
|
|
|
|
const form = useForm<TaskSectionFormValues>({
|
|
resolver: zodResolver(taskSectionSchema),
|
|
defaultValues: { title: "", arrangement: "", is_default: false },
|
|
})
|
|
|
|
useEffect(() => {
|
|
if (initialData) {
|
|
const d = initialData?.data ?? initialData
|
|
form.reset({
|
|
title: d.title ?? "",
|
|
arrangement: d.arrangement != null ? String(d.arrangement) : "",
|
|
is_default: d.is_default ?? false,
|
|
})
|
|
}
|
|
}, [initialData, form])
|
|
|
|
const handleSubmit = async (values: TaskSectionFormValues) => {
|
|
try {
|
|
const promise = isEditing
|
|
? api.taskSections.update(resourceId!, {
|
|
title: values.title,
|
|
arrangement: values.arrangement ? Number(values.arrangement) : undefined,
|
|
is_default: values.is_default,
|
|
})
|
|
: api.taskSections.create({
|
|
title: values.title,
|
|
arrangement: values.arrangement ? Number(values.arrangement) : undefined,
|
|
is_default: values.is_default,
|
|
})
|
|
|
|
toast.promise(promise, {
|
|
loading: isEditing ? "Updating..." : "Creating...",
|
|
success: isEditing ? "Updated successfully" : "Created successfully",
|
|
error: isEditing ? "Failed to update" : "Failed to create",
|
|
})
|
|
|
|
await promise
|
|
form.reset()
|
|
onSuccess?.()
|
|
} catch {
|
|
// toast already shown
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Rhform form={form} onSubmit={handleSubmit}>
|
|
<FieldGroup>
|
|
<RhfTextField
|
|
name="title"
|
|
label="Title"
|
|
placeholder="e.g. Pre-Work"
|
|
required
|
|
/>
|
|
<RhfTextField
|
|
name="arrangement"
|
|
label="Arrangement Order"
|
|
placeholder="e.g. 1"
|
|
type="number"
|
|
/>
|
|
<RhfCheckboxField name="is_default" label="Set as default" />
|
|
<Button type="submit" disabled={form.formState.isSubmitting}>
|
|
{isEditing ? <Save className="h-4 w-4" /> : <Plus className="h-4 w-4" />}
|
|
{form.formState.isSubmitting
|
|
? isEditing ? "Updating..." : "Creating..."
|
|
: isEditing ? "Update" : "Create"}
|
|
</Button>
|
|
</FieldGroup>
|
|
</Rhform>
|
|
)
|
|
}
|