added zod validation
This commit is contained in:
1 parent
dfb8e0b322
commit
1f79766192
58 files changed
+3605
-2798
No files matched your search
@@ -1,132 +1,132 @@
|
||||
import { apiClient } from '@/lib/api/client'
|
||||
import apiClient from "@/lib/api/client";
|
||||
|
||||
// ─── Types ────────────────────────────────────────────────────────────────────
|
||||
|
||||
export type CardPinStatus = 'pending' | 'approved' | 'rejected'
|
||||
export type CardPinStatus = "pending" | "approved" | "rejected";
|
||||
|
||||
export interface CardPinItem {
|
||||
id: string
|
||||
card_type: string
|
||||
card_type_label: string
|
||||
card_number: string
|
||||
created_at: string
|
||||
province: string
|
||||
province_label: string
|
||||
branch: string
|
||||
branch_label: string
|
||||
first_name: string
|
||||
last_name: string
|
||||
father_name: string
|
||||
birth_date: string
|
||||
phone: string
|
||||
status: CardPinStatus
|
||||
note: string | null
|
||||
passport_series: string
|
||||
passport_number: string
|
||||
passport_file_1: string | null
|
||||
passport_file_2: string | null
|
||||
passport_file_3: string | null
|
||||
passport_file_4: string | null
|
||||
created_by: string
|
||||
created_by_label: string
|
||||
id: string;
|
||||
card_type: string;
|
||||
card_type_label: string;
|
||||
card_number: string;
|
||||
created_at: string;
|
||||
province: string;
|
||||
province_label: string;
|
||||
branch: string;
|
||||
branch_label: string;
|
||||
first_name: string;
|
||||
last_name: string;
|
||||
father_name: string;
|
||||
birth_date: string;
|
||||
phone: string;
|
||||
status: CardPinStatus;
|
||||
note: string | null;
|
||||
passport_series: string;
|
||||
passport_number: string;
|
||||
passport_file_1: string | null;
|
||||
passport_file_2: string | null;
|
||||
passport_file_3: string | null;
|
||||
passport_file_4: string | null;
|
||||
created_by: string;
|
||||
created_by_label: string;
|
||||
}
|
||||
|
||||
export interface CardPinListParams {
|
||||
page?: number
|
||||
per_page?: number
|
||||
search?: string
|
||||
status?: CardPinStatus | ''
|
||||
province?: string
|
||||
page?: number;
|
||||
per_page?: number;
|
||||
search?: string;
|
||||
status?: CardPinStatus | "";
|
||||
province?: string;
|
||||
}
|
||||
|
||||
export interface CardPinListResponse {
|
||||
data: CardPinItem[]
|
||||
data: CardPinItem[];
|
||||
meta: {
|
||||
current_page: number
|
||||
last_page: number
|
||||
per_page: number
|
||||
total: number
|
||||
}
|
||||
current_page: number;
|
||||
last_page: number;
|
||||
per_page: number;
|
||||
total: number;
|
||||
};
|
||||
}
|
||||
|
||||
export interface CardPinCreatePayload {
|
||||
status: CardPinStatus
|
||||
note: string
|
||||
card_type: string
|
||||
card_number: string
|
||||
province: string
|
||||
branch: string
|
||||
first_name: string
|
||||
last_name: string
|
||||
father_name: string
|
||||
birth_date: string
|
||||
phone: string
|
||||
passport_series: string
|
||||
passport_number: string
|
||||
passport_file_1: File | null
|
||||
passport_file_2: File | null
|
||||
passport_file_3: File | null
|
||||
passport_file_4: File | null
|
||||
status: CardPinStatus;
|
||||
note: string;
|
||||
card_type: string;
|
||||
card_number: string;
|
||||
province: string;
|
||||
branch: string;
|
||||
first_name: string;
|
||||
last_name: string;
|
||||
father_name: string;
|
||||
birth_date: string;
|
||||
phone: string;
|
||||
passport_series: string;
|
||||
passport_number: string;
|
||||
passport_file_1: File | null;
|
||||
passport_file_2: File | null;
|
||||
passport_file_3: File | null;
|
||||
passport_file_4: File | null;
|
||||
}
|
||||
|
||||
export type CardPinUpdatePayload = CardPinCreatePayload
|
||||
export type CardPinUpdatePayload = CardPinCreatePayload;
|
||||
|
||||
// ─── API Functions ────────────────────────────────────────────────────────────
|
||||
|
||||
const MOCK_CARD_PINS: CardPinItem[] = [
|
||||
{
|
||||
id: '1',
|
||||
card_type: 'altyn_asyr',
|
||||
card_type_label: 'Altyn Asyr',
|
||||
card_number: '8600123456789012',
|
||||
created_at: '2024-05-07 10:00',
|
||||
province: 'ashgabat',
|
||||
province_label: 'Aşgabat',
|
||||
branch: '1',
|
||||
branch_label: 'Merkezi şahamça',
|
||||
first_name: 'Aman',
|
||||
last_name: 'Amanow',
|
||||
father_name: 'Amanowiç',
|
||||
birth_date: '1990-01-01',
|
||||
phone: '99365123456',
|
||||
status: 'pending',
|
||||
note: 'Test bellik',
|
||||
passport_series: 'I-MR',
|
||||
passport_number: '123456',
|
||||
id: "1",
|
||||
card_type: "altyn_asyr",
|
||||
card_type_label: "Altyn Asyr",
|
||||
card_number: "8600123456789012",
|
||||
created_at: "2024-05-07 10:00",
|
||||
province: "ashgabat",
|
||||
province_label: "Aşgabat",
|
||||
branch: "1",
|
||||
branch_label: "Merkezi şahamça",
|
||||
first_name: "Aman",
|
||||
last_name: "Amanow",
|
||||
father_name: "Amanowiç",
|
||||
birth_date: "1990-01-01",
|
||||
phone: "99365123456",
|
||||
status: "pending",
|
||||
note: "Test bellik",
|
||||
passport_series: "I-MR",
|
||||
passport_number: "123456",
|
||||
passport_file_1: null,
|
||||
passport_file_2: null,
|
||||
passport_file_3: null,
|
||||
passport_file_4: null,
|
||||
created_by: '1',
|
||||
created_by_label: 'Admin',
|
||||
created_by: "1",
|
||||
created_by_label: "Admin",
|
||||
},
|
||||
{
|
||||
id: '2',
|
||||
card_type: 'visa',
|
||||
card_type_label: 'Visa',
|
||||
card_number: '4400123456789012',
|
||||
created_at: '2024-05-07 11:30',
|
||||
province: 'mary',
|
||||
province_label: 'Mary',
|
||||
branch: '2',
|
||||
branch_label: 'Mary şahamçasy',
|
||||
first_name: 'Berdi',
|
||||
last_name: 'Berdiyew',
|
||||
father_name: 'Berdiyewiç',
|
||||
birth_date: '1995-05-05',
|
||||
phone: '99361112233',
|
||||
status: 'approved',
|
||||
id: "2",
|
||||
card_type: "visa",
|
||||
card_type_label: "Visa",
|
||||
card_number: "4400123456789012",
|
||||
created_at: "2024-05-07 11:30",
|
||||
province: "mary",
|
||||
province_label: "Mary",
|
||||
branch: "2",
|
||||
branch_label: "Mary şahamçasy",
|
||||
first_name: "Berdi",
|
||||
last_name: "Berdiyew",
|
||||
father_name: "Berdiyewiç",
|
||||
birth_date: "1995-05-05",
|
||||
phone: "99361112233",
|
||||
status: "approved",
|
||||
note: null,
|
||||
passport_series: 'I-LB',
|
||||
passport_number: '654321',
|
||||
passport_series: "I-LB",
|
||||
passport_number: "654321",
|
||||
passport_file_1: null,
|
||||
passport_file_2: null,
|
||||
passport_file_3: null,
|
||||
passport_file_4: null,
|
||||
created_by: '1',
|
||||
created_by_label: 'Admin',
|
||||
created_by: "1",
|
||||
created_by_label: "Admin",
|
||||
},
|
||||
]
|
||||
];
|
||||
|
||||
export const cardPinApi = {
|
||||
list: async (params: CardPinListParams): Promise<CardPinListResponse> => {
|
||||
@@ -138,68 +138,77 @@ export const cardPinApi = {
|
||||
last_page: 1,
|
||||
per_page: params.per_page || 25,
|
||||
total: MOCK_CARD_PINS.length,
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
const { data } = await apiClient.get('/card-pins', { params })
|
||||
return data
|
||||
const { data } = await apiClient.get("/card-pins", { params });
|
||||
return data;
|
||||
},
|
||||
|
||||
getById: async (id: string): Promise<CardPinItem> => {
|
||||
const mock = MOCK_CARD_PINS.find(p => p.id === id)
|
||||
if (mock) return mock
|
||||
const mock = MOCK_CARD_PINS.find((p) => p.id === id);
|
||||
if (mock) return mock;
|
||||
|
||||
const { data } = await apiClient.get(`/card-pins/${id}`)
|
||||
return data
|
||||
const { data } = await apiClient.get(`/card-pins/${id}`);
|
||||
return data;
|
||||
},
|
||||
|
||||
create: (payload: CardPinCreatePayload): Promise<CardPinItem> => {
|
||||
const form = buildFormData(payload)
|
||||
return apiClient.post('/card-pins', form, {
|
||||
headers: { 'Content-Type': 'multipart/form-data' },
|
||||
}).then((r) => r.data)
|
||||
const form = buildFormData(payload);
|
||||
return apiClient
|
||||
.post("/card-pins", form, {
|
||||
headers: { "Content-Type": "multipart/form-data" },
|
||||
})
|
||||
.then((r) => r.data);
|
||||
},
|
||||
|
||||
update: (id: string, payload: CardPinUpdatePayload): Promise<CardPinItem> => {
|
||||
const form = buildFormData(payload)
|
||||
form.append('_method', 'PUT')
|
||||
return apiClient.post(`/card-pins/${id}`, form, {
|
||||
headers: { 'Content-Type': 'multipart/form-data' },
|
||||
}).then((r) => r.data)
|
||||
const form = buildFormData(payload);
|
||||
form.append("_method", "PUT");
|
||||
return apiClient
|
||||
.post(`/card-pins/${id}`, form, {
|
||||
headers: { "Content-Type": "multipart/form-data" },
|
||||
})
|
||||
.then((r) => r.data);
|
||||
},
|
||||
|
||||
delete: (id: string): Promise<void> =>
|
||||
apiClient.delete(`/card-pins/${id}`).then((r) => r.data),
|
||||
}
|
||||
delete: (id: string): Promise<void> => apiClient.delete(`/card-pins/${id}`).then((r) => r.data),
|
||||
};
|
||||
|
||||
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
||||
|
||||
function buildFormData(payload: CardPinCreatePayload): FormData {
|
||||
const form = new FormData()
|
||||
const form = new FormData();
|
||||
|
||||
const textFields: (keyof CardPinCreatePayload)[] = [
|
||||
'status', 'note', 'card_type', 'card_number',
|
||||
'province', 'branch',
|
||||
'first_name', 'last_name', 'father_name',
|
||||
'birth_date', 'phone',
|
||||
'passport_series', 'passport_number',
|
||||
]
|
||||
"status",
|
||||
"note",
|
||||
"card_type",
|
||||
"card_number",
|
||||
"province",
|
||||
"branch",
|
||||
"first_name",
|
||||
"last_name",
|
||||
"father_name",
|
||||
"birth_date",
|
||||
"phone",
|
||||
"passport_series",
|
||||
"passport_number",
|
||||
];
|
||||
|
||||
for (const key of textFields) {
|
||||
const val = payload[key]
|
||||
const val = payload[key];
|
||||
if (val !== null && val !== undefined) {
|
||||
form.append(key, String(val))
|
||||
form.append(key, String(val));
|
||||
}
|
||||
}
|
||||
|
||||
const fileFields: (keyof CardPinCreatePayload)[] = [
|
||||
'passport_file_1', 'passport_file_2', 'passport_file_3', 'passport_file_4',
|
||||
]
|
||||
const fileFields: (keyof CardPinCreatePayload)[] = ["passport_file_1", "passport_file_2", "passport_file_3", "passport_file_4"];
|
||||
|
||||
for (const key of fileFields) {
|
||||
const file = payload[key]
|
||||
if (file instanceof File) form.append(key, file)
|
||||
const file = payload[key];
|
||||
if (file instanceof File) form.append(key, file);
|
||||
}
|
||||
|
||||
return form
|
||||
}
|
||||
return form;
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { useState, useCallback, useMemo, useEffect } from 'react'
|
||||
import { useState, useCallback, 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 { CreditCard, User, FileText } from 'lucide-react'
|
||||
import type { LucideIcon } from 'lucide-react'
|
||||
@@ -9,6 +10,8 @@ import { FormInput } from '@/components/formInput'
|
||||
import { FormActions } from '@/components/formActions'
|
||||
import { StepBarCards, type StepCardItem } from '@/components/stepBarV2'
|
||||
import type { CardPinItem, CardPinCreatePayload } from '@/features/cardPins/api/cardPinApi'
|
||||
import { validateStep, DEFAULT_FORM_VALUES } from '@/features/cardPins/schemas/cardPin.schema'
|
||||
import type { CardPinFormData } from '@/features/cardPins/schemas/cardPin.schema'
|
||||
|
||||
// ─── Types ────────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -19,28 +22,6 @@ interface CardPinFormProps {
|
||||
isSubmitting: boolean
|
||||
}
|
||||
|
||||
interface FormState {
|
||||
status: string
|
||||
note: string
|
||||
card_type: string
|
||||
card_number: string
|
||||
province: string
|
||||
branch: string
|
||||
first_name: string
|
||||
last_name: string
|
||||
father_name: string
|
||||
birth_date: string
|
||||
phone: string
|
||||
passport_series: string
|
||||
passport_number: string
|
||||
passport_file_1: File | null
|
||||
passport_file_2: File | null
|
||||
passport_file_3: File | null
|
||||
passport_file_4: File | null
|
||||
}
|
||||
|
||||
type FormErrors = Partial<Record<keyof FormState, string>>
|
||||
|
||||
// ─── Constants ────────────────────────────────────────────────────────────────
|
||||
|
||||
const STATUS_OPTIONS = [
|
||||
@@ -71,32 +52,17 @@ const PROVINCE_OPTIONS = [
|
||||
{ value: 'ahal', label: 'Ahal' },
|
||||
]
|
||||
|
||||
const INITIAL_STATE: FormState = {
|
||||
status: 'pending', note: '', card_type: '', card_number: '',
|
||||
province: '', branch: '', first_name: '', last_name: '', father_name: '',
|
||||
birth_date: '', phone: '', passport_series: '', passport_number: '',
|
||||
passport_file_1: null, passport_file_2: null,
|
||||
passport_file_3: null, passport_file_4: null,
|
||||
}
|
||||
// ─── Form errors helper ──────────────────────────────────────────────────────
|
||||
|
||||
function mapToFormState(data: CardPinItem): FormState {
|
||||
return {
|
||||
status: data.status ?? 'pending',
|
||||
note: data.note ?? '',
|
||||
card_type: data.card_type ?? '',
|
||||
card_number: data.card_number ?? '',
|
||||
province: data.province ?? '',
|
||||
branch: data.branch ?? '',
|
||||
first_name: data.first_name ?? '',
|
||||
last_name: data.last_name ?? '',
|
||||
father_name: data.father_name ?? '',
|
||||
birth_date: data.birth_date ?? '',
|
||||
phone: data.phone ?? '',
|
||||
passport_series: data.passport_series ?? '',
|
||||
passport_number: data.passport_number ?? '',
|
||||
passport_file_1: null, passport_file_2: null,
|
||||
passport_file_3: null, passport_file_4: null,
|
||||
type FlatErrors = Partial<Record<keyof CardPinFormData, 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 CardPinFormData] = msg
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// ─── Step definitions ─────────────────────────────────────────────────────────
|
||||
@@ -107,7 +73,7 @@ interface StepDef {
|
||||
titleFallback: string
|
||||
subtitle: string
|
||||
icon: LucideIcon
|
||||
validate: (form: FormState, mode: 'create' | 'edit') => FormErrors
|
||||
validate: (form: CardPinFormData, mode: 'create' | 'edit') => FlatErrors
|
||||
}
|
||||
|
||||
const STEPS: StepDef[] = [
|
||||
@@ -117,15 +83,7 @@ const STEPS: StepDef[] = [
|
||||
titleFallback: 'Kart',
|
||||
subtitle: 'Görnüş & lokasiýa',
|
||||
icon: CreditCard,
|
||||
validate: (form) => {
|
||||
const e: FormErrors = {}
|
||||
if (!form.status) e.status = 'Status hökmany'
|
||||
if (!form.card_type) e.card_type = 'Kart görnüşi hökmany'
|
||||
if (!form.card_number) e.card_number = 'Kart belgisi hökmany'
|
||||
if (!form.province) e.province = 'Welaýat hökmany'
|
||||
if (!form.branch) e.branch = 'Şahamça hökmany'
|
||||
return e
|
||||
},
|
||||
validate: (form, mode) => validateStep(0, form, mode),
|
||||
},
|
||||
{
|
||||
id: 'personal',
|
||||
@@ -133,14 +91,7 @@ const STEPS: StepDef[] = [
|
||||
titleFallback: 'Şahsy',
|
||||
subtitle: 'Maglumatlar',
|
||||
icon: User,
|
||||
validate: (form) => {
|
||||
const e: FormErrors = {}
|
||||
if (!form.first_name) e.first_name = 'Ady hökmany'
|
||||
if (!form.last_name) e.last_name = 'Familiýasy hökmany'
|
||||
if (!form.birth_date) e.birth_date = 'Doglan güni hökmany'
|
||||
if (!form.phone) e.phone = 'Telefon hökmany'
|
||||
return e
|
||||
},
|
||||
validate: (form, mode) => validateStep(1, form, mode),
|
||||
},
|
||||
{
|
||||
id: 'passport',
|
||||
@@ -148,18 +99,7 @@ const STEPS: StepDef[] = [
|
||||
titleFallback: 'Pasport',
|
||||
subtitle: 'Resminamalar',
|
||||
icon: FileText,
|
||||
validate: (form, mode) => {
|
||||
const e: FormErrors = {}
|
||||
if (!form.passport_series) e.passport_series = 'Pasport seriýasy hökmany'
|
||||
if (!form.passport_number) e.passport_number = 'Pasport belgisi hökmany'
|
||||
if (mode === 'create') {
|
||||
if (!form.passport_file_1) e.passport_file_1 = 'Pasport (sahypa 1) hökmany'
|
||||
if (!form.passport_file_2) e.passport_file_2 = 'Pasport (2-3-nji sahypa) hökmany'
|
||||
if (!form.passport_file_3) e.passport_file_3 = 'Pasport (8-9 sahypa) hökmany'
|
||||
if (!form.passport_file_4) e.passport_file_4 = 'Pasport (32-nji sahypa) hökmany'
|
||||
}
|
||||
return e
|
||||
},
|
||||
validate: (form, mode) => validateStep(2, form, mode),
|
||||
},
|
||||
]
|
||||
|
||||
@@ -210,9 +150,9 @@ function BentoCard({
|
||||
// ─── Shared step content props ────────────────────────────────────────────────
|
||||
|
||||
interface StepContentProps {
|
||||
form: FormState
|
||||
errors: FormErrors
|
||||
set: <K extends keyof FormState>(k: K, v: FormState[K]) => void
|
||||
form: CardPinFormData
|
||||
errors: FlatErrors
|
||||
set: <K extends keyof CardPinFormData>(k: K, v: CardPinFormData[K]) => void
|
||||
}
|
||||
|
||||
// ─── Step panels ──────────────────────────────────────────────────────────────
|
||||
@@ -457,17 +397,22 @@ export function CardPinForm({ mode, initialData, onSubmit, isSubmitting }: CardP
|
||||
const { t } = useTranslation()
|
||||
const navigate = useNavigate()
|
||||
|
||||
const [form, setForm] = useState<FormState>(() => initialData ? mapToFormState(initialData) : INITIAL_STATE)
|
||||
const [errors, setErrors] = useState<FormErrors>({})
|
||||
const {
|
||||
watch, setValue, getValues, formState: { errors: rhfErrors }, clearErrors,
|
||||
} = useForm<CardPinFormData>({
|
||||
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 [currentStep, setCurrentStep] = useState(0)
|
||||
const [visited, setVisited] = useState<Set<number>>(
|
||||
() => mode === 'edit' ? new Set(STEPS.map((_, i) => i)) : new Set<number>(),
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
if (initialData) setForm(mapToFormState(initialData))
|
||||
}, [initialData])
|
||||
|
||||
// ── Computed step errors ───────────────────────────────────────────────────
|
||||
|
||||
const stepsWithErrors = useMemo(() => {
|
||||
@@ -480,10 +425,10 @@ export function CardPinForm({ mode, initialData, onSubmit, isSubmitting }: CardP
|
||||
|
||||
// ── set helper ─────────────────────────────────────────────────────────────
|
||||
|
||||
const set = useCallback(<K extends keyof FormState>(key: K, value: FormState[K]) => {
|
||||
setForm((prev) => ({ ...prev, [key]: value }))
|
||||
setErrors((prev) => ({ ...prev, [key]: undefined }))
|
||||
}, [])
|
||||
const set = useCallback(<K extends keyof CardPinFormData>(key: K, value: CardPinFormData[K]) => {
|
||||
(setValue as (name: K, val: CardPinFormData[K]) => void)(key, value)
|
||||
clearErrors(key)
|
||||
}, [setValue, clearErrors])
|
||||
|
||||
const stepProps = useMemo(() => ({ form, errors, set }), [form, errors, set])
|
||||
|
||||
@@ -496,25 +441,21 @@ export function CardPinForm({ mode, initialData, onSubmit, isSubmitting }: CardP
|
||||
markVisited(currentStep)
|
||||
const errs = STEPS[currentStep].validate(form, mode)
|
||||
if (Object.keys(errs).length > 0) {
|
||||
setErrors(errs)
|
||||
toast.error('Dogry maglumat girizmegiňizi haýyş edýäris.')
|
||||
return
|
||||
}
|
||||
setErrors({})
|
||||
setCurrentStep(currentStep + 1)
|
||||
window.scrollTo({ top: 0, behavior: 'smooth' })
|
||||
}
|
||||
|
||||
const handleBack = () => {
|
||||
markVisited(currentStep)
|
||||
setErrors({})
|
||||
setCurrentStep(currentStep - 1)
|
||||
window.scrollTo({ top: 0, behavior: 'smooth' })
|
||||
}
|
||||
|
||||
const handleGoTo = (i: number) => {
|
||||
markVisited(currentStep)
|
||||
setErrors({})
|
||||
setCurrentStep(i)
|
||||
window.scrollTo({ top: 0, behavior: 'smooth' })
|
||||
}
|
||||
@@ -524,14 +465,14 @@ export function CardPinForm({ mode, initialData, onSubmit, isSubmitting }: CardP
|
||||
const handleSubmit = () => {
|
||||
setVisited(new Set(STEPS.map((_, i) => i)))
|
||||
|
||||
const allErrors: FormErrors = {}
|
||||
for (const step of STEPS) Object.assign(allErrors, step.validate(form, mode))
|
||||
const values = getValues()
|
||||
const allErrors: FlatErrors = {}
|
||||
for (const step of STEPS) Object.assign(allErrors, step.validate(values, mode))
|
||||
|
||||
if (Object.keys(allErrors).length > 0) {
|
||||
setErrors(allErrors)
|
||||
toast.error('Käbir hökmany meýdanlar doldurylan däldir.')
|
||||
for (let i = 0; i < STEPS.length; i++) {
|
||||
if (Object.keys(STEPS[i].validate(form, mode)).length > 0) {
|
||||
if (Object.keys(STEPS[i].validate(values, mode)).length > 0) {
|
||||
setCurrentStep(i)
|
||||
break
|
||||
}
|
||||
@@ -540,23 +481,23 @@ export function CardPinForm({ mode, initialData, onSubmit, isSubmitting }: CardP
|
||||
}
|
||||
|
||||
onSubmit({
|
||||
status: form.status as CardPinCreatePayload['status'],
|
||||
note: form.note,
|
||||
card_type: form.card_type,
|
||||
card_number: form.card_number,
|
||||
province: form.province,
|
||||
branch: form.branch,
|
||||
first_name: form.first_name,
|
||||
last_name: form.last_name,
|
||||
father_name: form.father_name,
|
||||
birth_date: form.birth_date,
|
||||
phone: form.phone,
|
||||
passport_series: form.passport_series,
|
||||
passport_number: form.passport_number,
|
||||
passport_file_1: form.passport_file_1,
|
||||
passport_file_2: form.passport_file_2,
|
||||
passport_file_3: form.passport_file_3,
|
||||
passport_file_4: form.passport_file_4,
|
||||
status: values.status as CardPinCreatePayload['status'],
|
||||
note: values.note ?? '',
|
||||
card_type: values.card_type,
|
||||
card_number: values.card_number,
|
||||
province: values.province,
|
||||
branch: values.branch,
|
||||
first_name: values.first_name,
|
||||
last_name: values.last_name,
|
||||
father_name: values.father_name ?? '',
|
||||
birth_date: values.birth_date,
|
||||
phone: values.phone,
|
||||
passport_series: values.passport_series,
|
||||
passport_number: values.passport_number,
|
||||
passport_file_1: values.passport_file_1,
|
||||
passport_file_2: values.passport_file_2,
|
||||
passport_file_3: values.passport_file_3,
|
||||
passport_file_4: values.passport_file_4,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -613,4 +554,22 @@ export function CardPinForm({ mode, initialData, onSubmit, isSubmitting }: CardP
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
function mapInitial(data: CardPinItem): Partial<CardPinFormData> {
|
||||
return {
|
||||
status: data.status ?? 'pending',
|
||||
note: data.note ?? '',
|
||||
card_type: data.card_type ?? '',
|
||||
card_number: data.card_number ?? '',
|
||||
province: data.province ?? '',
|
||||
branch: data.branch ?? '',
|
||||
first_name: data.first_name ?? '',
|
||||
last_name: data.last_name ?? '',
|
||||
father_name: data.father_name ?? '',
|
||||
birth_date: data.birth_date ?? '',
|
||||
phone: data.phone ?? '',
|
||||
passport_series: data.passport_series ?? '',
|
||||
passport_number: data.passport_number ?? '',
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
import { z } from 'zod'
|
||||
|
||||
const fileRequired = z.custom<File>((val) => val instanceof File, 'Faýl hökmany')
|
||||
|
||||
export const cardPinFormSchema = z.object({
|
||||
status: z.string().min(1, 'Status hökmany'),
|
||||
note: z.string().optional(),
|
||||
card_type: z.string().min(1, 'Kart görnüşi hökmany'),
|
||||
card_number: z.string().min(1, 'Kart belgisi hökmany'),
|
||||
province: z.string().min(1, 'Welaýat hökmany'),
|
||||
branch: z.string().min(1, 'Şahamça hökmany'),
|
||||
first_name: z.string().min(1, 'Ady hökmany'),
|
||||
last_name: z.string().min(1, 'Familiýasy hökmany'),
|
||||
father_name: z.string().optional(),
|
||||
birth_date: z.string().min(1, 'Doglan güni hökmany'),
|
||||
phone: z.string().min(1, 'Telefon hökmany'),
|
||||
passport_series: z.string().min(1, 'Pasport seriýasy hökmany'),
|
||||
passport_number: z.string().min(1, 'Pasport belgisi hökmany'),
|
||||
passport_file_1: z.custom<File | null>().nullable(),
|
||||
passport_file_2: z.custom<File | null>().nullable(),
|
||||
passport_file_3: z.custom<File | null>().nullable(),
|
||||
passport_file_4: z.custom<File | null>().nullable(),
|
||||
})
|
||||
|
||||
export type CardPinFormData = z.infer<typeof cardPinFormSchema>
|
||||
|
||||
export const DEFAULT_FORM_VALUES: CardPinFormData = {
|
||||
status: 'pending',
|
||||
note: '',
|
||||
card_type: '',
|
||||
card_number: '',
|
||||
province: '',
|
||||
branch: '',
|
||||
first_name: '',
|
||||
last_name: '',
|
||||
father_name: '',
|
||||
birth_date: '',
|
||||
phone: '',
|
||||
passport_series: '',
|
||||
passport_number: '',
|
||||
passport_file_1: null,
|
||||
passport_file_2: null,
|
||||
passport_file_3: null,
|
||||
passport_file_4: null,
|
||||
}
|
||||
|
||||
export const stepSchemas: Record<number, z.ZodType<Partial<CardPinFormData>>> = {
|
||||
0: cardPinFormSchema.pick({
|
||||
status: true, card_type: true, card_number: true, province: true, branch: true,
|
||||
}),
|
||||
1: cardPinFormSchema.pick({
|
||||
first_name: true, last_name: true, birth_date: true, phone: true,
|
||||
}),
|
||||
2: z.object({
|
||||
passport_series: z.string().min(1, 'Pasport seriýasy hökmany'),
|
||||
passport_number: z.string().min(1, 'Pasport belgisi hökmany'),
|
||||
passport_file_1: fileRequired,
|
||||
passport_file_2: fileRequired,
|
||||
passport_file_3: fileRequired,
|
||||
passport_file_4: fileRequired,
|
||||
}),
|
||||
}
|
||||
|
||||
export function validateStep(
|
||||
stepIndex: number,
|
||||
form: CardPinFormData,
|
||||
mode: 'create' | 'edit',
|
||||
): Partial<Record<keyof CardPinFormData, string>> {
|
||||
if (stepIndex === 2 && mode === 'edit') {
|
||||
const schema = cardPinFormSchema.pick({ passport_series: true, passport_number: true })
|
||||
const result = schema.safeParse(form)
|
||||
if (result.success) return {}
|
||||
const errors: Partial<Record<keyof CardPinFormData, string>> = {}
|
||||
for (const issue of result.error.issues) {
|
||||
const key = issue.path[0] as keyof CardPinFormData
|
||||
if (!errors[key]) errors[key] = issue.message
|
||||
}
|
||||
return errors
|
||||
}
|
||||
|
||||
const schema = stepSchemas[stepIndex]
|
||||
const result = schema.safeParse(form)
|
||||
if (result.success) return {}
|
||||
|
||||
const errors: Partial<Record<keyof CardPinFormData, string>> = {}
|
||||
for (const issue of result.error.issues) {
|
||||
const key = issue.path[0] as keyof CardPinFormData
|
||||
if (!errors[key]) errors[key] = issue.message
|
||||
}
|
||||
return errors
|
||||
}
|
||||
Reference in new issue
Block a user