1 |
8c45ccb0
|
hrubyjar
|
import 'antd/dist/antd.css';
|
2 |
a5000e3c
|
Lukáš Vlček
|
import { Button, Form, Input } from 'antd';
|
3 |
|
|
import { LockOutlined, UserOutlined } from '@ant-design/icons';
|
4 |
8c45ccb0
|
hrubyjar
|
import { LoggedUserContext } from '../../contexts/LoggedUserContext';
|
5 |
51f70e00
|
Lukáš Vlček
|
import { LoginLayout } from '../../layouts/LoginLayout';
|
6 |
8c45ccb0
|
hrubyjar
|
import { useContext } from 'react';
|
7 |
|
|
import { useRouter } from 'next/router';
|
8 |
|
|
import { ShowToast } from '../../utils/alerts';
|
9 |
|
|
import { getTokenData } from '../../utils/login';
|
10 |
|
|
import { LoginResponse } from '../../api';
|
11 |
e9c60748
|
Dominik Poch
|
|
12 |
7a84a6d5
|
Dominik Poch
|
/**
|
13 |
|
|
* Creates a login screen.
|
14 |
|
|
* @returns Html structure of the login screen.
|
15 |
|
|
*/
|
16 |
e9c60748
|
Dominik Poch
|
function Login() {
|
17 |
8c45ccb0
|
hrubyjar
|
const { login, role } = useContext(LoggedUserContext);
|
18 |
|
|
const router = useRouter();
|
19 |
7f244939
|
Lukáš Vlček
|
|
20 |
7a84a6d5
|
Dominik Poch
|
/**
|
21 |
|
|
* Handles submission a form when its fields were successfully validated.
|
22 |
|
|
* @param values Fields of the login form.
|
23 |
|
|
*/
|
24 |
8c45ccb0
|
hrubyjar
|
const onFinish = async (values: any) => {
|
25 |
|
|
const loginRes = await login(values.username, values.password);
|
26 |
|
|
|
27 |
|
|
if (!loginRes) {
|
28 |
|
|
ShowToast(
|
29 |
|
|
'Chybně zadané heslo nebo uživatelské jméno',
|
30 |
|
|
'error',
|
31 |
|
|
3000,
|
32 |
|
|
'top-end'
|
33 |
|
|
);
|
34 |
|
|
} else {
|
35 |
|
|
let data: LoginResponse | null = await getTokenData();
|
36 |
|
|
ShowToast('Přihlášení proběhlo úspěšně', 'success', 3000, 'top-end');
|
37 |
|
|
if (data?.role === 'ADMINISTRATOR') {
|
38 |
|
|
await router.push('/documents/admin');
|
39 |
|
|
} else if (data?.role === 'ANNOTATOR') {
|
40 |
|
|
await router.push('/documents/annotator');
|
41 |
|
|
}
|
42 |
|
|
}
|
43 |
e9c60748
|
Dominik Poch
|
};
|
44 |
|
|
|
45 |
7a84a6d5
|
Dominik Poch
|
/**
|
46 |
|
|
* Handles submission a form when its validation failed.
|
47 |
|
|
* @param errorInfo Validation errors.
|
48 |
|
|
*/
|
49 |
e9c60748
|
Dominik Poch
|
const onFinishFailed = (errorInfo: any) => {
|
50 |
8c45ccb0
|
hrubyjar
|
ShowToast('Zadané údaje nejsou validní', 'error', 3000, 'top-end');
|
51 |
e9c60748
|
Dominik Poch
|
};
|
52 |
|
|
|
53 |
|
|
return (
|
54 |
77eb11b3
|
Dominik Poch
|
<LoginLayout>
|
55 |
|
|
<Form
|
56 |
|
|
name="login"
|
57 |
|
|
onFinish={onFinish}
|
58 |
|
|
onFinishFailed={onFinishFailed}
|
59 |
|
|
autoComplete="off"
|
60 |
e9c60748
|
Dominik Poch
|
>
|
61 |
77eb11b3
|
Dominik Poch
|
<Form.Item
|
62 |
2d3822e7
|
Dominik Poch
|
name="username"
|
63 |
cbd9de5e
|
Dominik Poch
|
rules={[
|
64 |
|
|
{
|
65 |
|
|
required: true,
|
66 |
2d3822e7
|
Dominik Poch
|
message: 'Prosím zadejte své uživatelské jméno',
|
67 |
cbd9de5e
|
Dominik Poch
|
},
|
68 |
|
|
]}
|
69 |
77eb11b3
|
Dominik Poch
|
>
|
70 |
|
|
<Input
|
71 |
|
|
prefix={<UserOutlined className="site-form-item-icon" />}
|
72 |
2d3822e7
|
Dominik Poch
|
placeholder="Uživatelské jméno"
|
73 |
77eb11b3
|
Dominik Poch
|
/>
|
74 |
|
|
</Form.Item>
|
75 |
e9c60748
|
Dominik Poch
|
|
76 |
77eb11b3
|
Dominik Poch
|
<Form.Item
|
77 |
|
|
name="password"
|
78 |
cbd9de5e
|
Dominik Poch
|
rules={[{ required: true, message: 'Prosím zadejte své heslo' }]}
|
79 |
77eb11b3
|
Dominik Poch
|
>
|
80 |
|
|
<Input.Password
|
81 |
|
|
prefix={<LockOutlined className="site-form-item-icon" />}
|
82 |
|
|
placeholder="Heslo"
|
83 |
|
|
/>
|
84 |
|
|
</Form.Item>
|
85 |
|
|
<Form.Item>
|
86 |
882caa6a
|
Dominik Poch
|
<Button type="primary" htmlType="submit" className="w-100">
|
87 |
77eb11b3
|
Dominik Poch
|
Přihlásit
|
88 |
|
|
</Button>
|
89 |
|
|
</Form.Item>
|
90 |
|
|
</Form>
|
91 |
|
|
</LoginLayout>
|
92 |
e9c60748
|
Dominik Poch
|
);
|
93 |
|
|
}
|
94 |
|
|
|
95 |
|
|
export default Login;
|