added lang support to create pages
This commit is contained in:
1 parent
35164d16d3
commit
a0ed9a702c
34 files changed
+1456
-639
No files matched your search
@@ -1,6 +1,6 @@
|
||||
// features/currencyRates/components/CurrencyRateForm.tsx
|
||||
|
||||
import { useCallback, useMemo } from 'react'
|
||||
import { useCallback, useMemo, useEffect } from 'react'
|
||||
import { useNavigate } from 'react-router-dom'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { useForm } from 'react-hook-form'
|
||||
@@ -10,7 +10,7 @@ import { FormActions } from '@/components/formActions'
|
||||
import { CURRENCY_OPTIONS, type CurrencyRate } from '../api/currencyRatesApi'
|
||||
import { useCreateCurrencyRate, useUpdateCurrencyRate } from '../hooks/useCurrencyRates'
|
||||
import {
|
||||
currencyRateFormSchema,
|
||||
createCurrencyRateFormSchema,
|
||||
DEFAULT_FORM_VALUES,
|
||||
buildPayload,
|
||||
type CurrencyRateFormData,
|
||||
@@ -38,7 +38,11 @@ function flattenErrors(errors: Record<string, { message?: string } | undefined>)
|
||||
// ─── CurrencyRateForm ─────────────────────────────────────────────────────────
|
||||
|
||||
export function CurrencyRateForm({ mode, initialData, rateId }: CurrencyRateFormProps) {
|
||||
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 = useCreateCurrencyRate()
|
||||
@@ -46,6 +50,8 @@ export function CurrencyRateForm({ mode, initialData, rateId }: CurrencyRateForm
|
||||
|
||||
const isPending = createMutation.isPending || updateMutation.isPending
|
||||
|
||||
const schema = useMemo(() => createCurrencyRateFormSchema(t), [t, i18n.language])
|
||||
|
||||
const {
|
||||
watch,
|
||||
setValue,
|
||||
@@ -54,7 +60,7 @@ export function CurrencyRateForm({ mode, initialData, rateId }: CurrencyRateForm
|
||||
trigger,
|
||||
getValues,
|
||||
} = useForm<CurrencyRateFormData>({
|
||||
resolver: zodResolver(currencyRateFormSchema),
|
||||
resolver: zodResolver(schema),
|
||||
defaultValues: initialData
|
||||
? {
|
||||
currencyFrom: initialData.currencyFrom,
|
||||
@@ -70,6 +76,14 @@ export function CurrencyRateForm({ mode, initialData, rateId }: CurrencyRateForm
|
||||
[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 setField = useCallback(
|
||||
(key: keyof CurrencyRateFormData) => (value: string) => {
|
||||
setValue(key, value)
|
||||
|
||||
@@ -1,19 +1,18 @@
|
||||
import { z } from 'zod'
|
||||
import i18next from 'i18next'
|
||||
import type { CreateCurrencyRatePayload, CurrencyCode } from '../api/currencyRatesApi'
|
||||
|
||||
const t = i18next.t.bind(i18next)
|
||||
export function createCurrencyRateFormSchema(t: (key: string, fallback?: string) => string) {
|
||||
return z.object({
|
||||
currencyFrom: z.string().min(1, t('validation.required', 'validation.required')),
|
||||
currencyTo: z.string().min(1, t('validation.required', 'validation.required')),
|
||||
value: z
|
||||
.string()
|
||||
.min(1, t('validation.required', 'validation.required'))
|
||||
.refine((v) => !isNaN(Number(v)) && Number(v) > 0, t('validation.valuePositive', 'validation.valuePositive')),
|
||||
})
|
||||
}
|
||||
|
||||
export const currencyRateFormSchema = z.object({
|
||||
currencyFrom: z.string().min(1, t('validation.required', 'Hökmany meýdan')),
|
||||
currencyTo: z.string().min(1, t('validation.required', 'Hökmany meýdan')),
|
||||
value: z
|
||||
.string()
|
||||
.min(1, t('validation.required', 'Hökmany meýdan'))
|
||||
.refine((v) => !isNaN(Number(v)) && Number(v) > 0, t('validation.valuePositive', 'Baha 0-dan uly bolmaly')),
|
||||
})
|
||||
|
||||
export type CurrencyRateFormData = z.infer<typeof currencyRateFormSchema>
|
||||
export type CurrencyRateFormData = z.infer<ReturnType<typeof createCurrencyRateFormSchema>>
|
||||
|
||||
export const DEFAULT_FORM_VALUES: CurrencyRateFormData = {
|
||||
currencyFrom: '',
|
||||
|
||||
Reference in new issue
Block a user