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