fixed some ui styles
This commit is contained in:
@@ -1,31 +1,32 @@
|
||||
import { useCallback, useMemo, useEffect } 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 { Checkbox } from '@/components/ui/checkbox'
|
||||
import { FormActions } from '@/components/formActions'
|
||||
import { FormInput } from '@/components/formInput'
|
||||
import { MultiLangInput } from '@/components/multiLangInput'
|
||||
import type { District } from '../api/districtsApi'
|
||||
import { useCreateDistrict, useUpdateDistrict } from '../hooks/useDistricts'
|
||||
import { createDistrictFormSchema, DEFAULT_FORM_VALUES, buildPayload } from '../schemas/district.schema'
|
||||
import type { DistrictFormData } from '../schemas/district.schema'
|
||||
import { useCallback, useMemo, useEffect } 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 { Checkbox } from "@/components/ui/checkbox";
|
||||
import { FormActions } from "@/components/formActions";
|
||||
import { FormInput } from "@/components/formInput";
|
||||
import { MultiLangInput } from "@/components/multiLangInput";
|
||||
import { BentoGrid, BentoCard } from "@/components/bento";
|
||||
import type { District } from "../api/districtsApi";
|
||||
import { useCreateDistrict, useUpdateDistrict } from "../hooks/useDistricts";
|
||||
import { createDistrictFormSchema, DEFAULT_FORM_VALUES, buildPayload } from "../schemas/district.schema";
|
||||
import type { DistrictFormData } from "../schemas/district.schema";
|
||||
|
||||
type FlatErrors = Partial<Record<keyof DistrictFormData, string>>
|
||||
type FlatErrors = Partial<Record<keyof DistrictFormData, string>>;
|
||||
|
||||
function flattenErrors(errors: Record<string, { message?: string } | undefined>): FlatErrors {
|
||||
const result: FlatErrors = {}
|
||||
const result: FlatErrors = {};
|
||||
for (const key of Object.keys(errors)) {
|
||||
const msg = errors[key]?.message
|
||||
if (msg) result[key as keyof DistrictFormData] = msg
|
||||
const msg = errors[key]?.message;
|
||||
if (msg) result[key as keyof DistrictFormData] = msg;
|
||||
}
|
||||
return result
|
||||
return result;
|
||||
}
|
||||
|
||||
export interface DistrictFormProps {
|
||||
mode: 'create' | 'edit'
|
||||
initialData?: District
|
||||
mode: "create" | "edit";
|
||||
initialData?: District;
|
||||
}
|
||||
|
||||
function mapInitial(data: District): DistrictFormData {
|
||||
@@ -33,123 +34,113 @@ function mapInitial(data: District): DistrictFormData {
|
||||
nameTk: data.name.tk,
|
||||
nameRu: data.name.ru,
|
||||
nameEn: data.name.en,
|
||||
description: data.description ?? '',
|
||||
description: data.description ?? "",
|
||||
isActive: data.isActive,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export function DistrictForm({ mode, initialData }: DistrictFormProps) {
|
||||
const { t: _t, i18n } = useTranslation()
|
||||
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 navigate = useNavigate();
|
||||
|
||||
const createMutation = useCreateDistrict()
|
||||
const updateMutation = useUpdateDistrict()
|
||||
const createMutation = useCreateDistrict();
|
||||
const updateMutation = useUpdateDistrict();
|
||||
|
||||
const isPending = createMutation.isPending || updateMutation.isPending
|
||||
const isPending = createMutation.isPending || updateMutation.isPending;
|
||||
|
||||
const schema = useMemo(() => createDistrictFormSchema(t), [t, i18n.language])
|
||||
const schema = useMemo(() => createDistrictFormSchema(t), [t, i18n.language]);
|
||||
|
||||
const {
|
||||
watch, setValue, getValues, formState: { errors: rhfErrors }, clearErrors, trigger,
|
||||
watch,
|
||||
setValue,
|
||||
getValues,
|
||||
formState: { errors: rhfErrors },
|
||||
clearErrors,
|
||||
trigger,
|
||||
} = useForm<DistrictFormData>({
|
||||
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])
|
||||
const form = watch();
|
||||
const errors = useMemo(() => flattenErrors(rhfErrors as Record<string, { message?: string } | undefined>), [rhfErrors]);
|
||||
|
||||
const set = useCallback(<K extends keyof DistrictFormData>(key: K) =>
|
||||
(value: DistrictFormData[K]) => {
|
||||
(setValue as (name: K, val: DistrictFormData[K]) => void)(key, value)
|
||||
clearErrors(key)
|
||||
}, [setValue, clearErrors])
|
||||
const set = useCallback(
|
||||
<K extends keyof DistrictFormData>(key: K) =>
|
||||
(value: DistrictFormData[K]) => {
|
||||
(setValue as (name: K, val: DistrictFormData[K]) => void)(key, value);
|
||||
clearErrors(key);
|
||||
},
|
||||
[setValue, clearErrors],
|
||||
);
|
||||
|
||||
// ── Re-validate on language change ──
|
||||
useEffect(() => {
|
||||
if (Object.keys(rhfErrors).length > 0) trigger()
|
||||
if (Object.keys(rhfErrors).length > 0) trigger();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [i18n.language])
|
||||
}, [i18n.language]);
|
||||
|
||||
const handleSubmit = async () => {
|
||||
const isValid = await trigger()
|
||||
if (!isValid) return
|
||||
const isValid = await trigger();
|
||||
if (!isValid) return;
|
||||
|
||||
const payload = buildPayload(getValues())
|
||||
const payload = buildPayload(getValues());
|
||||
|
||||
if (mode === 'create') {
|
||||
if (mode === "create") {
|
||||
createMutation.mutate(payload, {
|
||||
onSuccess: () => navigate('/settings/location/districts'),
|
||||
})
|
||||
onSuccess: () => navigate("/settings/location/districts"),
|
||||
});
|
||||
} else if (initialData) {
|
||||
updateMutation.mutate(
|
||||
{ id: initialData.id, ...payload },
|
||||
{ onSuccess: () => navigate('/settings/location/districts') },
|
||||
)
|
||||
updateMutation.mutate({ id: initialData.id, ...payload }, { onSuccess: () => navigate("/settings/location/districts") });
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const nameFields = {
|
||||
tk: { value: form.nameTk, onChange: set('nameTk'), error: errors.nameTk },
|
||||
ru: { value: form.nameRu, onChange: set('nameRu'), error: errors.nameRu },
|
||||
en: { value: form.nameEn, onChange: set('nameEn'), error: errors.nameEn },
|
||||
}
|
||||
tk: { value: form.nameTk, onChange: set("nameTk"), error: errors.nameTk },
|
||||
ru: { value: form.nameRu, onChange: set("nameRu"), error: errors.nameRu },
|
||||
en: { value: form.nameEn, onChange: set("nameEn"), error: errors.nameEn },
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-5">
|
||||
<h1 className="text-xl font-semibold text-foreground">
|
||||
{mode === 'create'
|
||||
? t('districts.createTitle', 'Etrap dörediň')
|
||||
: t('districts.editTitle', 'Etrap üýtgetmek')}
|
||||
{mode === "create" ? t("districts.createTitle", "Etrap dörediň") : t("districts.editTitle", "Etrap üýtgetmek")}
|
||||
</h1>
|
||||
<div>
|
||||
<div className="bg-card border border-border rounded-xl overflow-hidden">
|
||||
<div className="grid grid-cols-[220px_1fr] items-start px-4 py-4 border-b border-border">
|
||||
<span className="text-sm text-muted-foreground pt-2">
|
||||
{t('districts.fields.name', 'Ady')}
|
||||
<BentoGrid cols={2}>
|
||||
<BentoCard span="full">
|
||||
<p className="text-sm font-semibold text-muted-foreground mb-1">
|
||||
{t("districts.fields.name", "Ady")}
|
||||
<span className="text-destructive ml-0.5">*</span>
|
||||
</span>
|
||||
<MultiLangInput fields={nameFields} placeholder={t('districts.fields.name', 'Ady')} />
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-[220px_1fr] items-center py-3 px-4 border-b border-border">
|
||||
<span className="text-sm text-muted-foreground">
|
||||
{t('districts.fields.description', 'Bellikler')}
|
||||
</span>
|
||||
</p>
|
||||
<MultiLangInput fields={nameFields} placeholder={t("districts.fields.name", "Ady")} />
|
||||
</BentoCard>
|
||||
<BentoCard>
|
||||
<FormInput
|
||||
type="text"
|
||||
label={t("districts.fields.description", "Bellikler")}
|
||||
value={form.description}
|
||||
onChange={set('description')}
|
||||
placeholder={t('districts.fields.description', 'Bellikler')}
|
||||
onChange={set("description")}
|
||||
placeholder={t("districts.fields.description", "Bellikler")}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-[220px_1fr] items-center py-3 px-4">
|
||||
<span className="text-sm text-muted-foreground">
|
||||
{t('districts.fields.isActive', 'Işjeň')}
|
||||
</span>
|
||||
<Checkbox
|
||||
checked={form.isActive}
|
||||
onCheckedChange={(checked) => set('isActive')(!!checked)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</BentoCard>
|
||||
<BentoCard>
|
||||
<div className="flex items-center gap-3 pt-1">
|
||||
<span className="text-sm font-semibold text-muted-foreground">{t("districts.fields.isActive", "Işjeň")}</span>
|
||||
<Checkbox checked={form.isActive} onCheckedChange={(checked) => set("isActive")(!!checked)} />
|
||||
</div>
|
||||
</BentoCard>
|
||||
</BentoGrid>
|
||||
<FormActions
|
||||
isPending={isPending}
|
||||
onSubmit={handleSubmit}
|
||||
onCancel={() => navigate('/settings/location/districts')}
|
||||
onCancel={() => navigate("/settings/location/districts")}
|
||||
cancelVariant="ghost"
|
||||
submitLabel={mode === 'create'
|
||||
? t('districts.actions.create', 'Etrap döret')
|
||||
: t('districts.actions.update', 'Täzelemek')}
|
||||
className="mt-6"
|
||||
submitLabel={mode === "create" ? t("districts.actions.create", "Etrap döret") : t("districts.actions.update", "Täzelemek")}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user