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