-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAdminDataImportPage.jsx
More file actions
194 lines (181 loc) · 7.58 KB
/
AdminDataImportPage.jsx
File metadata and controls
194 lines (181 loc) · 7.58 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* This program is part of the OpenLMIS logistics management information system platform software.
* Copyright © 2017 VillageReach
*
* This program is free software: you can redistribute it and/or modify it under the terms
* of the GNU Affero General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details. You should have received a copy of
* the GNU Affero General Public License along with this program. If not, see
* http://www.gnu.org/licenses. For additional information contact info@OpenLMIS.org.
*/
import React, { useState, useMemo } from 'react';
import { toast } from 'react-toastify';
import getService from '../../../react-components/utils/angular-utils';
import Select from '../../../react-components/inputs/select';
import Loading from '../../../react-components/modals/loading';
import { TYPE_OF_IMPORTS } from '../../consts';
import ImportSummaryModal from './ImportSummaryModal.jsx';
const AdminDataImportPage = () => {
const [typeOfImport, setTypeOfImport] = useState('');
const [selectedFile, setSelectedFile] = useState('');
const [displayLoading, setDisplayLoading] = useState(false);
const [isModalOpen, setIsModalOpen] = useState(false);
const [importModalContent, setImportModalContent] = useState([]);
const serverService = useMemo(
() => {
return getService('adminDataImport');
},
[]
);
const { formatMessage } = useMemo(() => getService('messageService'), []);
const importZip = () => {
if (selectedFile) {
setDisplayLoading(true);
serverService.importData(selectedFile)
.then((response) => {
if (response && response.results) {
setImportModalContent(response.results);
setIsModalOpen(true);
}
toast.success(formatMessage('admin.dataImport.toast.success'))
})
.catch((error) => {
console.error("Error caught:", error);
})
.finally(() => {
setSelectedFile('');
setTypeOfImport('');
setDisplayLoading(false);
});
}
}
const FileUploader = () => {
const hiddenFileInput = React.useRef(null);
const handleClick = () => {
hiddenFileInput.current.click();
};
const handleChange = event => {
const fileUploaded = event.target.files[0];
setSelectedFile(fileUploaded);
};
return (
<>
{!selectedFile &&
<>
<button
className='file-upload-button'
onClick={handleClick}
disabled={!typeOfImport}
>
{formatMessage('admin.dataImport.selectFile')}
</button>
<input
style={{ display: 'none' }}
ref={hiddenFileInput}
type='file'
onChange={handleChange}
disabled={!typeOfImport}
accept=".zip, .rar, .7z"
/>
</>
}
{selectedFile &&
<div className='input-import-file'>
<input
type='text'
value={selectedFile.name}
readOnly
/>
<button type='button'>
<i className="fa-duotone fa-xmark"></i>
<i
className='fa fa-times clear-icon clear clear-button'
onClick={() => setSelectedFile('')}
/>
</button>
</div>
}
</>
);
}
return (
<>
{
isModalOpen &&
<ImportSummaryModal
results={importModalContent}
/>
}
<div>
<h2 id='data-export-header'>
{formatMessage('admin.dataImport.label')}
</h2>
<div className='required' style={{ marginBottom: '4px', fontFamily: 'Arial', fontSize: '16px' }}>
<label id='type-header'>
{formatMessage('admin.dataImport.type')}
</label>
</div>
<div className='field-full-width' style={{ marginBottom: '16px', width: '40%' }}>
<Select
isTranslatable={true}
options={TYPE_OF_IMPORTS}
onChange={value => {
setSelectedFile('');
setTypeOfImport(value);
}}
value={typeOfImport}
/>
{TYPE_OF_IMPORTS.map(type => {
if (type.value === typeOfImport) {
return (
<div key={type.value}>
<p style={{ maxWidth: '544px' }} key={type.value}>{formatMessage(type.info)}</p>
{typeOfImport === 'User' && (
<p
style={{
color: '#cc0000',
fontWeight: 'bold',
marginTop: '12px',
maxWidth: '544px',
lineHeight: 1.4,
}}
>
{formatMessage(type.additionalInfo)}
</p>
)}
</div>
)
}
})}
</div>
<div className='required' style={{ marginBottom: '4px', fontFamily: 'Arial', fontSize: '16px' }}>
<label id='type-header'>
{formatMessage('admin.dataImport.zipFile')}
</label>
</div>
<div className='field-full-width' style={{ marginBottom: '16px', width: '40%' }}>
<FileUploader />
</div>
</div>
<div className="openlmis-toolbar">
<button
className='primary'
type='button'
style={{ marginTop: '0.5em' }}
disabled={selectedFile.length === 0}
onClick={importZip}
>
{formatMessage('admin.dataImport.import')}
</button>
</div>
{displayLoading &&
<Loading />
}
</>
)
};
export default AdminDataImportPage;