Projekt

Obecné

Profil

Stáhnout (3.84 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { Button, Modal, Upload } from 'antd';
2
import { InboxOutlined } from '@ant-design/icons';
3
import 'antd/dist/antd.css';
4
import React from 'react';
5
import { DocumentAddInfo, EAddDocumentFormat } from '../../api';
6
import { documentController } from '../../controllers';
7
import { ShowToast } from '../../utils/alerts';
8

    
9
const { Dragger } = Upload;
10

    
11
interface ModalProps {
12
    onCancel: () => void;
13
}
14

    
15
/**
16
 * Creates a modal window that loads documents to the app.
17
 * @returns The modal window.
18
 */
19
const AddDocumentModal: React.FC<ModalProps> = ({ onCancel }) => {
20
    /**
21
     * Handles successful closing of the modal window.
22
     */
23
    const handleOk = () => {
24
        onCancel();
25
    };
26

    
27
    /**
28
     * Handles cancelling of the model window.
29
     */
30
    const handleCancel = () => {
31
        onCancel();
32
    };
33

    
34
    const [upload, setUpload] = React.useState(false);
35
    const [tempDocs, setTempDocs] = React.useState<DocumentAddInfo[]>([]);
36

    
37
    const handleUpload = () => {
38
        if (tempDocs.length !== 0) {
39
            setUpload(true);
40
            documentController.documentsPost({ documents: tempDocs }).then((r) => {
41
                ShowToast(
42
                    'Nahrávání souborů proběhlo úspěšně',
43
                    'success',
44
                    3000,
45
                    'top-end'
46
                );
47
                setUpload(false);
48
                onCancel();
49
            });
50
        }
51
    };
52
    /**
53
     * Settings of a file loader.
54
     */
55
    const props = {
56
        name: 'file',
57
        multiple: true,
58
        directory: false,
59
        onRemove: (file: any) => {
60
            let docInfo = tempDocs.find((doc) => {
61
                return doc.name === file.name;
62
            });
63
            if (docInfo) {
64
                const index = tempDocs.indexOf(docInfo);
65
                const newFileList = tempDocs.slice();
66
                newFileList.splice(index, 1);
67
                setTempDocs(newFileList);
68
            }
69
        },
70
        beforeUpload: (file: any) => {
71
            let docInfo: DocumentAddInfo = { name: file.name };
72
            if ('application/x-zip-compressed' === file.type) {
73
                docInfo.format = EAddDocumentFormat.Zip;
74
            } else if ('text/html' === file.type || 'text/plain' === file.type) {
75
                docInfo.format = EAddDocumentFormat.Textfile;
76
            }
77

    
78
            const reader = new FileReader();
79
            reader.onload = (e) => {
80
                if (e.target && e.target.result) {
81
                    let content = e.target.result.toString();
82
                    docInfo.content = content.split('base64,').pop();
83
                }
84
            };
85
            reader.readAsDataURL(file);
86
            tempDocs.push(docInfo);
87
        },
88
    };
89

    
90
    return (
91
        <Modal
92
            title="Nový dokument"
93
            onOk={handleOk}
94
            visible={true}
95
            onCancel={handleCancel}
96
            footer={[
97
                <Button key="back" onClick={handleCancel}>
98
                    Storno
99
                </Button>,
100
                <Button
101
                    key="submit"
102
                    type="primary"
103
                    onClick={handleUpload}
104
                    loading={upload}
105
                >
106
                    Nahrát
107
                </Button>,
108
            ]}
109
        >
110
            <Dragger {...props} accept={'.txt, .zip, .html'} maxCount={10}>
111
                <p className="ant-upload-drag-icon">
112
                    <InboxOutlined />
113
                </p>
114
                <p className="ant-upload-text">
115
                    Soubory lze nahrát stisknutím nebo přetažením...
116
                </p>
117
            </Dragger>
118
            <p>
119
                Aplikace podporuje textové soubory (ve formátu .txt a .html), případně
120
                .zip obsahující takovéto soubory v <b>kořenovém</b> adresáři
121
            </p>
122
        </Modal>
123
    );
124
};
125

    
126
export default AddDocumentModal;
(1-1/11)