"use client" import { AlertTriangle, Save } from "lucide-react" import { useEffect } from "react" import { useForm } from "react-hook-form" import { zodResolver } from "@hookform/resolvers/zod" import { useQuery } from "@tanstack/react-query" 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, RhfTextField, RhfSelectField, RhfAsyncSelectField, RhfTextareaField, } from "@/shared/components/form" import { useAuthApi } from "@/shared/useApi" import { useFormMutation } from "@/shared/hooks/use-form-mutation" import { toId } from "@/shared/lib/utils" import { FirstDayOfWork } from "@garage/api" import { SETTINGS_ROUTES } from "@garage/api" import { settingsFormSchema, type SettingsFormValues } from "./settings.schema" // ── Constants ── const FIRST_DAY_OPTIONS = FirstDayOfWork.map((d) => ({ value: d, label: d.charAt(0).toUpperCase() + d.slice(1), })) const mapLookupOption = (item: any) => ({ value: String(item.id), label: item.name }) const STORE_OBJECT = { getOptionValue: (o: any) => o, getOptionLabel: (o: any) => o.label } // ── Default values ── const DEFAULT_VALUES: SettingsFormValues = { name: "", email: "", phone: "", alternative_phone: "", website: "", time_zone: "", upi_id: "", first_day_of_work: "", latitude: "", longitude: "", bank_details: "", first_address_line: "", second_address_line: "", country: null, state: null, city: "", zip_code: "", description: "", security: "", privacy_policy: "", } // ── Mapping helpers ── function mapToFormValues(data: unknown): SettingsFormValues { const d = (data as any)?.data ?? data ?? {} return { name: d.name ?? "", email: d.email ?? "", phone: d.phone ?? "", alternative_phone: d.alternative_phone ?? "", website: d.website ?? "", time_zone: d.time_zone ?? "", upi_id: d.upi_id ?? "", first_day_of_work: d.first_day_of_work ?? "", latitude: d.latitude ?? "", longitude: d.longitude ?? "", bank_details: d.bank_details ?? "", first_address_line: d.first_address_line ?? "", second_address_line: d.second_address_line ?? "", country: null, state: null, city: d.city ?? "", zip_code: d.zip_code ?? "", description: d.description ?? "", security: d.security ?? "", privacy_policy: d.privacy_policy ?? "", } } function mapFormToPayload(values: SettingsFormValues) { return { name: values.name, email: values.email || undefined, phone: values.phone || undefined, time_zone: values.time_zone || undefined, first_day_of_work: values.first_day_of_work || undefined, first_address_line: values.first_address_line || undefined, country_id: toId(values.country), city: values.city || undefined, } } // ── Component ── export function SettingsForm() { const api = useAuthApi() const form = useForm({ resolver: zodResolver(settingsFormSchema) as any, defaultValues: DEFAULT_VALUES, }) const { data, isLoading } = useQuery({ queryKey: [SETTINGS_ROUTES.INDEX], queryFn: () => api.settings.fetch(), }) useEffect(() => { if (!data) return const raw = (data as any)?.data const record = Array.isArray(raw) ? raw[0] : raw if (record) { form.reset(mapToFormValues(record)) } }, [data]) // eslint-disable-line react-hooks/exhaustive-deps const { mutate, error, isPending } = useFormMutation(form, { mutationFn: (values: SettingsFormValues) => { const payload = mapFormToPayload(values) const promise = api.settings.update(payload) toast.promise(promise, { loading: "Saving settings...", success: "Settings saved successfully", error: "Failed to save settings", }) return promise }, }) return ( mutate(values)}> {error && ( Failed to save settings {error.message} )}
{/* Main Content - 8/12 */}
{/* General Information Section */}

General Information

{/* Address Section */}

Address

api.geo.countries()} mapOption={mapLookupOption} {...STORE_OBJECT} /> api.geo.states()} mapOption={mapLookupOption} {...STORE_OBJECT} />
{/* More Information Section */}

More Information

{/* Sidebar - 4/12 */}
{/* Location & Time Section */}

Location & Time

{/* Policies Section */}

Policies

{/* Action Button */}
) }