Projekt

Obecné

Profil

Stáhnout (4.62 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, Table, Tag, 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 { AnnotationListInfo, DocumentListInfo, UserInfo } from '../../../api';
13
import { documentController, userController } from '../../../controllers';
14
import AssignDocumentModal from '../../../components/modals/AssignDocumentModal';
15
import { ShowToast } from '../../../utils/alerts';
16
import { TableDocInfo } from '../../../components/types/TableDocInfo';
17

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

    
25
    const [documents, setDocuments] = useState<TableDocInfo[]>([]);
26
    const [selectedDocs, setSelectedDocs] = useState<string[] | undefined[]>([]);
27

    
28
    async function fetchData() {
29
        const docs = (await documentController.documentsGet(0, 1000)).data.documents;
30

    
31
        // @ts-ignore
32
        const tableDocs: TableDocInfo[] = docs?.map((doc, index) => {
33
            return { key: index, ...doc };
34
        });
35

    
36
        if (!docs) {
37
            setDocuments([]);
38
        } else {
39
            setDocuments(tableDocs);
40
        }
41
    }
42

    
43
    useEffect(() => {
44
        if (!redirecting && role === 'ADMINISTRATOR') {
45
            fetchData();
46
        }
47
    }, [logout, redirecting, role, router]);
48

    
49
    const showAssignModal = () => {
50
        if (selectedDocs.length == 0) {
51
            ShowToast('Vyberte dokument pro přiřazení', 'warning', 3000, 'top-end');
52
        } else {
53
            setVisibleAssign(true);
54
        }
55
    };
56

    
57
    const showAddModal = () => {
58
        setVisibleAdd(true);
59
    };
60

    
61
    const hideModal = () => {
62
        fetchData();
63
        setVisibleAdd(false);
64
        setVisibleAssign(false);
65
    };
66

    
67
    const columns = [
68
        {
69
            title: 'Název dokumentu',
70
            dataIndex: 'name',
71
            key: 'name',
72
        },
73
        {
74
            title: 'Délka',
75
            dataIndex: 'length',
76
            key: 'length',
77
        },
78
        {
79
            title: 'Anotátoři',
80
            dataIndex: 'annotatingUsers',
81
            key: 'annotatingUsers',
82
            render: (columnData: UserInfo[], record: DocumentListInfo, index: number) => {
83
                return (
84
                    <div>
85
                        {columnData.map((e) => (
86
                            <span
87
                                key={e.username + '.' + record.id}
88
                                title={e.username ?? ''}
89
                            >
90
                                <FontAwesomeIcon
91
                                    icon={faUser}
92
                                    title={e.username ?? ''}
93
                                    className={'me-2'}
94
                                />
95
                            </span>
96
                        ))}
97
                    </div>
98
                );
99
            },
100
        },
101
    ];
102

    
103
    const rowSelection = {
104
        onChange: (selectedRowKeys: React.Key[], selectedRows: DocumentListInfo[]) => {
105
            // @ts-ignore
106
            setSelectedDocs(selectedRows.map((row) => row.id));
107
        },
108
    };
109

    
110
    return redirecting || role !== 'ADMINISTRATOR' ? null : (
111
        <MainLayout>
112
            <Typography.Title level={2}>
113
                <FontAwesomeIcon icon={faFileLines} /> Dokumenty
114
            </Typography.Title>
115
            <Button type={'primary'} onClick={showAddModal}>
116
                Nahrát dokument
117
            </Button>
118
            <Button onClick={showAssignModal}>Přiřadit dokumenty</Button>
119
            {visibleAdd && <AddDocumentModal onCancel={hideModal} />}
120
            {visibleAssign && (
121
                <AssignDocumentModal documentsIds={selectedDocs} onCancel={hideModal} />
122
            )}
123

    
124
            <Table
125
                rowSelection={{
126
                    type: 'checkbox',
127
                    ...rowSelection,
128
                }}
129
                columns={columns}
130
                dataSource={documents}
131
                size="middle"
132
                scroll={{ y: 600 }}
133
            />
134
        </MainLayout>
135
    );
136
}
137

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