Projekt

Obecné

Profil

Stáhnout (6.8 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 { faFileLines, faUser } from '@fortawesome/free-solid-svg-icons';
8
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
9
import { LoggedUserContext } from '../../../contexts/LoggedUserContext';
10
import { MainLayout } from '../../../layouts/MainLayout';
11
import AddDocumentModal from '../../../components/modals/AddDocumentModal';
12
import { DocumentListInfo, UserInfo } from '../../../api';
13
import { documentController, userController } from '../../../controllers';
14
import AssignDocumentModal from '../../../components/modals/AssignDocumentModal';
15
import { ShowConfirmDelete, ShowToast } from '../../../utils/alerts';
16
import { TableDocInfo } from '../../../components/types/TableDocInfo';
17
import { UserFilter } from '../../../components/types/UserFilter';
18
import { getColumnSearchProps, getLocaleProps } from '../../../utils/tableUtils';
19
import { UserOutlined } from '@ant-design/icons';
20
import Swal from 'sweetalert2';
21

    
22
function AdminDocumentPage() {
23
    const redirecting = useUnauthRedirect('/login');
24
    const { logout, role } = useContext(LoggedUserContext);
25
    const [visibleAdd, setVisibleAdd] = React.useState(false);
26
    const [visibleAssign, setVisibleAssign] = React.useState(false);
27
    const router = useRouter();
28

    
29
    const [documents, setDocuments] = useState<TableDocInfo[]>([]);
30
    const [userFilters, setUserFilters] = useState<UserFilter[]>([]);
31
    const [selectedDocs, setSelectedDocs] = useState<string[] | undefined[]>([]);
32

    
33
    async function fetchData() {
34
        const docs = (await documentController.documentsGet(0, 1000)).data.documents;
35
        // @ts-ignore
36
        const tableDocs: TableDocInfo[] = docs?.map((doc, index) => {
37
            return { key: index, ...doc };
38
        });
39

    
40
        const users = (await userController.usersGet()).data.users;
41
        // @ts-ignore
42
        const filters: UserFilter[] = users?.map((user) => {
43
            return {
44
                text: user.name + ' ' + user.surname,
45
                value: user.username,
46
            };
47
        });
48
        setUserFilters(filters);
49

    
50
        if (!docs) {
51
            setDocuments([]);
52
        } else {
53
            setDocuments(tableDocs);
54
        }
55
    }
56

    
57
    useEffect(() => {
58
        if (!redirecting && role === 'ADMINISTRATOR') {
59
            fetchData();
60
        }
61
    }, [logout, redirecting, role, router]);
62

    
63
    const showAssignModal = () => {
64
        if (selectedDocs.length == 0) {
65
            ShowToast('Vyberte dokument pro přiřazení', 'warning', 3000, 'top-end');
66
        } else {
67
            setVisibleAssign(true);
68
        }
69
    };
70

    
71
    const showAddModal = () => {
72
        setVisibleAdd(true);
73
    };
74

    
75
    const hideModal = () => {
76
        fetchData();
77
        setVisibleAdd(false);
78
        setVisibleAssign(false);
79
    };
80

    
81
    const removeUserDocument = (userId: string, documentId: string) => {
82
        Swal.fire({
83
            title: 'Opravdu si přejete uživateli odebrat dokument?',
84
            showCancelButton: true,
85
            confirmButtonText: 'Odebrat',
86
        }).then((result) => {
87
            // TODO call API
88
        });
89
    };
90

    
91
    const columns = [
92
        {
93
            title: 'Název dokumentu',
94
            dataIndex: 'name',
95
            key: 'name',
96
            ...getColumnSearchProps('name', 'název'),
97
            sorter: {
98
                // @ts-ignore
99
                compare: (a, b) => a.name.localeCompare(b.name),
100
                multiple: 2,
101
            },
102
        },
103
        {
104
            title: 'Délka',
105
            dataIndex: 'length',
106
            key: 'length',
107
            sorter: {
108
                // @ts-ignore
109
                compare: (a, b) => a.length - b.length,
110
                multiple: 1,
111
            },
112
        },
113
        {
114
            title: 'Anotátoři',
115
            dataIndex: 'annotatingUsers',
116
            key: 'annotatingUsers',
117
            render: (columnData: UserInfo[], record: DocumentListInfo, index: number) => {
118
                return (
119
                    <div>
120
                        <Space>
121
                            {columnData.map((e) => (
122
                                <span
123
                                    key={e.username + '.' + record.id}
124
                                    title={e.username ?? ''}
125
                                >
126
                                    <FontAwesomeIcon
127
                                        icon={faUser}
128
                                        size={'2x'}
129
                                        onClick={() =>
130
                                            // @ts-ignore
131
                                            removeUserDocument(e.id, record.id)
132
                                        }
133
                                        title={e.username ?? ''}
134
                                        className={'me-2'}
135
                                    />
136
                                </span>
137
                            ))}
138
                        </Space>
139
                    </div>
140
                );
141
            },
142
            filters: userFilters,
143
            filterSearch: true,
144
            // @ts-ignore
145
            onFilter: (value, record) =>
146
                // @ts-ignore
147
                record.annotatingUsers.find((user) => user['username'] === value),
148
            sorter: {
149
                // @ts-ignore
150
                compare: (a, b) => a.annotatingUsers.length - b.annotatingUsers.length,
151
                multiple: 3,
152
            },
153
        },
154
    ];
155

    
156
    const rowSelection = {
157
        onChange: (selectedRowKeys: React.Key[], selectedRows: DocumentListInfo[]) => {
158
            // @ts-ignore
159
            setSelectedDocs(selectedRows.map((row) => row.id));
160
        },
161
    };
162

    
163
    return redirecting || role !== 'ADMINISTRATOR' ? null : (
164
        <MainLayout>
165
            <Typography.Title level={2}>
166
                <FontAwesomeIcon icon={faFileLines} /> Dokumenty
167
            </Typography.Title>
168
            <Button type={'primary'} onClick={showAddModal}>
169
                Nahrát dokument
170
            </Button>
171
            <Button onClick={showAssignModal}>Přiřadit dokumenty</Button>
172
            {visibleAdd && <AddDocumentModal onCancel={hideModal} />}
173
            {visibleAssign && (
174
                <AssignDocumentModal documentsIds={selectedDocs} onCancel={hideModal} />
175
            )}
176

    
177
            <Table
178
                locale={{ ...getLocaleProps() }}
179
                rowSelection={{
180
                    type: 'checkbox',
181
                    ...rowSelection,
182
                }}
183
                // @ts-ignore
184
                columns={columns}
185
                dataSource={documents}
186
                size="middle"
187
                scroll={{ y: 600 }}
188
            />
189
        </MainLayout>
190
    );
191
}
192

    
193
export default AdminDocumentPage;
    (1-1/1)