2024-06-17 21:30:58 +03:00
|
|
|
|
'use client';
|
|
|
|
|
|
2025-02-03 13:13:11 +03:00
|
|
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
2024-06-17 21:30:58 +03:00
|
|
|
|
import axios from 'axios';
|
|
|
|
|
import clsx from 'clsx';
|
2025-02-03 13:13:11 +03:00
|
|
|
|
import { useState } from 'react';
|
|
|
|
|
import { useForm } from 'react-hook-form';
|
2024-06-17 21:30:58 +03:00
|
|
|
|
|
2025-01-27 15:50:15 +03:00
|
|
|
|
import { useConceptNavigation } from '@/app/Navigation/NavigationContext';
|
2024-06-17 21:30:58 +03:00
|
|
|
|
import { urls } from '@/app/urls';
|
2025-02-03 13:13:11 +03:00
|
|
|
|
import { IUserSignupDTO, UserSignupSchema } from '@/backend/users/api';
|
2025-01-21 20:33:05 +03:00
|
|
|
|
import { useSignup } from '@/backend/users/useSignup';
|
2024-06-17 21:30:58 +03:00
|
|
|
|
import { IconHelp } from '@/components/Icons';
|
2025-01-29 23:18:08 +03:00
|
|
|
|
import { ErrorData } from '@/components/info/InfoError';
|
2024-06-17 21:30:58 +03:00
|
|
|
|
import Button from '@/components/ui/Button';
|
|
|
|
|
import Checkbox from '@/components/ui/Checkbox';
|
|
|
|
|
import FlexColumn from '@/components/ui/FlexColumn';
|
|
|
|
|
import Overlay from '@/components/ui/Overlay';
|
|
|
|
|
import PrettyJson from '@/components/ui/PrettyJSON';
|
|
|
|
|
import SubmitButton from '@/components/ui/SubmitButton';
|
|
|
|
|
import TextInput from '@/components/ui/TextInput';
|
|
|
|
|
import TextURL from '@/components/ui/TextURL';
|
|
|
|
|
import Tooltip from '@/components/ui/Tooltip';
|
|
|
|
|
import { HelpTopic } from '@/models/miscellaneous';
|
|
|
|
|
import { globals, patterns } from '@/utils/constants';
|
|
|
|
|
|
|
|
|
|
function FormSignup() {
|
|
|
|
|
const router = useConceptNavigation();
|
2025-02-03 13:13:11 +03:00
|
|
|
|
const { signup, isPending, error: serverError, reset } = useSignup();
|
2024-06-17 21:30:58 +03:00
|
|
|
|
const [acceptPrivacy, setAcceptPrivacy] = useState(false);
|
|
|
|
|
const [acceptRules, setAcceptRules] = useState(false);
|
|
|
|
|
|
2025-02-03 13:13:11 +03:00
|
|
|
|
const {
|
|
|
|
|
register,
|
|
|
|
|
handleSubmit,
|
|
|
|
|
clearErrors,
|
|
|
|
|
formState: { errors }
|
|
|
|
|
} = useForm<IUserSignupDTO>({
|
|
|
|
|
resolver: zodResolver(UserSignupSchema)
|
|
|
|
|
});
|
2024-06-17 21:30:58 +03:00
|
|
|
|
|
2025-02-03 13:13:11 +03:00
|
|
|
|
function resetErrors() {
|
2025-01-21 20:33:05 +03:00
|
|
|
|
reset();
|
2025-02-03 13:13:11 +03:00
|
|
|
|
clearErrors();
|
|
|
|
|
}
|
2024-06-17 21:30:58 +03:00
|
|
|
|
|
|
|
|
|
function handleCancel() {
|
|
|
|
|
if (router.canBack()) {
|
|
|
|
|
router.back();
|
|
|
|
|
} else {
|
|
|
|
|
router.push(urls.library);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-03 13:13:11 +03:00
|
|
|
|
function onSubmit(data: IUserSignupDTO) {
|
|
|
|
|
signup(data, createdUser => router.push(urls.login_hint(createdUser.username)));
|
2024-06-17 21:30:58 +03:00
|
|
|
|
}
|
2025-02-03 18:17:07 +03:00
|
|
|
|
|
2024-06-17 21:30:58 +03:00
|
|
|
|
return (
|
2025-02-03 13:13:11 +03:00
|
|
|
|
<form
|
|
|
|
|
className={clsx('cc-fade-in cc-column', 'mx-auto w-[36rem]', 'px-6 py-3')}
|
|
|
|
|
onSubmit={event => void handleSubmit(onSubmit)(event)}
|
|
|
|
|
onChange={resetErrors}
|
|
|
|
|
>
|
2024-06-17 21:30:58 +03:00
|
|
|
|
<h1>
|
|
|
|
|
<span>Новый пользователь</span>
|
|
|
|
|
<Overlay id={globals.password_tooltip} position='top-[5.4rem] left-[3.5rem]'>
|
|
|
|
|
<IconHelp size='1.25rem' className='icon-primary' />
|
|
|
|
|
</Overlay>
|
|
|
|
|
<Tooltip anchorSelect={`#${globals.password_tooltip}`} offset={6}>
|
|
|
|
|
используйте уникальный пароль для каждого сайта
|
|
|
|
|
</Tooltip>
|
|
|
|
|
<Overlay id={globals.email_tooltip} position='top-[0.5rem] right-[1.75rem]'>
|
|
|
|
|
<IconHelp size='1.25rem' className='icon-primary' />
|
|
|
|
|
</Overlay>
|
|
|
|
|
<Tooltip anchorSelect={`#${globals.email_tooltip}`} offset={6}>
|
|
|
|
|
электронная почта используется для восстановления пароля
|
|
|
|
|
</Tooltip>
|
|
|
|
|
</h1>
|
|
|
|
|
<div className='flex gap-12'>
|
|
|
|
|
<FlexColumn>
|
|
|
|
|
<TextInput
|
|
|
|
|
id='username'
|
2025-02-03 13:13:11 +03:00
|
|
|
|
{...register('username')}
|
2024-06-17 21:30:58 +03:00
|
|
|
|
autoComplete='username'
|
|
|
|
|
label='Имя пользователя (логин)'
|
2024-07-19 20:42:54 +03:00
|
|
|
|
spellCheck={false}
|
2024-06-17 21:30:58 +03:00
|
|
|
|
pattern={patterns.login}
|
|
|
|
|
title='Минимум 3 знака. Латинские буквы и цифры. Не может начинаться с цифры'
|
|
|
|
|
className='w-[15rem]'
|
2025-02-03 13:13:11 +03:00
|
|
|
|
error={errors.username}
|
2024-06-17 21:30:58 +03:00
|
|
|
|
/>
|
|
|
|
|
<TextInput
|
|
|
|
|
id='password'
|
|
|
|
|
type='password'
|
2025-02-03 13:13:11 +03:00
|
|
|
|
{...register('password')}
|
2024-06-17 21:30:58 +03:00
|
|
|
|
autoComplete='new-password'
|
|
|
|
|
label='Пароль'
|
|
|
|
|
className='w-[15rem]'
|
2025-02-03 13:13:11 +03:00
|
|
|
|
error={errors.password}
|
2024-06-17 21:30:58 +03:00
|
|
|
|
/>
|
|
|
|
|
<TextInput
|
|
|
|
|
id='password2'
|
|
|
|
|
type='password'
|
2025-02-03 13:13:11 +03:00
|
|
|
|
{...register('password2')}
|
2024-06-17 21:30:58 +03:00
|
|
|
|
label='Повторите пароль'
|
|
|
|
|
autoComplete='new-password'
|
|
|
|
|
className='w-[15rem]'
|
2025-02-03 13:13:11 +03:00
|
|
|
|
error={errors.password2}
|
2024-06-17 21:30:58 +03:00
|
|
|
|
/>
|
|
|
|
|
</FlexColumn>
|
|
|
|
|
|
|
|
|
|
<FlexColumn className='w-[15rem]'>
|
|
|
|
|
<TextInput
|
|
|
|
|
id='email'
|
2025-02-03 13:13:11 +03:00
|
|
|
|
{...register('email')}
|
2024-06-17 21:30:58 +03:00
|
|
|
|
autoComplete='email'
|
|
|
|
|
required
|
2024-07-19 20:42:54 +03:00
|
|
|
|
spellCheck={false}
|
2024-06-17 21:30:58 +03:00
|
|
|
|
label='Электронная почта (email)'
|
|
|
|
|
title='электронная почта в корректном формате, например: i.petrov@mycompany.ru.com'
|
2025-02-03 13:13:11 +03:00
|
|
|
|
error={errors.email}
|
2024-06-17 21:30:58 +03:00
|
|
|
|
/>
|
|
|
|
|
<TextInput
|
|
|
|
|
id='first_name'
|
2025-02-03 13:13:11 +03:00
|
|
|
|
{...register('first_name')}
|
2024-06-17 21:30:58 +03:00
|
|
|
|
label='Отображаемое имя'
|
|
|
|
|
autoComplete='given-name'
|
2025-02-03 13:13:11 +03:00
|
|
|
|
error={errors.first_name}
|
2024-06-17 21:30:58 +03:00
|
|
|
|
/>
|
|
|
|
|
<TextInput
|
|
|
|
|
id='last_name'
|
2025-02-03 13:13:11 +03:00
|
|
|
|
{...register('last_name')}
|
2024-06-17 21:30:58 +03:00
|
|
|
|
label='Отображаемая фамилия'
|
|
|
|
|
autoComplete='family-name'
|
2025-02-03 13:13:11 +03:00
|
|
|
|
error={errors.last_name}
|
2024-06-17 21:30:58 +03:00
|
|
|
|
/>
|
|
|
|
|
</FlexColumn>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className='flex gap-1 text-sm'>
|
|
|
|
|
<Checkbox id='accept_terms' label='Принимаю условия' value={acceptPrivacy} setValue={setAcceptPrivacy} />
|
|
|
|
|
<TextURL text='обработки персональных данных...' href={urls.help_topic(HelpTopic.INFO_PRIVACY)} />
|
|
|
|
|
</div>
|
|
|
|
|
<div className='flex gap-1 text-sm'>
|
|
|
|
|
<Checkbox id='accept_rules' label='Принимаю ' value={acceptRules} setValue={setAcceptRules} />
|
|
|
|
|
<TextURL text='правила поведения на Портале...' href={urls.help_topic(HelpTopic.INFO_RULES)} />
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className='flex justify-around my-3'>
|
2025-02-03 18:17:07 +03:00
|
|
|
|
<SubmitButton
|
|
|
|
|
text='Регистрировать'
|
|
|
|
|
className='min-w-[10rem]'
|
|
|
|
|
loading={isPending}
|
|
|
|
|
disabled={!acceptPrivacy || !acceptRules}
|
|
|
|
|
/>
|
2024-06-17 21:30:58 +03:00
|
|
|
|
<Button text='Назад' className='min-w-[10rem]' onClick={() => handleCancel()} />
|
|
|
|
|
</div>
|
2025-02-03 13:13:11 +03:00
|
|
|
|
{serverError ? <ServerError error={serverError} /> : null}
|
2024-06-17 21:30:58 +03:00
|
|
|
|
</form>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default FormSignup;
|
|
|
|
|
|
|
|
|
|
// ====== Internals =========
|
2025-02-03 13:13:11 +03:00
|
|
|
|
function ServerError({ error }: { error: ErrorData }): React.ReactElement {
|
2024-06-17 21:30:58 +03:00
|
|
|
|
if (axios.isAxiosError(error) && error.response && error.response.status === 400) {
|
|
|
|
|
if ('email' in error.response.data) {
|
|
|
|
|
return (
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
2025-02-03 13:13:11 +03:00
|
|
|
|
<div className='mx-auto text-sm select-text text-warn-600'>{error.response.data.email}</div>
|
2024-06-17 21:30:58 +03:00
|
|
|
|
);
|
|
|
|
|
} else if ('username' in error.response.data) {
|
|
|
|
|
return (
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
2025-02-03 13:13:11 +03:00
|
|
|
|
<div className='mx-auto text-sm select-text text-warn-600'>{error.response.data.username}</div>
|
2024-06-17 21:30:58 +03:00
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return (
|
2024-12-16 23:51:31 +03:00
|
|
|
|
<div className='mx-auto text-sm select-text text-warn-600'>
|
2024-06-17 21:30:58 +03:00
|
|
|
|
<PrettyJson data={error.response} />
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-29 23:18:08 +03:00
|
|
|
|
throw error as Error;
|
2024-06-17 21:30:58 +03:00
|
|
|
|
}
|