Revize 4a7bae81
Přidáno uživatelem Jaroslav Hrubý před téměř 3 roky(ů)
webapp/components/modals/AddDocument.tsx | ||
---|---|---|
1 |
import { message, Button, Modal, Upload } from 'antd'; |
|
2 |
import { InboxOutlined } from '@ant-design/icons'; |
|
3 |
import 'antd/dist/antd.css'; |
|
4 |
|
|
5 |
const { Dragger } = Upload; |
|
6 |
|
|
7 |
/** |
|
8 |
* Creates a modal window that loads documents to the app. |
|
9 |
* @returns The modal window. |
|
10 |
*/ |
|
11 |
export function AddDocument(afterClose: () => {}) { |
|
12 |
/** |
|
13 |
* Settings of a file loader. |
|
14 |
*/ |
|
15 |
const props = { |
|
16 |
name: 'file', |
|
17 |
multiple: true, |
|
18 |
directory: true, |
|
19 |
/** |
|
20 |
@todo: Probably will be needed to change the uploading URL |
|
21 |
**/ |
|
22 |
action: 'https://www.mocky.io/v2/5cc8019d300000980a055e76', |
|
23 |
onChange(info: any) { |
|
24 |
const { status } = info.file; |
|
25 |
|
|
26 |
if (status !== 'uploading') { |
|
27 |
console.log(info.file, info.fileList); |
|
28 |
} |
|
29 |
|
|
30 |
if (status === 'done') { |
|
31 |
message.success(`${info.file.name} file uploaded successfully.`); |
|
32 |
} else if (status === 'error') { |
|
33 |
message.error(`${info.file.name} file upload failed.`); |
|
34 |
} |
|
35 |
}, |
|
36 |
onDrop(e: any) { |
|
37 |
console.log('Dropped files', e.dataTransfer.files); |
|
38 |
}, |
|
39 |
}; |
|
40 |
|
|
41 |
/** |
|
42 |
* Handles successful closing of the modal window. |
|
43 |
*/ |
|
44 |
const handleOk = () => { |
|
45 |
/** |
|
46 |
@todo: Send documents to a server when all files are loaded |
|
47 |
**/ |
|
48 |
console.log('Document is loaded'); |
|
49 |
afterClose(); |
|
50 |
}; |
|
51 |
|
|
52 |
/** |
|
53 |
* Handles cancelling of the model window. |
|
54 |
*/ |
|
55 |
const handleCancel = () => { |
|
56 |
console.log('Loading of documents is cancelled.'); |
|
57 |
afterClose(); |
|
58 |
}; |
|
59 |
|
|
60 |
return ( |
|
61 |
<Modal |
|
62 |
title="Nový dokument" |
|
63 |
onOk={handleOk} |
|
64 |
onCancel={handleCancel} |
|
65 |
footer={[ |
|
66 |
<Button key="back" onClick={handleCancel}> |
|
67 |
Storno |
|
68 |
</Button>, |
|
69 |
<Button key="submit" type="primary" onClick={handleOk}> |
|
70 |
Nahrát |
|
71 |
</Button>, |
|
72 |
]} |
|
73 |
> |
|
74 |
<Dragger> |
|
75 |
<p className="ant-upload-drag-icon"> |
|
76 |
<InboxOutlined /> |
|
77 |
</p> |
|
78 |
<p className="ant-upload-text"> |
|
79 |
Soubory lze nahrát stisknutím nebo přetažením... |
|
80 |
</p> |
|
81 |
</Dragger> |
|
82 |
</Modal> |
|
83 |
); |
|
84 |
} |
webapp/components/modals/AddDocumentModal.tsx | ||
---|---|---|
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 |
|
|
8 |
const { Dragger } = Upload; |
|
9 |
|
|
10 |
interface ModalProps { |
|
11 |
onCancel: () => void; |
|
12 |
} |
|
13 |
|
|
14 |
/** |
|
15 |
* Creates a modal window that loads documents to the app. |
|
16 |
* @returns The modal window. |
|
17 |
*/ |
|
18 |
const AddDocumentModal: React.FC<ModalProps> = ({ onCancel }) => { |
|
19 |
/** |
|
20 |
* Handles successful closing of the modal window. |
|
21 |
*/ |
|
22 |
const handleOk = () => { |
|
23 |
onCancel(); |
|
24 |
}; |
|
25 |
|
|
26 |
/** |
|
27 |
* Handles cancelling of the model window. |
|
28 |
*/ |
|
29 |
const handleCancel = () => { |
|
30 |
onCancel(); |
|
31 |
}; |
|
32 |
|
|
33 |
const [upload, setUpload] = React.useState(false); |
|
34 |
const tempDocs: DocumentAddInfo[] = []; |
|
35 |
|
|
36 |
const handleUpload = () => { |
|
37 |
if (tempDocs.length !== 0) { |
|
38 |
setUpload(true); |
|
39 |
documentController.documentsPost({ documents: tempDocs }); |
|
40 |
setUpload(false); |
|
41 |
} |
|
42 |
onCancel(); |
|
43 |
}; |
|
44 |
/** |
|
45 |
* Settings of a file loader. |
|
46 |
*/ |
|
47 |
const props = { |
|
48 |
name: 'file', |
|
49 |
multiple: true, |
|
50 |
directory: false, |
|
51 |
onRemove: (file: any) => { |
|
52 |
let docInfo = tempDocs.find((doc) => { |
|
53 |
return doc.name === file.name; |
|
54 |
}); |
|
55 |
if (docInfo) { |
|
56 |
const index = tempDocs.indexOf(docInfo); |
|
57 |
const newFileList = tempDocs.slice(); |
|
58 |
newFileList.splice(index, 1); |
|
59 |
} |
|
60 |
}, |
|
61 |
beforeUpload: (file: any) => { |
|
62 |
let docInfo: DocumentAddInfo = { name: file.name }; |
|
63 |
if ('application/x-zip-compressed' === file.type) { |
|
64 |
docInfo.format = EAddDocumentFormat.Zip; |
|
65 |
} else if ('text/html' === file.type || 'text/plain' === file.type) { |
|
66 |
docInfo.format = EAddDocumentFormat.Textfile; |
|
67 |
} |
|
68 |
|
|
69 |
const reader = new FileReader(); |
|
70 |
reader.onload = (e) => { |
|
71 |
if (e.target && e.target.result) { |
|
72 |
let content = e.target.result.toString(); |
|
73 |
docInfo.content = content.split('base64,').pop(); |
|
74 |
} |
|
75 |
}; |
|
76 |
reader.readAsDataURL(file); |
|
77 |
tempDocs.push(docInfo); |
|
78 |
}, |
|
79 |
}; |
|
80 |
|
|
81 |
return ( |
|
82 |
<Modal |
|
83 |
title="Nový dokument" |
|
84 |
onOk={handleOk} |
|
85 |
visible={true} |
|
86 |
onCancel={handleCancel} |
|
87 |
footer={[ |
|
88 |
<Button key="back" onClick={handleCancel}> |
|
89 |
Storno |
|
90 |
</Button>, |
|
91 |
<Button |
|
92 |
key="submit" |
|
93 |
type="primary" |
|
94 |
onClick={handleUpload} |
|
95 |
loading={upload} |
|
96 |
> |
|
97 |
Nahrát |
|
98 |
</Button>, |
|
99 |
]} |
|
100 |
> |
|
101 |
<Dragger {...props} accept={'.txt, .zip, .html'}> |
|
102 |
<p className="ant-upload-drag-icon"> |
|
103 |
<InboxOutlined /> |
|
104 |
</p> |
|
105 |
<p className="ant-upload-text"> |
|
106 |
Soubory lze nahrát stisknutím nebo přetažením... |
|
107 |
</p> |
|
108 |
</Dragger> |
|
109 |
</Modal> |
|
110 |
); |
|
111 |
}; |
|
112 |
|
|
113 |
export default AddDocumentModal; |
webapp/pages/documents/admin/index.tsx | ||
---|---|---|
8 | 8 |
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; |
9 | 9 |
import { LoggedUserContext } from '../../../contexts/LoggedUserContext'; |
10 | 10 |
import { MainLayout } from '../../../layouts/MainLayout'; |
11 |
import AddDocumentModal from '../../../components/modals/AddDocumentModal'; |
|
11 | 12 |
|
12 | 13 |
function AdminDocumentPage() { |
13 | 14 |
const redirecting = useUnauthRedirect('/login'); |
14 | 15 |
const { logout, role } = useContext(LoggedUserContext); |
16 |
const [visible, setVisible] = React.useState(false); |
|
15 | 17 |
const router = useRouter(); |
16 | 18 |
|
17 | 19 |
useEffect(() => { |
... | ... | |
21 | 23 |
}, [logout, redirecting, role, router]); |
22 | 24 |
|
23 | 25 |
const showModal = () => { |
24 |
// TODO show AddDocument component |
|
26 |
setVisible(true); |
|
27 |
}; |
|
28 |
|
|
29 |
const hideModal = () => { |
|
30 |
setVisible(false); |
|
25 | 31 |
}; |
26 | 32 |
|
27 | 33 |
return redirecting || role !== 'ADMINISTRATOR' ? null : ( |
... | ... | |
32 | 38 |
<Button type={'primary'} onClick={showModal}> |
33 | 39 |
Nahrát dokument |
34 | 40 |
</Button> |
41 |
{visible && <AddDocumentModal onCancel={hideModal} />} |
|
35 | 42 |
</MainLayout> |
36 | 43 |
); |
37 | 44 |
} |
Také k dispozici: Unified diff
Documents upload implemented, connected to REST API