159 lines
5.7 KiB
TypeScript
159 lines
5.7 KiB
TypeScript
import { useMemo, useEffect, useCallback } from "react";
|
|
import { useNavigate } from "react-router-dom";
|
|
import { useTranslation } from "react-i18next";
|
|
import { useForm } from "react-hook-form";
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
import { FormActions } from "@/components/formActions";
|
|
import { BentoGrid, BentoCard } from "@/components/bento";
|
|
import { useCreatePermission, useUpdatePermission } from "../hooks/usePermissions";
|
|
import { createPermissionFormSchema, DEFAULT_FORM_VALUES, buildPayload } from "../schemas/permission.schema";
|
|
import type { PermissionFormData } from "../schemas/permission.schema";
|
|
import type { Permission } from "../api/permissionsApi";
|
|
import { FormInput } from "@/components/formInput";
|
|
|
|
// ─── Types ────────────────────────────────────────────────────────────────────
|
|
|
|
interface PermissionFormProps {
|
|
mode: "create" | "edit";
|
|
initialData?: Permission;
|
|
}
|
|
|
|
type FlatErrors = Partial<Record<keyof PermissionFormData, 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 PermissionFormData] = msg;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
// ─── PermissionForm ───────────────────────────────────────────────────────────
|
|
|
|
export function PermissionForm({ mode, initialData }: PermissionFormProps) {
|
|
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 navigate = useNavigate();
|
|
|
|
const createPermission = useCreatePermission();
|
|
const updatePermission = useUpdatePermission(initialData?.id ?? 0);
|
|
|
|
const isPending = createPermission.isPending || updatePermission.isPending;
|
|
|
|
const schema = useMemo(() => createPermissionFormSchema(t), [t, i18n.language]);
|
|
|
|
const {
|
|
watch,
|
|
setValue,
|
|
getValues,
|
|
formState: { errors: rhfErrors },
|
|
clearErrors,
|
|
trigger,
|
|
} = useForm<PermissionFormData>({
|
|
resolver: zodResolver(schema),
|
|
defaultValues: initialData
|
|
? {
|
|
...DEFAULT_FORM_VALUES,
|
|
code: initialData.code,
|
|
name: initialData.name,
|
|
guardName: initialData.guardName,
|
|
}
|
|
: DEFAULT_FORM_VALUES,
|
|
});
|
|
|
|
const form = watch();
|
|
const errors = useMemo(() => flattenErrors(rhfErrors as Record<string, { message?: string } | undefined>), [rhfErrors]);
|
|
|
|
const setField = useCallback(
|
|
(key: keyof PermissionFormData) => (value: string) => {
|
|
(setValue as (k: keyof PermissionFormData, v: string) => void)(key, value);
|
|
clearErrors(key);
|
|
},
|
|
[setValue, clearErrors],
|
|
);
|
|
|
|
// ── Re-validate on language change ──
|
|
useEffect(() => {
|
|
if (Object.keys(rhfErrors).length > 0) trigger();
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [i18n.language]);
|
|
|
|
// ── Submit ─────────────────────────────────────────────────────────────────
|
|
|
|
const handleSubmit = async () => {
|
|
const isValid = await trigger();
|
|
if (!isValid) return;
|
|
|
|
const values = getValues();
|
|
const payload = buildPayload(values);
|
|
|
|
if (mode === "create") {
|
|
createPermission.mutate(payload, {
|
|
onSuccess: () => navigate("/settings/users/permissions"),
|
|
});
|
|
} else {
|
|
updatePermission.mutate(payload, {
|
|
onSuccess: () => navigate("/settings/users/permissions"),
|
|
});
|
|
}
|
|
};
|
|
|
|
// ── Render ─────────────────────────────────────────────────────────────────
|
|
|
|
return (
|
|
<div className="flex flex-col gap-5">
|
|
<h1 className="text-xl font-semibold text-foreground">
|
|
{mode === "create" ? t("permissions.createTitle", "Rugsat dörediň") : t("permissions.editTitle", "Rugsat üýtgetmek")}
|
|
</h1>
|
|
<BentoGrid cols={2}>
|
|
<BentoCard>
|
|
<FormInput
|
|
type="text"
|
|
label={t("permissions.fields.code", "Kod")}
|
|
value={form.code}
|
|
onChange={setField("code")}
|
|
placeholder={t("permissions.fields.code", "Kod")}
|
|
error={errors.code}
|
|
disabled={isPending}
|
|
required
|
|
/>
|
|
</BentoCard>
|
|
<BentoCard>
|
|
<FormInput
|
|
type="text"
|
|
label={t("permissions.fields.guardName", "Guard name")}
|
|
value={form.guardName}
|
|
onChange={setField("guardName")}
|
|
placeholder="operator"
|
|
error={errors.guardName}
|
|
disabled={isPending}
|
|
required
|
|
/>
|
|
</BentoCard>
|
|
<BentoCard span="full">
|
|
<FormInput
|
|
type="text"
|
|
label={t("permissions.fields.name", "Ady")}
|
|
value={form.name}
|
|
onChange={setField("name")}
|
|
placeholder={t("permissions.fields.name", "Ady")}
|
|
error={errors.name}
|
|
disabled={isPending}
|
|
required
|
|
/>
|
|
</BentoCard>
|
|
</BentoGrid>
|
|
<FormActions
|
|
isPending={isPending}
|
|
onSubmit={handleSubmit}
|
|
onCancel={() => navigate("/settings/users/permissions")}
|
|
submitLabel={mode === "create" ? t("permissions.actions.create", "Rugsat dörediň") : t("permissions.actions.save", "Ýatda sakla")}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|