Projekt

Obecné

Profil

Stáhnout (2.65 KB) Statistiky
| Větev: | Tag: | Revize:
1
import 'antd/dist/antd.css';
2
import React, { useContext, useEffect, useState } from 'react';
3

    
4
import { useUnauthRedirect } from '../../hooks';
5
import { useRouter } from 'next/router';
6
import { Button, Space, Table, Typography } from 'antd';
7
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
8
import { faUsers } from '@fortawesome/free-solid-svg-icons';
9
import { LoggedUserContext } from '../../contexts/LoggedUserContext';
10
import { MainLayout } from '../../layouts/MainLayout';
11
import { UserInfo } from '../../api';
12
import { userController } from '../../controllers';
13
import AddUserModal from '../../components/modals/AddUserModal';
14

    
15
function UsersPage() {
16
    const redirecting = useUnauthRedirect('/login');
17
    const { logout, role } = useContext(LoggedUserContext);
18
    const router = useRouter();
19

    
20
    const [visible, setVisible] = React.useState(false);
21
    const [users, setUsers] = useState<UserInfo[]>([]);
22

    
23
    useEffect(() => {
24
        async function fetchData() {
25
            let usrs = (await userController.usersGet()).data.users;
26
            if (!usrs) {
27
                setUsers([]);
28
            } else {
29
                setUsers(usrs);
30
            }
31
        }
32

    
33
        if (!redirecting && role === 'ADMINISTRATOR') {
34
            fetchData();
35
        }
36
    }, [logout, redirecting, role, router]);
37

    
38
    const showModal = () => {
39
        setVisible(true);
40
    };
41

    
42
    const hideModal = () => {
43
        setVisible(false);
44
    };
45

    
46
    const columns = [
47
        { title: 'Jméno', dataIndex: 'name', key: 'name' },
48
        { title: 'Příjmení', dataIndex: 'surname', key: 'surname' },
49
        { title: 'Uživatelské jméno', dataIndex: 'username', key: 'username' },
50
        {
51
            title: '',
52
            key: 'operation',
53
            render: () => (
54
                <div>
55
                    <Space size={'middle'}>
56
                        <Button>Dokumenty</Button>
57
                        <Button>Upravit</Button>
58
                        <Button>Smazat</Button>
59
                    </Space>
60
                </div>
61
            ),
62
        },
63
    ];
64

    
65
    return redirecting || role !== 'ADMINISTRATOR' ? null : (
66
        <MainLayout>
67
            <Typography.Title level={2}>
68
                <FontAwesomeIcon icon={faUsers} /> Uživatelé
69
            </Typography.Title>
70
            <Button type={'primary'} onClick={showModal}>
71
                Přidat uživatele
72
            </Button>
73
            {visible && <AddUserModal onCancel={hideModal} />}
74

    
75
            <Table
76
                columns={columns}
77
                dataSource={users}
78
                size="small"
79
                scroll={{ y: 500 }}
80
            />
81
        </MainLayout>
82
    );
83
}
84

    
85
export default UsersPage;
    (1-1/1)