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';
|
2023-07-26 23:11:00 +03:00
|
|
|
|
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-26 02:24:16 +03:00
|
|
|
|
import Button from '../components/common/Button';
|
|
|
|
|
import Form from '../components/common/Form';
|
|
|
|
|
import SubmitButton from '../components/common/SubmitButton';
|
|
|
|
|
import TextInput from '../components/common/TextInput';
|
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-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-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() {
|
2023-09-03 18:26:50 +03:00
|
|
|
|
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
|
|
|
|
};
|
2023-07-26 23:11:00 +03:00
|
|
|
|
signup(data, createdUser => {
|
2023-09-05 00:23:53 +03:00
|
|
|
|
navigateTo(`/login?username=${createdUser.username}`);
|
2023-07-26 23:11:00 +03:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
return (
|
2023-09-19 17:54:48 +03:00
|
|
|
|
<div className='flex justify-center w-full py-2'>
|
2023-07-26 23:11:00 +03:00
|
|
|
|
{ user &&
|
2023-10-26 11:50:18 +03:00
|
|
|
|
<b>{`Вы вошли в систему как ${user.username}`}</b>}
|
2023-07-26 23:11:00 +03:00
|
|
|
|
{ !user &&
|
2023-08-27 15:39:49 +03:00
|
|
|
|
<Form
|
2023-09-02 01:11:27 +03:00
|
|
|
|
title='Регистрация'
|
2023-08-27 15:39:49 +03:00
|
|
|
|
onSubmit={handleSubmit}
|
2023-09-15 23:29:52 +03:00
|
|
|
|
dimensions='w-[24rem]'
|
2023-08-27 15:39:49 +03:00
|
|
|
|
>
|
2023-07-15 17:46:19 +03:00
|
|
|
|
<TextInput id='username' label='Имя пользователя' type='text'
|
|
|
|
|
required
|
|
|
|
|
value={username}
|
2023-08-27 16:35:17 +03:00
|
|
|
|
onChange={event => setUsername(event.target.value)}
|
2023-07-15 17:46:19 +03:00
|
|
|
|
/>
|
2023-07-25 20:27:29 +03:00
|
|
|
|
<TextInput id='password' label='Пароль' type='password'
|
2023-07-15 17:46:19 +03:00
|
|
|
|
required
|
|
|
|
|
value={password}
|
2023-08-27 16:35:17 +03:00
|
|
|
|
onChange={event => setPassword(event.target.value)}
|
2023-07-15 17:46:19 +03:00
|
|
|
|
/>
|
|
|
|
|
<TextInput id='password2' label='Повторите пароль' type='password'
|
|
|
|
|
required
|
|
|
|
|
value={password2}
|
2023-08-27 16:35:17 +03:00
|
|
|
|
onChange={event => setPassword2(event.target.value)}
|
2023-07-15 17:46:19 +03:00
|
|
|
|
/>
|
|
|
|
|
<div className='text-sm'>
|
2023-08-22 20:29:07 +03:00
|
|
|
|
<p>- используйте уникальный пароль</p>
|
|
|
|
|
<p>- портал функционирует в тестовом режиме</p>
|
2023-09-03 18:26:50 +03:00
|
|
|
|
<p className='font-semibold text-warning'>- безопасность информации не гарантируется</p>
|
2023-08-22 20:29:07 +03:00
|
|
|
|
{/* <p>- минимум 8 символов</p>
|
2023-07-15 17:46:19 +03:00
|
|
|
|
<p>- большие, маленькие буквы, цифры</p>
|
2023-08-22 20:29:07 +03:00
|
|
|
|
<p>- минимум 1 спец. символ</p> */}
|
2023-07-15 17:46:19 +03:00
|
|
|
|
</div>
|
|
|
|
|
<TextInput id='email' label='email' type='text'
|
|
|
|
|
required
|
|
|
|
|
value={email}
|
2023-08-27 16:35:17 +03:00
|
|
|
|
onChange={event => setEmail(event.target.value)}
|
2023-07-15 17:46:19 +03:00
|
|
|
|
/>
|
|
|
|
|
<TextInput id='first_name' label='Имя' type='text'
|
|
|
|
|
value={firstName}
|
2023-08-27 16:35:17 +03:00
|
|
|
|
onChange={event => setFirstName(event.target.value)}
|
2023-07-15 17:46:19 +03:00
|
|
|
|
/>
|
|
|
|
|
<TextInput id='last_name' label='Фамилия' type='text'
|
|
|
|
|
value={lastName}
|
2023-08-27 16:35:17 +03:00
|
|
|
|
onChange={event => setLastName(event.target.value)}
|
2023-07-15 17:46:19 +03:00
|
|
|
|
/>
|
|
|
|
|
|
2023-09-02 01:11:27 +03:00
|
|
|
|
<div className='flex items-center justify-center w-full gap-4 my-4'>
|
|
|
|
|
<SubmitButton
|
|
|
|
|
text='Регистрировать'
|
|
|
|
|
loading={loading}
|
2023-09-15 23:29:52 +03:00
|
|
|
|
dimensions='min-w-[10rem]'
|
2023-09-02 01:11:27 +03:00
|
|
|
|
/>
|
|
|
|
|
<Button
|
|
|
|
|
text='Отмена'
|
|
|
|
|
onClick={() => handleCancel()}
|
2023-09-15 23:29:52 +03:00
|
|
|
|
dimensions='min-w-[10rem]'
|
2023-09-02 01:11:27 +03:00
|
|
|
|
/>
|
2023-07-15 17:46:19 +03:00
|
|
|
|
</div>
|
|
|
|
|
{ error && <BackendError error={error} />}
|
|
|
|
|
</Form>
|
|
|
|
|
}
|
2023-07-15 17:57:25 +03:00
|
|
|
|
</div>
|
2023-07-15 17:46:19 +03:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-25 20:27:29 +03:00
|
|
|
|
export default RegisterPage;
|