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

122 lines
4.0 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';
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() {
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
return (
<div className='flex justify-center w-full py-2'>
{user ? <b>{`Вы вошли в систему как ${user.username}`}</b> : null}
{!user ?
<Form
title='Регистрация'
onSubmit={handleSubmit}
dimensions='w-[24rem]'
>
<TextInput id='username' required
label='Имя пользователя'
value={username}
onChange={event => setUsername(event.target.value)}
/>
<TextInput id='password' type='password' required
label='Пароль'
value={password}
onChange={event => setPassword(event.target.value)}
/>
<TextInput id='password2' required type='password'
label='Повторите пароль'
value={password2}
onChange={event => setPassword2(event.target.value)}
/>
<div className='text-sm'>
<p>- используйте уникальный пароль</p>
<p>- портал функционирует в тестовом режиме</p>
<p className='font-semibold text-warning'>- безопасность информации не гарантируется</p>
{/* <p>- минимум 8 символов</p>
<p>- большие, маленькие буквы, цифры</p>
<p>- минимум 1 спец. символ</p> */}
</div>
<TextInput id='email' required
label='email'
value={email}
onChange={event => setEmail(event.target.value)}
/>
<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)}
/>
<div className='flex items-center justify-center w-full gap-4 my-4'>
<SubmitButton
text='Регистрировать'
dimensions='min-w-[10rem]'
loading={loading}
2023-07-15 17:46:19 +03:00
/>
<Button
text='Отмена'
dimensions='min-w-[10rem]'
onClick={() => handleCancel()}
2023-07-15 17:46:19 +03:00
/>
</div>
{error ? <BackendError error={error} /> : null}
</Form> : null}
</div>);
2023-07-15 17:46:19 +03:00
}
2023-07-25 20:27:29 +03:00
export default RegisterPage;