Projekt

Obecné

Profil

Stáhnout (5.72 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { Badge, Button, Checkbox, Dropdown, List, Modal, Space, Table } from 'antd';
2
import 'antd/dist/antd.css';
3
import React, { useContext, useEffect, useState } from 'react';
4
import { DocumentListInfo, UserInfo } from '../../api';
5
import {
6
    annotationController,
7
    documentController,
8
    userController,
9
} from '../../controllers';
10
import { useUnauthRedirect } from '../../hooks';
11
import { LoggedUserContext } from '../../contexts/LoggedUserContext';
12
import { ShowToast } from '../../utils/alerts';
13
import { getColumnSearchProps, getLocaleProps } from '../../utils/tableUtils';
14
import { TableUserInfo } from '../types/TableUserInfo';
15
import { TableDocInfo } from '../types/TableDocInfo';
16

    
17
interface ModalProps {
18
    onCancel: () => void;
19
    documentsIds: (string | undefined)[];
20
}
21

    
22
function AssignDocumentModal({ onCancel, documentsIds }: ModalProps) {
23
    const redirecting = useUnauthRedirect('/login');
24
    const { role } = useContext(LoggedUserContext);
25
    const [users, setUsers] = useState<TableUserInfo[]>([]);
26
    const [selectedUsers, setSelectedUsers] = useState<string[]>([]);
27

    
28
    const handleOk = () => {
29
        onCancel();
30
    };
31

    
32
    const handleCancel = () => {
33
        onCancel();
34
    };
35

    
36
    async function fetchData() {
37
        if (!redirecting && role === 'ADMINISTRATOR') {
38
            const usrs = (await userController.usersGet()).data.users;
39
            // @ts-ignore
40
            const tableUsers: TableUserInfo[] = usrs?.map((usr, index) => {
41
                return { key: index, ...usr };
42
            });
43
            setUsers(tableUsers || []);
44
        }
45
    }
46

    
47
    useEffect(() => {
48
        fetchData();
49
    }, [documentsIds, fetchData, redirecting, role]);
50

    
51
    const handleUpload = async () => {
52
        // console.log(documentsIds, selectedUsers);
53
        if (documentsIds && documentsIds.length !== 0) {
54
            await annotationController
55
                .annotationsPost({
56
                    // @ts-ignore
57
                    documentIdList: documentsIds,
58
                    userIdList: selectedUsers,
59
                })
60
                .then((r) => {
61
                    ShowToast(
62
                        'Přiřazení dokumentů proběhlo úspěšně',
63
                        'success',
64
                        3000,
65
                        'top-end'
66
                    );
67
                    onCancel();
68
                });
69
        }
70
    };
71

    
72
    const rowSelection = {
73
        onChange: (selectedRowKeys: React.Key[], selectedRows: UserInfo[]) => {
74
            // @ts-ignore
75
            setSelectedUsers(selectedRows.map((row) => row.id));
76
        },
77
    };
78

    
79
    const columns = [
80
        {
81
            title: 'Jméno',
82
            dataIndex: 'name',
83
            key: 'name',
84
            ...getColumnSearchProps('name', 'jméno'),
85
            // @ts-ignore
86
            sorter: (a, b) => a.name.localeCompare(b.name),
87
        },
88
        {
89
            title: 'Příjmení',
90
            dataIndex: 'surname',
91
            key: 'surname',
92
            ...getColumnSearchProps('surname', 'příjmení'),
93
            // @ts-ignore
94
            sorter: (a, b) => a.surname.localeCompare(b.surname),
95
        },
96
        {
97
            title: 'Uživatelské jméno',
98
            dataIndex: 'username',
99
            key: 'username',
100
            ...getColumnSearchProps('username', 'uživatelské jméno'),
101
            // @ts-ignore
102
            sorter: (a, b) => a.username.localeCompare(b.username),
103
        },
104
        {
105
            title: 'Role',
106
            dataIndex: 'role',
107
            key: 'role',
108
            filters: [
109
                {
110
                    text: 'Administrátor',
111
                    value: 'ADMINISTRATOR',
112
                },
113
                {
114
                    text: 'Anotátor',
115
                    value: 'ANNOTATOR',
116
                },
117
            ],
118
            // @ts-ignore
119
            onFilter: (value, record) => record.role.indexOf(value) === 0,
120
            // @ts-ignore
121
            sorter: (a, b) => a.username.localeCompare(b.username),
122
            render: (role: string) => {
123
                let label = '';
124
                if (role === 'ANNOTATOR') {
125
                    label = 'Anotátor';
126
                }
127
                if (role === 'ADMINISTRATOR') {
128
                    label = 'Administrátor';
129
                }
130
                return label;
131
            },
132
        },
133
        {
134
            title: 'Přiřazeno dokumentů',
135
            key: 'docCounts',
136
            align: 'center' as 'center',
137
            dataIndex: 'assignedDocumentsCount',
138
            // @ts-ignore
139
            render: (assignedDocumentsCount) => (
140
                <div>
141
                    <Space size={'middle'}>
142
                        <Badge count={assignedDocumentsCount} />
143
                    </Space>
144
                </div>
145
            ),
146
            // @ts-ignore
147
            sorter: (a, b) => a.assignedDocumentsCount - b.assignedDocumentsCount,
148
        },
149
    ];
150

    
151
    return (
152
        <Modal
153
            title="Přiřadit dokumenty"
154
            onOk={handleOk}
155
            visible={true}
156
            onCancel={handleCancel}
157
            width={1000}
158
            footer={[
159
                <Button key="back" onClick={handleCancel}>
160
                    Storno
161
                </Button>,
162
                <Button key="submit" type="primary" onClick={handleUpload}>
163
                    Přiřadit
164
                </Button>,
165
            ]}
166
        >
167
            <Table
168
                locale={{ ...getLocaleProps() }}
169
                rowSelection={{
170
                    type: 'checkbox',
171
                    ...rowSelection,
172
                }}
173
                // @ts-ignore
174
                columns={columns}
175
                dataSource={users}
176
                pagination={false}
177
                scroll={{ y: 400 }}
178
            />
179
        </Modal>
180
    );
181
}
182

    
183
export default AssignDocumentModal;
(3-3/11)