edit company-user registration

This commit is contained in:
smanylov
2026-02-05 19:31:06 +07:00
parent 8890cfb067
commit 777555c187
4 changed files with 60 additions and 19 deletions
+35 -15
View File
@@ -1,8 +1,7 @@
'use server'
import { API_BASE_URL, getSignupFormSchema } from '@/app/actions/definitions';
import { createSession } from '@/app/actions/session';
import { redirect } from 'next/navigation';
import { getSessionData } from '@/app/actions/session';
export type CompanyUserRegistrationFormState = {
previousState: {
@@ -12,6 +11,7 @@ export type CompanyUserRegistrationFormState = {
confirm_password: string;
phone: string;
},
createdUser: string | null
error: Record<string, string> | null
} | undefined;
@@ -19,6 +19,11 @@ export async function companyUserRegistration(
state: CompanyUserRegistrationFormState | undefined,
formData: FormData,
): Promise<CompanyUserRegistrationFormState> {
const token = await getSessionData('token');
if (!token) {
return;
}
const fullName = formData.get('fullName') as string || '';
const email = formData.get('email') as string || '';
const password = formData.get('password') as string || '';
@@ -53,6 +58,7 @@ export async function companyUserRegistration(
}
});
console.log('registration error');
console.log(errors);
return {
@@ -63,21 +69,11 @@ export async function companyUserRegistration(
confirm_password,
phone
},
createdUser: null,
error: errors
};
}
return {
previousState: {
fullName,
email,
password,
confirm_password,
phone
},
error: null
};
try {
const { fullName, email, password, phone } = validatedFields.data;
const response = await fetch(`${API_BASE_URL}/api/v1/data`, {
@@ -89,7 +85,10 @@ export async function companyUserRegistration(
fullName: fullName,
email,
phone: phone,
password
password,
accountType: 'b2b',
authToken: token,
companyName: ''
}
}),
headers: {
@@ -104,6 +103,14 @@ export async function companyUserRegistration(
message_body: { token: string },
message_code: number
};
console.log({
fullName: fullName,
email,
phone: phone,
password,
accountType: 'b2b',
authToken: token
});
if (parsed.message_code === 0) {
console.log(`registration - ${email}`);
@@ -113,7 +120,18 @@ export async function companyUserRegistration(
phone: phone,
password
});
await createSession(parsed.message_body.token, email as string);
return {
previousState: {
fullName: '',
email: '',
password: '',
confirm_password: '',
phone: ''
},
createdUser: fullName,
error: null
}
} else {
throw parsed;
}
@@ -162,6 +180,7 @@ export async function companyUserRegistration(
confirm_password,
phone
},
createdUser: null,
error: errors
};
}
@@ -174,6 +193,7 @@ export async function companyUserRegistration(
confirm_password,
phone
},
createdUser: null,
error: errors
};
}
+10 -3
View File
@@ -1,16 +1,23 @@
'use client'
import { useState, ChangeEvent } from 'react';
import { useState, ChangeEvent, useEffect } from 'react';
import { InputMask } from '@react-input/mask';
import styles from '@/app/styles/module/login.module.scss';
interface PhoneInputProps {
phoneState: string | undefined;
validateHandler: (e: ChangeEvent<HTMLInputElement>) => void;
clearCondition?: string | undefined | null
}
export default function PhoneInput({ phoneState, validateHandler }: PhoneInputProps) {
const [phone, setPhone] = useState(phoneState ? phoneState : '');
export default function PhoneInput({ phoneState, validateHandler, clearCondition }: PhoneInputProps) {
const [phone, setPhone] = useState<string | undefined>(phoneState ? phoneState : '');
useEffect(() => {
if (clearCondition) {
setPhone('');
}
}, [clearCondition])
return (
<InputMask
@@ -11,6 +11,7 @@ import { SignupFormSchema } from '@/app/actions/definitions';
import * as z from 'zod';
import PhoneInput from '@/app/ui/inputs/phone-input';
import { IconEye } from '@/app/ui/icons/icons';
import { toast } from 'sonner';
export function CompanyUsersSettingPanel() {
const {
@@ -43,6 +44,9 @@ export function CompanyUsersSettingPanel() {
useEffect(() => {
if (state) {
setFormState(state);
if (state.createdUser) {
toast.success(`Создан пользователь - ${state.createdUser}`);
}
}
}, [state])
@@ -84,7 +88,6 @@ export function CompanyUsersSettingPanel() {
case 'agree':
const schema = SignupFormSchema.shape[fieldName];
if (fieldName === 'phone') {
console.log(schema.safeParse(value.replace(/[-\(\)\s]/g, '')));
return schema.safeParse(value.replace(/[-\(\)\s]/g, ''));
}
if (fieldName === 'companyName') {
@@ -204,6 +207,7 @@ export function CompanyUsersSettingPanel() {
<PhoneInput
phoneState={formState?.previousState?.phone}
validateHandler={onChangeHandler}
clearCondition={state?.createdUser}
/>
{formState?.error?.phone && (
<p className="text-sm text-red-500">
@@ -294,6 +298,12 @@ export function CompanyUsersSettingPanel() {
)}
</div>
{formState?.error?.server && (
<p className="text-sm text-red-500 mt-4">
{t(formState?.error?.server)}
</p>
)}
<button type="submit" className="btn btn-primary">
Добавить пользователя
</button>
@@ -25,6 +25,10 @@ export default function PersonalDataSettings() {
}
});
useEffect(() => {
console.log(userData)
}, [userData]);
const t = useTranslations('Global');
return (