import { useCallback, useEffect, useMemo } from "react"; import { useNavigate } from "react-router-dom"; import { useTranslation } from "react-i18next"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { zodResolver } from "@hookform/resolvers/zod"; import { FormInput } from "@/components/formInput"; import { FormActions } from "@/components/formActions"; import { BentoGrid, BentoCard } from "@/components/bento"; import type { VisaMasterSetting } from "../api/visaMasterSberSettingsApi"; import { useCreateVisaMasterSetting, useUpdateVisaMasterSetting } from "../hooks/useVisaMasterSettings"; import { createVisaMasterSettingFormSchema, DEFAULT_FORM_VALUES, buildCreatePayload, buildUpdatePayload, } from "../schemas/visaMasterSberSetting.schema"; import type { VisaMasterSettingFormData } from "../schemas/visaMasterSberSetting.schema"; const LIST_PATH = "/visa-master-sber-settings"; export interface VisaMasterSettingFormProps { mode: "create" | "edit"; initialData?: VisaMasterSetting; } function mapInitial(data: VisaMasterSetting): VisaMasterSettingFormData { return { code: data.code, name: data.name, isActive: data.isActive, textTm: data.text?.tm ?? "", textRu: data.text?.ru ?? "", textEn: data.text?.en ?? "", }; } type FlatErrors = Partial>; function flattenErrors(errors: Record): FlatErrors { const result: FlatErrors = {}; for (const key of Object.keys(errors)) { const msg = errors[key]?.message; if (msg) result[key as keyof VisaMasterSettingFormData] = msg; } return result; } export function VisaMasterSettingForm({ mode, initialData }: VisaMasterSettingFormProps) { const { t: _t, i18n } = useTranslation(); const t: (key: string, fallback?: string) => string = useCallback( (key, fallback) => _t(key, fallback ?? key) as string, [_t, i18n.language], ); const errMsg = (msg: string | undefined) => (!msg ? undefined : msg.startsWith("validation.") ? t(msg, msg) : msg); const navigate = useNavigate(); const createMutation = useCreateVisaMasterSetting(); const updateMutation = useUpdateVisaMasterSetting(initialData?.code ?? ""); const isPending = createMutation.isPending || updateMutation.isPending; const schema = useMemo( () => createVisaMasterSettingFormSchema(t), // eslint-disable-next-line react-hooks/exhaustive-deps [i18n.language], ); const { watch, setValue, formState: { errors: rhfErrors }, clearErrors, trigger, getValues, } = useForm({ resolver: zodResolver(schema), defaultValues: initialData ? { ...DEFAULT_FORM_VALUES, ...mapInitial(initialData) } : DEFAULT_FORM_VALUES, }); const form = watch(); const errors = useMemo(() => flattenErrors(rhfErrors as Record), [rhfErrors]); useEffect(() => { if (Object.keys(rhfErrors).length > 0) trigger(); // eslint-disable-next-line react-hooks/exhaustive-deps }, [i18n.language]); const set = useCallback( (key: Exclude) => (value: string) => { setValue(key, value); clearErrors(key); }, [setValue, clearErrors], ); const handleSubmit = useCallback(async () => { const isValid = await trigger(); if (!isValid) { toast.error(t("common.errors.fillRequiredCorrectly", "Dogry maglumat girizmegiňizi haýyş edýäris.")); return; } const values = getValues(); try { if (mode === "create") { await createMutation.mutateAsync(buildCreatePayload(values)); } else { await updateMutation.mutateAsync(buildUpdatePayload(values)); } navigate(LIST_PATH); } catch { // error toast is handled in the mutation hooks } }, [mode, createMutation, updateMutation, navigate, trigger, getValues, t]); const handleCancel = () => navigate(LIST_PATH); return (

{mode === "create" ? t("visaMasterSettings.createTitle", "Visa/Master, Sber sazlamalar dörediň") : t("visaMasterSettings.editTitle", "Visa/Master, Sber sazlamalar üýtgetmek")}

); }