From 8b0448553324e525f4b168190d72e0a1cc412731 Mon Sep 17 00:00:00 2001 From: smanylov Date: Thu, 29 Jan 2026 21:34:02 +0700 Subject: [PATCH] complite b2b b2c registration --- src/app/actions/auth.ts | 14 ++++++-------- src/app/actions/definitions.ts | 11 +++++++---- src/app/lib/validation-config-parser.ts | 4 ++-- src/app/ui/forms/register-form.tsx | 3 +++ 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/app/actions/auth.ts b/src/app/actions/auth.ts index 0b6439d..6d22583 100644 --- a/src/app/actions/auth.ts +++ b/src/app/actions/auth.ts @@ -146,9 +146,7 @@ export async function registration( state: FormState | undefined, formData: FormData, ): Promise { - 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 { diff --git a/src/app/actions/definitions.ts b/src/app/actions/definitions.ts index fec94cf..6488098 100644 --- a/src/app/actions/definitions.ts +++ b/src/app/actions/definitions.ts @@ -38,26 +38,28 @@ const defaultConfig = validationConfig as ValidationConfig; let cachedSignupSchema: any = null; let schemaPromise: Promise | null = null; -async function loadConfigFromBackend(): Promise { +async function loadConfigFromBackend(accountType: 'b2b' | 'b2c'): Promise { 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); diff --git a/src/app/lib/validation-config-parser.ts b/src/app/lib/validation-config-parser.ts index 8baedd7..e6bcf97 100644 --- a/src/app/lib/validation-config-parser.ts +++ b/src/app/lib/validation-config-parser.ts @@ -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 = {}; // fullName @@ -39,7 +39,7 @@ export function createValidationSchema(config: ValidationConfig) { } // fullName - if (config.companyName) { + if (config.companyName && accountType !== 'b2c') { schema.companyName = z .string() .trim() diff --git a/src/app/ui/forms/register-form.tsx b/src/app/ui/forms/register-form.tsx index a705888..b818b42 100644 --- a/src/app/ui/forms/register-form.tsx +++ b/src/app/ui/forms/register-form.tsx @@ -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: