added lang support to create pages
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { useState, useCallback, useMemo } from 'react'
|
||||
import { useState, useCallback, useMemo, useEffect } from 'react'
|
||||
import { useNavigate } from 'react-router-dom'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { useForm } from 'react-hook-form'
|
||||
@@ -9,7 +9,7 @@ import { FormActions } from '@/components/formActions'
|
||||
import { Checkbox } from '@/components/ui/checkbox'
|
||||
import { useCreateLoanType, useUpdateLoanType } from '../hooks/useLoanTypes'
|
||||
import type { LoanType } from '../api/loanTypesApi'
|
||||
import { loanTypeFormSchema, DEFAULT_FORM_VALUES, buildPayload } from '../schemas/loanType.schema'
|
||||
import { createLoanTypeFormSchema, DEFAULT_FORM_VALUES, buildPayload } from '../schemas/loanType.schema'
|
||||
import type { LoanTypeFormData } from '../schemas/loanType.schema'
|
||||
|
||||
// ─── Types ────────────────────────────────────────────────────────────────────
|
||||
@@ -60,7 +60,11 @@ const LANG_TABS: { key: Lang; label: string }[] = [
|
||||
// ─── LoanTypeForm ─────────────────────────────────────────────────────────────
|
||||
|
||||
export function LoanTypeForm({ mode, initialData, loanTypeId }: LoanTypeFormProps) {
|
||||
const { t } = useTranslation()
|
||||
const { t: _t, i18n } = useTranslation()
|
||||
const t: (key: string, fallback?: string) => string = useCallback(
|
||||
(key, fallback) => _t(key, fallback ?? key) as string,
|
||||
[_t],
|
||||
)
|
||||
const navigate = useNavigate()
|
||||
|
||||
const createMutation = useCreateLoanType()
|
||||
@@ -70,6 +74,8 @@ export function LoanTypeForm({ mode, initialData, loanTypeId }: LoanTypeFormProp
|
||||
|
||||
const [activeLang, setActiveLang] = useState<Lang>('tk')
|
||||
|
||||
const schema = useMemo(() => createLoanTypeFormSchema(t), [t, i18n.language])
|
||||
|
||||
const {
|
||||
watch,
|
||||
setValue,
|
||||
@@ -78,7 +84,7 @@ export function LoanTypeForm({ mode, initialData, loanTypeId }: LoanTypeFormProp
|
||||
trigger,
|
||||
getValues,
|
||||
} = useForm<LoanTypeFormData>({
|
||||
resolver: zodResolver(loanTypeFormSchema),
|
||||
resolver: zodResolver(schema),
|
||||
defaultValues: initialData
|
||||
? { ...DEFAULT_FORM_VALUES, ...mapInitial(initialData) }
|
||||
: DEFAULT_FORM_VALUES,
|
||||
@@ -90,6 +96,12 @@ export function LoanTypeForm({ mode, initialData, loanTypeId }: LoanTypeFormProp
|
||||
[rhfErrors],
|
||||
)
|
||||
|
||||
// ── Re-validate on language change ──
|
||||
useEffect(() => {
|
||||
if (Object.keys(rhfErrors).length > 0) trigger()
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [i18n.language])
|
||||
|
||||
const set = useCallback(
|
||||
(key: keyof LoanTypeFormData) => (value: string | boolean) => {
|
||||
(setValue as (k: keyof LoanTypeFormData, v: string | boolean) => void)(key, value)
|
||||
|
||||
@@ -1,28 +1,27 @@
|
||||
import { z } from 'zod'
|
||||
import i18next from 'i18next'
|
||||
import type { CreateLoanTypePayload } from '../api/loanTypesApi'
|
||||
|
||||
const t = i18next.t.bind(i18next)
|
||||
export function createLoanTypeFormSchema(t: (key: string, fallback?: string) => string) {
|
||||
return z.object({
|
||||
nameTk: z.string().min(1, t('validation.required', 'validation.required')),
|
||||
nameRu: z.string().min(1, t('validation.required', 'validation.required')),
|
||||
nameEn: z.string().min(1, t('validation.required', 'validation.required')),
|
||||
notesTk: z.string().optional(),
|
||||
notesRu: z.string().optional(),
|
||||
notesEn: z.string().optional(),
|
||||
tax: z.string().refine(
|
||||
(v) => v.trim() !== '' && !isNaN(Number(v)) && Number(v) > 0,
|
||||
t('validation.positiveNumber', 'validation.positiveNumber'),
|
||||
),
|
||||
loanTerm: z.string().refine(
|
||||
(v) => v.trim() !== '' && !isNaN(Number(v)) && Number(v) > 0,
|
||||
t('validation.positiveNumber', 'validation.positiveNumber'),
|
||||
),
|
||||
isActive: z.boolean(),
|
||||
})
|
||||
}
|
||||
|
||||
export const loanTypeFormSchema = z.object({
|
||||
nameTk: z.string().min(1, t('validation.required', '')),
|
||||
nameRu: z.string().min(1, t('validation.required', '')),
|
||||
nameEn: z.string().min(1, t('validation.required', '')),
|
||||
notesTk: z.string().optional(),
|
||||
notesRu: z.string().optional(),
|
||||
notesEn: z.string().optional(),
|
||||
tax: z.string().refine(
|
||||
(v) => v.trim() !== '' && !isNaN(Number(v)) && Number(v) > 0,
|
||||
t('validation.positiveNumber', 'Oňyn san gerek'),
|
||||
),
|
||||
loanTerm: z.string().refine(
|
||||
(v) => v.trim() !== '' && !isNaN(Number(v)) && Number(v) > 0,
|
||||
t('validation.positiveNumber', 'Oňyn san gerek'),
|
||||
),
|
||||
isActive: z.boolean(),
|
||||
})
|
||||
|
||||
export type LoanTypeFormData = z.infer<typeof loanTypeFormSchema>
|
||||
export type LoanTypeFormData = z.infer<ReturnType<typeof createLoanTypeFormSchema>>
|
||||
|
||||
export const DEFAULT_FORM_VALUES: LoanTypeFormData = {
|
||||
nameTk: '',
|
||||
|
||||
Reference in New Issue
Block a user