improves steppbar, and phone validation, added formatter
This commit is contained in:
1 parent
7258b82265
commit
3472738fb1
27 files changed
+6958
-5363
No files matched your search
@@ -1,26 +1,44 @@
|
||||
import { z } from 'zod'
|
||||
import i18next from 'i18next'
|
||||
import type { IntlPaymentCreatePayload, IntlPaymentStatus, CurrencyType } from '../api/visaMasterPaymentsApi'
|
||||
import { z } from "zod";
|
||||
import i18next from "i18next";
|
||||
import type { IntlPaymentCreatePayload, IntlPaymentStatus, CurrencyType } from "../api/visaMasterPaymentsApi";
|
||||
|
||||
const fileField = z.custom<File | null>().nullable()
|
||||
const fileField = z.custom<File | null>().nullable();
|
||||
const VALID_PHONE_PREFIXES = ["61", "62", "63", "64", "65", "71"];
|
||||
|
||||
export const visaMasterPaymentFormSchema = z.object({
|
||||
client_id: z.string().min(1, 'validation.required'),
|
||||
status: z.string().min(1, 'validation.required'),
|
||||
note: z.string().optional().default(''),
|
||||
currency_type: z.string().min(1, 'validation.required'),
|
||||
province: z.string().min(1, 'validation.required'),
|
||||
branch: z.string().min(1, 'validation.required'),
|
||||
passport_first_name: z.string().min(1, 'validation.required'),
|
||||
passport_last_name: z.string().min(1, 'validation.required'),
|
||||
phone: z.string().min(1, 'validation.required'),
|
||||
email: z.string().optional().default(''),
|
||||
home_address: z.string().optional().default(''),
|
||||
passport_series: z.string().min(1, 'validation.required'),
|
||||
passport_number: z.string().min(1, 'validation.required'),
|
||||
payer_full_name: z.string().min(1, 'validation.required'),
|
||||
payer_account_number: z.string().min(1, 'validation.required'),
|
||||
receiver_info: z.string().min(1, 'validation.required'),
|
||||
client_id: z.string().min(1, "validation.required"),
|
||||
status: z.string().min(1, "validation.required"),
|
||||
note: z.string().optional().default(""),
|
||||
currency_type: z.string().min(1, "validation.required"),
|
||||
province: z.string().min(1, "validation.required"),
|
||||
branch: z.string().min(1, "validation.required"),
|
||||
passport_first_name: z.string().min(1, "validation.required"),
|
||||
passport_last_name: z.string().min(1, "validation.required"),
|
||||
phone: z
|
||||
.string()
|
||||
.min(1, "validation.required")
|
||||
.refine(
|
||||
(val) => {
|
||||
const digits = val.replace(/\D/g, "");
|
||||
return digits.length === 8;
|
||||
},
|
||||
{ message: "validation.invalidPhone" },
|
||||
)
|
||||
.refine(
|
||||
(val) => {
|
||||
const digits = val.replace(/\D/g, "");
|
||||
const prefix = digits.slice(0, 2);
|
||||
return VALID_PHONE_PREFIXES.includes(prefix);
|
||||
},
|
||||
{ message: "validation.invalidPhonePrefix" },
|
||||
),
|
||||
email: z.string().optional().default(""),
|
||||
home_address: z.string().optional().default(""),
|
||||
passport_series: z.string().min(1, "validation.required"),
|
||||
passport_number: z.string().min(1, "validation.required"),
|
||||
payer_full_name: z.string().min(1, "validation.required"),
|
||||
payer_account_number: z.string().min(1, "validation.required"),
|
||||
receiver_info: z.string().min(1, "validation.required"),
|
||||
doc_sberbank_account: fileField,
|
||||
doc_school_enrollment: fileField,
|
||||
doc_summons: fileField,
|
||||
@@ -36,62 +54,82 @@ export const visaMasterPaymentFormSchema = z.object({
|
||||
upd_doc_acceptance_letter: fileField,
|
||||
upd_doc_passport_biometric: fileField,
|
||||
upd_doc_passport_old: fileField,
|
||||
})
|
||||
});
|
||||
|
||||
export type IntlPaymentFormData = z.infer<typeof visaMasterPaymentFormSchema>
|
||||
export type IntlPaymentFormData = z.infer<typeof visaMasterPaymentFormSchema>;
|
||||
|
||||
export const stepSchemas: Record<number, z.ZodType<Partial<IntlPaymentFormData>>> = {
|
||||
0: visaMasterPaymentFormSchema.pick({ client_id: true, status: true, currency_type: true }),
|
||||
1: visaMasterPaymentFormSchema.pick({ province: true, branch: true }),
|
||||
2: visaMasterPaymentFormSchema.pick({ passport_first_name: true, passport_last_name: true, phone: true }),
|
||||
3: visaMasterPaymentFormSchema.pick({
|
||||
passport_series: true, passport_number: true,
|
||||
payer_full_name: true, payer_account_number: true, receiver_info: true,
|
||||
passport_series: true,
|
||||
passport_number: true,
|
||||
payer_full_name: true,
|
||||
payer_account_number: true,
|
||||
receiver_info: true,
|
||||
}),
|
||||
4: z.object({}),
|
||||
}
|
||||
};
|
||||
|
||||
export function validateStep(
|
||||
stepIndex: number,
|
||||
form: IntlPaymentFormData,
|
||||
mode: 'create' | 'edit',
|
||||
mode: "create" | "edit",
|
||||
): Partial<Record<keyof IntlPaymentFormData, string>> {
|
||||
void mode
|
||||
const schema = stepSchemas[stepIndex]
|
||||
const result = schema.safeParse(form)
|
||||
if (result.success) return {}
|
||||
const errors: Partial<Record<keyof IntlPaymentFormData, string>> = {}
|
||||
void mode;
|
||||
const schema = stepSchemas[stepIndex];
|
||||
const result = schema.safeParse(form);
|
||||
if (result.success) return {};
|
||||
const errors: Partial<Record<keyof IntlPaymentFormData, string>> = {};
|
||||
for (const issue of result.error.issues) {
|
||||
const key = issue.path[0] as keyof IntlPaymentFormData
|
||||
const key = issue.path[0] as keyof IntlPaymentFormData;
|
||||
if (!errors[key]) {
|
||||
const msg = issue.message
|
||||
errors[key] = msg.startsWith('validation.') ? i18next.t(msg, msg) : msg
|
||||
const msg = issue.message;
|
||||
errors[key] = msg.startsWith("validation.") ? i18next.t(msg, msg) : msg;
|
||||
}
|
||||
}
|
||||
return errors
|
||||
return errors;
|
||||
}
|
||||
|
||||
export const DEFAULT_FORM_VALUES: IntlPaymentFormData = {
|
||||
client_id: '', status: 'pending', note: '', currency_type: '',
|
||||
province: '', branch: '',
|
||||
passport_first_name: '', passport_last_name: '', phone: '',
|
||||
email: '', home_address: '',
|
||||
passport_series: '', passport_number: '',
|
||||
payer_full_name: '', payer_account_number: '', receiver_info: '',
|
||||
doc_sberbank_account: null, doc_school_enrollment: null,
|
||||
doc_summons: null, doc_passport_tm: null,
|
||||
doc_foreign_passport: null, doc_foreign_passport_copy: null,
|
||||
doc_exit_permission: null, doc_school_foreign_info: null,
|
||||
client_id: "",
|
||||
status: "pending",
|
||||
note: "",
|
||||
currency_type: "",
|
||||
province: "",
|
||||
branch: "",
|
||||
passport_first_name: "",
|
||||
passport_last_name: "",
|
||||
phone: "",
|
||||
email: "",
|
||||
home_address: "",
|
||||
passport_series: "",
|
||||
passport_number: "",
|
||||
payer_full_name: "",
|
||||
payer_account_number: "",
|
||||
receiver_info: "",
|
||||
doc_sberbank_account: null,
|
||||
doc_school_enrollment: null,
|
||||
doc_summons: null,
|
||||
doc_passport_tm: null,
|
||||
doc_foreign_passport: null,
|
||||
doc_foreign_passport_copy: null,
|
||||
doc_exit_permission: null,
|
||||
doc_school_foreign_info: null,
|
||||
doc_school_departure_info: null,
|
||||
upd_doc_passport_tm: null, upd_doc_foreign_passport: null,
|
||||
upd_doc_visa: null, upd_doc_acceptance_letter: null,
|
||||
upd_doc_passport_biometric: null, upd_doc_passport_old: null,
|
||||
}
|
||||
upd_doc_passport_tm: null,
|
||||
upd_doc_foreign_passport: null,
|
||||
upd_doc_visa: null,
|
||||
upd_doc_acceptance_letter: null,
|
||||
upd_doc_passport_biometric: null,
|
||||
upd_doc_passport_old: null,
|
||||
};
|
||||
|
||||
export function buildPayload(data: IntlPaymentFormData): IntlPaymentCreatePayload {
|
||||
return {
|
||||
...data,
|
||||
status: data.status as IntlPaymentStatus,
|
||||
currency_type: data.currency_type as CurrencyType,
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user