"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, RhfAsyncSelectField, RhfCheckboxField, } 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 { employeeFormSchema, type EmployeeFormValues, STATUS_OPTIONS, TYPE_OPTIONS, WAGE_TYPE_OPTIONS, } from "./employee.schema" import { EMPLOYEE_ROUTES, DEPARTMENT_ROUTES, SHOP_TIMING_ROUTES, SHOP_CALENDAR_ROUTES, GEO_ROUTES } from "@garage/api" // ── Constants ── const STATUS_SELECT_OPTIONS = STATUS_OPTIONS.map((v) => ({ value: v, label: v.replace(/_/g, " ").replace(/\b\w/g, (c) => c.toUpperCase()), })) const TYPE_SELECT_OPTIONS = TYPE_OPTIONS.map((v) => ({ value: v, label: v.replace(/_/g, " ").replace(/\b\w/g, (c) => c.toUpperCase()), })) const WAGE_TYPE_SELECT_OPTIONS = WAGE_TYPE_OPTIONS.map((v) => ({ value: v, label: v, })) // ── Props ── export type EmployeeFormProps = { resourceId?: string | null initialData?: unknown onSuccess?: () => void } // ── Default values ── const DEFAULT_VALUES: EmployeeFormValues = { department: null, country: null, shop_calender: null, shop_timing: null, role_id: null, first_name: "", last_name: "", email: "", password: "", phone: "", designation: "", salary: null, wage_type: null, status: "active", type: "employee", track_attendance: true, notify_owner_when_punch_in_out: false, geo_fence_radius: null, } // ── Mapping helpers ── function mapToFormValues(data: unknown): EmployeeFormValues { const d = (data as any)?.data ?? data ?? {} return { department: toRelation(d.department_id, d.department?.name), country: toRelation(d.country_id, d.country?.name), shop_calender: toRelation(d.shop_calender_id, d.shop_calender?.title), shop_timing: toRelation(d.shop_timing_id, d.shop_timing?.title), role_id: d.role_id ? Number(d.role_id) : null, first_name: d.first_name || "", last_name: d.last_name || "", email: d.email || "", password: "", phone: d.phone || "", designation: d.designation || "", salary: d.salary ?? null, wage_type: d.wage_type ?? null, status: d.status || "active", type: d.type || "employee", track_attendance: d.track_attendance ?? true, notify_owner_when_punch_in_out: d.notify_owner_when_punch_in_out ?? false, geo_fence_radius: d.geo_fence_radius ?? null, } } function mapFormToPayload(values: EmployeeFormValues) { return { department_id: toId(values.department), country_id: toId(values.country), shop_calender_id: toId(values.shop_calender), shop_timing_id: toId(values.shop_timing), role_id: values.role_id ?? undefined, first_name: values.first_name, last_name: values.last_name, email: values.email, password: values.password, phone: values.phone || undefined, designation: values.designation || undefined, salary: values.salary ?? undefined, wage_type: values.wage_type ?? undefined, status: values.status, type: values.type, track_attendance: values.track_attendance, notify_owner_when_punch_in_out: values.notify_owner_when_punch_in_out, geo_fence_radius: values.geo_fence_radius ?? undefined, } } // ── Shared mapOption for async selects ── const mapLookupOption = (item: any) => ({ value: String(item.id), label: item.name ?? item.title ?? String(item.id), }) const STORE_OBJECT = { getOptionValue: (o: any) => o, getOptionLabel: (o: any) => o.label } // ── Component ── export function EmployeeForm({ resourceId, initialData, onSuccess }: EmployeeFormProps) { const api = useAuthApi() const { form, isEditing } = useResourceForm({ schema: employeeFormSchema, defaultValues: DEFAULT_VALUES, resourceId, initialData, initialize: (id) => api.employees.show(id), queryKey: [EMPLOYEE_ROUTES.BY_ID, resourceId], mapToFormValues: mapToFormValues, }) const { mutate, error, isPending } = useFormMutation(form, { mutationFn: (values: EmployeeFormValues) => { const payload = mapFormToPayload(values) const promise = isEditing && resourceId ? api.employees.update(resourceId, payload) : api.employees.create(payload) toast.promise(promise, { loading: isEditing ? "Updating employee..." : "Creating employee...", success: isEditing ? "Employee updated successfully" : "Employee created successfully", error: isEditing ? "Failed to update employee" : "Failed to create employee", }) return promise }, onSuccess: () => { form.reset() onSuccess?.() }, }) return ( mutate(values)}> {error && ( {isEditing ? "Failed to update employee" : "Failed to create employee"} {error.message} )}
api.departments.list()} mapOption={mapLookupOption} {...STORE_OBJECT} />
api.geo.countries()} mapOption={mapLookupOption} {...STORE_OBJECT} /> api.shopCalendars.list()} mapOption={mapLookupOption} {...STORE_OBJECT} /> api.shopTimings.list()} mapOption={mapLookupOption} {...STORE_OBJECT} />
) }