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

163 lines
5.5 KiB
TypeScript
Raw Normal View History

2023-07-15 17:46:19 +03:00
import { useEffect, useState } from 'react';
2023-09-05 00:23:53 +03:00
import { useLocation } from 'react-router-dom';
import { toast } from 'react-toastify';
2023-07-15 17:46:19 +03:00
2023-07-25 20:27:29 +03:00
import BackendError from '../components/BackendError';
2023-11-27 12:11:39 +03:00
import Button from '../components/Common/Button';
2023-12-07 23:08:49 +03:00
import Checkbox from '../components/Common/Checkbox';
2023-12-08 00:33:47 +03:00
import ConceptTooltip from '../components/Common/ConceptTooltip';
import Overlay from '../components/Common/Overlay';
2023-11-27 12:11:39 +03:00
import SubmitButton from '../components/Common/SubmitButton';
import TextInput from '../components/Common/TextInput';
2023-12-07 23:08:49 +03:00
import TextURL from '../components/Common/TextURL';
2023-12-08 00:33:47 +03:00
import ExpectedAnonymous from '../components/ExpectedAnonymous';
import { HelpIcon } from '../components/Icons';
2023-07-25 20:27:29 +03:00
import { useAuth } from '../context/AuthContext';
2023-09-05 00:23:53 +03:00
import { useConceptNavigation } from '../context/NagivationContext';
2023-09-11 20:31:54 +03:00
import { type IUserSignupData } from '../models/library';
2023-12-08 19:58:16 +03:00
import { globalIDs, patterns } from '../utils/constants';
2023-07-15 17:46:19 +03:00
function RegisterPage() {
2023-09-02 01:11:27 +03:00
const location = useLocation();
2023-09-05 00:23:53 +03:00
const { navigateTo, navigateHistory } = 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 (location.key !== 'default') {
2023-09-05 00:23:53 +03:00
navigateHistory(-1);
2023-09-02 01:11:27 +03:00
} else {
2023-09-05 00:23:53 +03:00
navigateTo('/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 => {
2023-09-05 00:23:53 +03:00
navigateTo(`/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'
>
<HelpIcon color='text-primary' size={5} />
</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}
dimensions='w-[12rem]'
onChange={event => setUsername(event.target.value)}
/>
<TextInput id='password' type='password' required
label='Пароль'
dimensions='w-[15rem]'
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='Повторите пароль'
dimensions='w-[15rem]'
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>
<div className='flex text-sm'>
<Checkbox
label='Принимаю условия'
value={acceptPrivacy}
setValue={setAcceptPrivacy}
/>
<TextURL
text='обработки персональных данных...'
href={'/manuals?topic=privacy'}
/>
</div>
<div className='flex items-center justify-around w-full my-3'>
<SubmitButton
text='Регистрировать'
dimensions='min-w-[10rem]'
loading={loading}
disabled={!acceptPrivacy}
/>
<Button
text='Назад'
dimensions='min-w-[10rem]'
onClick={() => handleCancel()}
/>
</div>
{error ? <BackendError error={error} /> : null}
</form>);
2023-07-15 17:46:19 +03:00
}
2023-07-25 20:27:29 +03:00
export default RegisterPage;