Projekt

Obecné

Profil

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