ConceptPortal-public/rsconcept/frontend/src/pages/RegisterPage.tsx

162 lines
5.3 KiB
TypeScript
Raw Normal View History

'use client';
2023-07-15 17:46:19 +03:00
import { useEffect, useState } from 'react';
2023-12-16 19:20:26 +03:00
import { BiInfoCircle } from 'react-icons/bi';
import { toast } from 'react-toastify';
2023-07-15 17:46:19 +03:00
import Button from '@/components/Common/Button';
import Checkbox from '@/components/Common/Checkbox';
import ConceptTooltip from '@/components/Common/ConceptTooltip';
import Overlay from '@/components/Common/Overlay';
import SubmitButton from '@/components/Common/SubmitButton';
import TextInput from '@/components/Common/TextInput';
import TextURL from '@/components/Common/TextURL';
import ExpectedAnonymous from '@/components/ExpectedAnonymous';
import InfoError from '@/components/InfoError';
import { useAuth } from '@/context/AuthContext';
import { useConceptNavigation } from '@/context/NagivationContext';
import { type IUserSignupData } from '@/models/library';
import { globalIDs, patterns } from '@/utils/constants';
2023-07-15 17:46:19 +03:00
function RegisterPage() {
const router = useConceptNavigation();
2023-07-31 23:47:18 +03:00
const { user, signup, loading, error, setError } = useAuth();
2023-07-15 17:46:19 +03:00
const [username, setUsername] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [password2, setPassword2] = useState('');
const [firstName, setFirstName] = useState('');
const [lastName, setLastName] = useState('');
2023-07-25 20:27:29 +03:00
2023-12-07 23:08:49 +03:00
const [acceptPrivacy, setAcceptPrivacy] = useState(false);
2023-07-15 17:46:19 +03:00
useEffect(() => {
setError(undefined);
}, [username, email, password, password2, setError]);
2023-09-02 01:11:27 +03:00
function handleCancel() {
if (router.canBack()) {
router.back();
2023-09-02 01:11:27 +03:00
} else {
router.push('/library');
2023-09-02 01:11:27 +03:00
}
}
2023-07-31 23:47:18 +03:00
function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
2023-07-15 17:46:19 +03:00
event.preventDefault();
if (!loading) {
2023-07-25 20:27:29 +03:00
const data: IUserSignupData = {
username,
email,
password,
password2,
first_name: firstName,
last_name: lastName
2023-07-15 17:46:19 +03:00
};
signup(data, createdUser => {
router.push(`/login?username=${createdUser.username}`);
toast.success(`Пользователь успешно создан: ${createdUser.username}`);
});
2023-07-15 17:46:19 +03:00
}
2023-07-31 23:47:18 +03:00
}
2023-07-15 17:46:19 +03:00
2023-12-08 00:33:47 +03:00
if (user) {
return (<ExpectedAnonymous />);
}
2023-07-15 17:46:19 +03:00
return (
2023-12-08 00:33:47 +03:00
<form
2023-12-08 19:58:16 +03:00
className='flex flex-col gap-3 px-6 py-3 h-fit'
2023-12-08 00:33:47 +03:00
onSubmit={handleSubmit}
>
<h1>Новый пользователь</h1>
<div className='flex gap-12'>
<div className='flex flex-col gap-3'>
<div className='absolute'>
<Overlay
id={globalIDs.password_tooltip}
position='top-[4.8rem] left-[3.4rem] absolute'
>
2023-12-16 19:20:26 +03:00
<BiInfoCircle size='1.25rem' className='clr-text-primary' />
2023-12-08 00:33:47 +03:00
</Overlay>
<ConceptTooltip
anchorSelect={`#${globalIDs.password_tooltip}`}
offset={6}
>
<p>- используйте уникальный пароль</p>
<p>- портал функционирует в тестовом режиме</p>
</ConceptTooltip>
</div>
<TextInput id='username' required
label='Имя пользователя (логин)'
2023-12-08 19:58:16 +03:00
pattern={patterns.login}
tooltip='Минимум 3 знака. Латинские буквы и цифры. Не может начинаться с цифры'
2023-12-08 00:33:47 +03:00
value={username}
className='w-[15rem]'
2023-12-08 00:33:47 +03:00
onChange={event => setUsername(event.target.value)}
/>
<TextInput id='password' type='password' required
label='Пароль'
className='w-[15rem]'
2023-12-08 00:33:47 +03:00
value={password}
onChange={event => setPassword(event.target.value)}
2023-12-07 23:08:49 +03:00
/>
2023-12-08 00:33:47 +03:00
<TextInput id='password2' required type='password'
label='Повторите пароль'
className='w-[15rem]'
2023-12-08 00:33:47 +03:00
value={password2}
onChange={event => setPassword2(event.target.value)}
2023-12-07 23:08:49 +03:00
/>
</div>
2023-12-08 00:33:47 +03:00
<div className='flex flex-col gap-3 w-[15rem]'>
<TextInput id='email' required
label='Электронная почта (email)'
2023-12-08 19:58:16 +03:00
tooltip='электронная почта в корректном формате, например: i.petrov@mycompany.ru.com'
2023-12-08 00:33:47 +03:00
value={email}
onChange={event => setEmail(event.target.value)}
2023-07-15 17:46:19 +03:00
/>
2023-12-08 00:33:47 +03:00
<TextInput id='first_name'
label='Отображаемое имя'
value={firstName}
onChange={event => setFirstName(event.target.value)}
/>
<TextInput id='last_name'
label='Отображаемая фамилия'
value={lastName}
onChange={event => setLastName(event.target.value)}
2023-07-15 17:46:19 +03:00
/>
</div>
2023-12-08 00:33:47 +03:00
</div>
2023-12-17 20:19:28 +03:00
<div className='flex gap-1 text-sm'>
2023-12-08 00:33:47 +03:00
<Checkbox
label='Принимаю условия'
value={acceptPrivacy}
setValue={setAcceptPrivacy}
/>
<TextURL
text='обработки персональных данных...'
href={'/manuals?topic=privacy'}
/>
</div>
2023-12-17 20:19:28 +03:00
<div className='flex justify-around my-3'>
2023-12-08 00:33:47 +03:00
<SubmitButton
text='Регистрировать'
className='min-w-[10rem]'
2023-12-08 00:33:47 +03:00
loading={loading}
disabled={!acceptPrivacy}
/>
<Button
text='Назад'
className='min-w-[10rem]'
2023-12-08 00:33:47 +03:00
onClick={() => handleCancel()}
/>
</div>
{error ? <InfoError error={error} /> : null}
2023-12-08 00:33:47 +03:00
</form>);
2023-07-15 17:46:19 +03:00
}
export default RegisterPage;