complite b2b b2c registration
This commit is contained in:
@@ -146,9 +146,7 @@ export async function registration(
|
||||
state: FormState | undefined,
|
||||
formData: FormData,
|
||||
): Promise<FormState> {
|
||||
const accountType = formData.get('accountType') as string || '';
|
||||
|
||||
console.log(accountType);
|
||||
const accountType = formData.get('accountType') as 'b2b' | 'b2c';
|
||||
|
||||
const fullName = formData.get('fullName') as string || '';
|
||||
const email = formData.get('email') as string || '';
|
||||
@@ -158,7 +156,7 @@ export async function registration(
|
||||
const companyName = formData.get('companyName') as string || '';
|
||||
const agree = formData.get('agree') as string || '';
|
||||
|
||||
const SignupFormSchema = await getSignupFormSchema();
|
||||
const SignupFormSchema = await getSignupFormSchema(accountType);
|
||||
|
||||
const dataToValidate = {
|
||||
fullName,
|
||||
@@ -167,7 +165,7 @@ export async function registration(
|
||||
password,
|
||||
confirm_password,
|
||||
agree,
|
||||
...(accountType === 'btb' && { companyName })
|
||||
...(accountType === 'b2b' && { companyName })
|
||||
};
|
||||
|
||||
const validatedFields = SignupFormSchema.safeParse(dataToValidate);
|
||||
@@ -193,7 +191,7 @@ export async function registration(
|
||||
confirm_password,
|
||||
phone,
|
||||
agree,
|
||||
...(accountType === 'btb' && { companyName })
|
||||
...(accountType === 'b2b' && { companyName })
|
||||
},
|
||||
error: errors
|
||||
};
|
||||
@@ -212,7 +210,7 @@ export async function registration(
|
||||
phone: phone,
|
||||
password,
|
||||
accountType,
|
||||
...(accountType === 'btb' && { companyName: companyName })
|
||||
...(accountType === 'b2b' && { companyName: companyName })
|
||||
}
|
||||
}),
|
||||
headers: {
|
||||
@@ -227,7 +225,7 @@ export async function registration(
|
||||
message_body: { token: string },
|
||||
message_code: string
|
||||
};
|
||||
console.log(parsed);
|
||||
|
||||
if (parsed.message_desc === 'Operation successful') {
|
||||
await createSession(parsed.message_body.token, email as string);
|
||||
} else {
|
||||
|
||||
@@ -38,26 +38,28 @@ const defaultConfig = validationConfig as ValidationConfig;
|
||||
let cachedSignupSchema: any = null;
|
||||
let schemaPromise: Promise<any> | null = null;
|
||||
|
||||
async function loadConfigFromBackend(): Promise<any> {
|
||||
async function loadConfigFromBackend(accountType: 'b2b' | 'b2c'): Promise<any> {
|
||||
try {
|
||||
const response = await fetch('/api/validation-config');
|
||||
if (!response.ok) throw new Error('Failed to load config');
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
//сейчас выполняем это пока бека нету
|
||||
console.error('Using default validation config:', error);
|
||||
return defaultConfig;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getSignupFormSchema() {
|
||||
export async function getSignupFormSchema(accountType: 'b2b' | 'b2c') {
|
||||
if (cachedSignupSchema) {
|
||||
return cachedSignupSchema;
|
||||
}
|
||||
|
||||
if (!schemaPromise) {
|
||||
schemaPromise = (async () => {
|
||||
const config = await loadConfigFromBackend();
|
||||
cachedSignupSchema = createValidationSchema(config);
|
||||
const config = await loadConfigFromBackend(accountType);
|
||||
cachedSignupSchema = createValidationSchema(config, accountType);
|
||||
|
||||
return cachedSignupSchema;
|
||||
})();
|
||||
}
|
||||
@@ -65,6 +67,7 @@ export async function getSignupFormSchema() {
|
||||
return await schemaPromise;
|
||||
}
|
||||
|
||||
// для валидации в реально времени на клиентской странице
|
||||
export const SignupFormSchema = createValidationSchema(defaultConfig);
|
||||
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ interface ValidationConfig {
|
||||
|
||||
/* const EMAIL_REGEX = /^([a-z0-9.\-_]+|[а-яё0-9.\-_]+)@(([a-z0-9.\-]+)\.([a-z]{2,})|([a-z0-9.\-]+)\.([а-яё]{2,})|([а-яё0-9.\-]+)\.([a-z]{2,})|([а-яё0-9.\-]+)\.([а-яё]{2,}))$/i; */
|
||||
|
||||
export function createValidationSchema(config: ValidationConfig) {
|
||||
export function createValidationSchema(config: ValidationConfig, accountType?: 'b2b' | 'b2c') {
|
||||
const schema: Record<string, any> = {};
|
||||
|
||||
// fullName
|
||||
@@ -39,7 +39,7 @@ export function createValidationSchema(config: ValidationConfig) {
|
||||
}
|
||||
|
||||
// fullName
|
||||
if (config.companyName) {
|
||||
if (config.companyName && accountType !== 'b2c') {
|
||||
schema.companyName = z
|
||||
.string()
|
||||
.trim()
|
||||
|
||||
@@ -84,6 +84,9 @@ export default function RegisterForm() {
|
||||
if (fieldName === 'phone') {
|
||||
return schema.safeParse(value.replace(/[-\(\)\s]/g, ''));
|
||||
}
|
||||
if (fieldName === 'companyName' && accountType === 'b2c') {
|
||||
return { success: true, data: value };
|
||||
}
|
||||
return schema.safeParse(value);
|
||||
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user