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

122 lines
4.3 KiB
TypeScript
Raw Normal View History

2023-07-15 17:46:19 +03:00
import { useEffect, useState } from 'react';
2023-09-02 01:11:27 +03:00
import { useLocation, useNavigate } 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-09-02 01:11:27 +03:00
import Button from '../components/Common/Button';
2023-07-15 17:46:19 +03:00
import Form from '../components/Common/Form';
import SubmitButton from '../components/Common/SubmitButton';
2023-07-25 20:27:29 +03:00
import TextInput from '../components/Common/TextInput';
import { useAuth } from '../context/AuthContext';
import { type IUserSignupData } from '../utils/models';
2023-07-15 17:46:19 +03:00
function RegisterPage() {
2023-09-02 01:11:27 +03:00
const location = useLocation();
const navigate = useNavigate();
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() {
if (location.key !== 'default') {
2023-09-02 01:11:27 +03:00
navigate(-1);
} else {
navigate('/library');
}
}
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 => {
navigate(`/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
return (
2023-07-25 20:27:29 +03:00
<div className='w-full py-2'>
{ user &&
2023-08-10 14:30:20 +03:00
<b>{`Вы вошли в систему как ${user.username}. Если хотите зарегистрировать нового пользователя, выйдите из системы (меню в правом верхнем углу экрана)`}</b>}
{ !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}
widthClass='w-[24rem]'
>
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'>
<p>- используйте уникальный пароль</p>
<p>- портал функционирует в тестовом режиме</p>
<p className='font-semibold text-warning'>- безопасность информации не гарантируется</p>
{/* <p>- минимум 8 символов</p>
2023-07-15 17:46:19 +03:00
<p>- большие, маленькие буквы, цифры</p>
<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}
widthClass='min-w-[10rem]'
/>
<Button
text='Отмена'
onClick={() => handleCancel()}
widthClass='min-w-[10rem]'
/>
2023-07-15 17:46:19 +03:00
</div>
{ error && <BackendError error={error} />}
</Form>
}
</div>
2023-07-15 17:46:19 +03:00
);
}
2023-07-25 20:27:29 +03:00
export default RegisterPage;