219 lines
7.2 KiB
TypeScript
219 lines
7.2 KiB
TypeScript
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<Record<keyof VisaMasterSettingFormData, string>>;
|
|
|
|
function flattenErrors(errors: Record<string, { message?: string } | undefined>): 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<VisaMasterSettingFormData>({
|
|
resolver: zodResolver(schema),
|
|
defaultValues: initialData ? { ...DEFAULT_FORM_VALUES, ...mapInitial(initialData) } : DEFAULT_FORM_VALUES,
|
|
});
|
|
|
|
const form = watch();
|
|
const errors = useMemo(() => flattenErrors(rhfErrors as Record<string, { message?: string } | undefined>), [rhfErrors]);
|
|
|
|
useEffect(() => {
|
|
if (Object.keys(rhfErrors).length > 0) trigger();
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [i18n.language]);
|
|
|
|
const set = useCallback(
|
|
(key: Exclude<keyof VisaMasterSettingFormData, "isActive">) => (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 (
|
|
<div className="flex flex-col gap-5">
|
|
<h1 className="text-xl font-semibold text-foreground">
|
|
{mode === "create"
|
|
? t("visaMasterSettings.createTitle", "Visa/Master, Sber sazlamalar dörediň")
|
|
: t("visaMasterSettings.editTitle", "Visa/Master, Sber sazlamalar üýtgetmek")}
|
|
</h1>
|
|
|
|
<BentoGrid cols={2}>
|
|
<BentoCard>
|
|
<FormInput
|
|
type="text"
|
|
label={t("visaMasterSettings.fields.kod", "Kod")}
|
|
value={form.code}
|
|
onChange={set("code")}
|
|
placeholder={t("visaMasterSettings.fields.kod", "Kod")}
|
|
error={errMsg(errors.code)}
|
|
disabled={isPending || mode === "edit"}
|
|
required
|
|
/>
|
|
</BentoCard>
|
|
<BentoCard>
|
|
<FormInput
|
|
type="text"
|
|
label={t("visaMasterSettings.fields.ady", "Ady")}
|
|
value={form.name}
|
|
onChange={set("name")}
|
|
placeholder={t("visaMasterSettings.fields.ady", "Ady")}
|
|
error={errMsg(errors.name)}
|
|
disabled={isPending}
|
|
required
|
|
/>
|
|
</BentoCard>
|
|
</BentoGrid>
|
|
|
|
<BentoGrid cols={1}>
|
|
<BentoCard>
|
|
<FormInput
|
|
type="textarea"
|
|
label={t("visaMasterSettings.fields.textTm", "Tekst (TM)")}
|
|
value={form.textTm}
|
|
onChange={set("textTm")}
|
|
placeholder={t("visaMasterSettings.fields.textTm", "Tekst (TM)")}
|
|
error={errMsg(errors.textTm)}
|
|
disabled={isPending}
|
|
rows={3}
|
|
required
|
|
/>
|
|
</BentoCard>
|
|
<BentoCard>
|
|
<FormInput
|
|
type="textarea"
|
|
label={t("visaMasterSettings.fields.textRu", "Tekst (RU)")}
|
|
value={form.textRu}
|
|
onChange={set("textRu")}
|
|
placeholder={t("visaMasterSettings.fields.textRu", "Tekst (RU)")}
|
|
error={errMsg(errors.textRu)}
|
|
disabled={isPending}
|
|
rows={3}
|
|
/>
|
|
</BentoCard>
|
|
<BentoCard>
|
|
<FormInput
|
|
type="textarea"
|
|
label={t("visaMasterSettings.fields.textEn", "Tekst (EN)")}
|
|
value={form.textEn}
|
|
onChange={set("textEn")}
|
|
placeholder={t("visaMasterSettings.fields.textEn", "Tekst (EN)")}
|
|
error={errMsg(errors.textEn)}
|
|
disabled={isPending}
|
|
rows={3}
|
|
/>
|
|
</BentoCard>
|
|
<BentoCard>
|
|
<label className="flex items-center gap-2 text-sm text-foreground cursor-pointer">
|
|
<input
|
|
type="checkbox"
|
|
className="h-4 w-4"
|
|
checked={form.isActive}
|
|
disabled={isPending}
|
|
onChange={(e) => setValue("isActive", e.target.checked)}
|
|
/>
|
|
{t("visaMasterSettings.fields.isActive", "Aktiw")}
|
|
</label>
|
|
<FormActions
|
|
isPending={isPending}
|
|
onSubmit={handleSubmit}
|
|
onCancel={handleCancel}
|
|
submitLabel={
|
|
mode === "create"
|
|
? t("visaMasterSettings.actions.create", "Visa/Master, Sber sazlamalar dörediň")
|
|
: t("visaMasterSettings.actions.update", "Ýatda sakla")
|
|
}
|
|
/>
|
|
</BentoCard>
|
|
</BentoGrid>
|
|
</div>
|
|
);
|
|
}
|