-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataFilesDownloadMessageModal.jsx
More file actions
154 lines (144 loc) · 4.85 KB
/
DataFilesDownloadMessageModal.jsx
File metadata and controls
154 lines (144 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import React from 'react';
import { useSelector, useDispatch, shallowEqual } from 'react-redux';
import { Modal, ModalHeader, ModalBody, ModalFooter, Input } from 'reactstrap';
import { Button, FormField, InlineMessage, SectionMessage } from '_common';
import { useHistory, useLocation } from 'react-router-dom';
import { Formik, Form } from 'formik';
import * as yup from 'yup';
import { useCompress } from 'hooks/datafiles/mutations';
import styles from './DataFilesCompressModal.module.scss';
const DataFilesDownloadMessageModal = () => {
const history = useHistory();
const location = useLocation();
const dispatch = useDispatch();
const { compress, status, setStatus } = useCompress();
const isOpen = useSelector((state) => state.files.modals.downloadMessage);
const selectedFiles = useSelector(
({ files: { selected, listing } }) =>
selected.FilesListing.map((i) => ({
...listing.FilesListing[i],
})),
shallowEqual
);
const onClosed = () => {
dispatch({ type: 'DATA_FILES_MODAL_CLOSE' });
if (status) {
setStatus({});
history.push(location.pathname);
}
};
const compressCallback = ({ filenameDisplay, compressionType }) => {
compress({
filename: filenameDisplay,
files: selectedFiles,
compressionType,
fromDownload: true,
});
};
const initialValues = {
filenameDisplay:
selectedFiles[0] && selectedFiles.length === 1
? selectedFiles[0].name
: `Archive_${new Date().toISOString().split('.')[0]}`,
compressionType: 'zip',
};
const validationSchema = yup.object().shape({
filenameDisplay: yup
.string()
.trim('The filename cannot include leading and trailing spaces')
.strict(true)
.required('The filename is required'),
});
const toggle = () => {
dispatch({
type: 'DATA_FILES_TOGGLE_MODAL',
payload: { operation: 'downloadMessage', props: {} },
});
};
return (
<Modal
isOpen={isOpen}
onClosed={onClosed}
toggle={toggle}
size="md"
className="dataFilesModal"
>
<ModalHeader toggle={toggle} charCode="">
Download
</ModalHeader>
<Formik
initialValues={initialValues}
validationSchema={validationSchema}
onSubmit={compressCallback}
>
{({ setFieldValue, values, isValid }) => {
const handleSelectChange = (e) => {
setFieldValue('compressionType', e.target.value);
};
const formDisabled =
status.type === 'RUNNING' || status.type === 'SUCCESS';
const buttonDisabled =
formDisabled || !isValid || values.filenameDisplay === '';
return (
<Form>
<ModalBody>
<SectionMessage type="warning">
Folders and multiple files must be compressed before
downloading.
</SectionMessage>
<FormField
label="Compressed File Name"
name="filenameDisplay"
disabled={formDisabled}
addonType="append"
addon={
<Input
type="select"
name="compressionType"
bsSize="sm"
onChange={handleSelectChange}
disabled={formDisabled}
className={styles['bg-color']}
>
<option value="zip">.zip</option>
<option value="tgz">.tar.gz</option>
</Input>
}
/>
<p>
<em>
A job to compress the folder(s) and/or files will be
submitted. The compressed file will appear in this directory
for you to download.
</em>
</p>
</ModalBody>
<ModalFooter>
<InlineMessage
isVisible={status.type === 'SUCCESS'}
type="success"
>
Successfully started compress job
</InlineMessage>
{status.type === 'ERROR' && status.message && (
<InlineMessage type="error">{status.message}</InlineMessage>
)}
<Button
disabled={buttonDisabled}
type="primary"
size={status.type === 'ERROR' ? 'long' : 'medium'}
isLoading={status.type === 'RUNNING'}
iconNameBefore={status.type === 'ERROR' ? 'alert' : null}
attr="submit"
>
Compress
</Button>
</ModalFooter>
</Form>
);
}}
</Formik>
</Modal>
);
};
export default DataFilesDownloadMessageModal;