authscape 1.0.118 → 1.0.119

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "authscape",
3
- "version": "1.0.118",
3
+ "version": "1.0.119",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,346 @@
1
+ import React, { useState, useRef, useEffect } from "react";
2
+ import { apiService } from "authscape";
3
+ import LinearProgress from "@mui/material/LinearProgress";
4
+ import { Box } from "@mui/system";
5
+ import { Grid } from "@mui/material";
6
+ import FileCopyRoundedIcon from "@mui/icons-material/FileCopyRounded";
7
+ import UploadRoundedIcon from "@mui/icons-material/UploadRounded";
8
+ import Typography from "@mui/material/Typography";
9
+ import Stack from "@mui/material/Stack";
10
+ import FileDownloadOutlinedIcon from "@mui/icons-material/FileDownloadOutlined";
11
+ import IconButton from "@mui/material/IconButton";
12
+ import DeleteIcon from "@mui/icons-material/Delete";
13
+ import Button from "@mui/material/Button";
14
+ import Dialog from "@mui/material/Dialog";
15
+ import DialogActions from "@mui/material/DialogActions";
16
+ import DialogContent from "@mui/material/DialogContent";
17
+ import DialogContentText from "@mui/material/DialogContentText";
18
+ import DialogTitle from "@mui/material/DialogTitle";
19
+ import Tooltip from '@mui/material/Tooltip';
20
+ import Backdrop from '@mui/material/Backdrop';
21
+ import CircularProgress from '@mui/material/CircularProgress';
22
+
23
+ export default function FileUploader({
24
+ url,
25
+ params,
26
+ multiple = false,
27
+ fileLoaderUri = null,
28
+ children,
29
+ isHidden = false,
30
+ refOveride = null,
31
+ primaryColor = "#000",
32
+ deleteUri = "/Storage/RemoveFile?orderFileId="
33
+ }) {
34
+ // Declare a new state variable, which we'll call "count"
35
+ const [message, setMessage] = useState("");
36
+ const [loaded, setLoaded] = useState(0);
37
+ // const [selectedFile, setSelectedFile] = useState(null);
38
+ const [uploading, setUploading] = useState(false);
39
+
40
+ const [viewDeleteDialog, setViewDeleteDialog] = useState(false);
41
+
42
+ const [filesUploaded, setFilesUploaded] = useState([]);
43
+
44
+ const [orderFileId, setOrderFileId] = useState(null);
45
+
46
+ const [filesDownloadable, setFilesDownloadable] = useState(null);
47
+
48
+ const [parameters, setParameters] = useState([]);
49
+
50
+ const fileUploader = useRef();
51
+
52
+ const handleUpload = async (event) => {
53
+ let selectedFiles = event.target.files;
54
+
55
+ setLoaded(0);
56
+ setMessage(event.target.files[0] ? event.target.files[0].name : "");
57
+
58
+ if (uploading) return;
59
+ if (!selectedFiles) {
60
+ setMessage("Select a file first");
61
+ return;
62
+ }
63
+
64
+ setUploading(true);
65
+
66
+ let fileCount = selectedFiles.length;
67
+ let fileIndex = 1;
68
+
69
+ for (
70
+ let selectedIndex = 0;
71
+ selectedIndex < selectedFiles.length;
72
+ selectedIndex++
73
+ ) {
74
+ const selectedFile = selectedFiles[selectedIndex];
75
+
76
+ const data = new FormData();
77
+ data.append("file", selectedFile, selectedFile.name);
78
+
79
+ for (let index = 0; index < parameters.length; index++) {
80
+ const element = parameters[index];
81
+ data.append(element.key, element.value);
82
+ }
83
+
84
+ try {
85
+ await apiService().post(url, data, {
86
+ onUploadProgress: (ProgressEvent) => {
87
+ let loadedTotal = Math.round(
88
+ (ProgressEvent.loaded / ProgressEvent.total) * 100
89
+ );
90
+ let percent = (fileIndex / fileCount) * 100;
91
+ setLoaded(percent);
92
+
93
+ if (percent == 100) {
94
+ setTimeout(() => {
95
+ setLoaded(0);
96
+ setMessage("");
97
+ reloadFiles();
98
+ }, 2000);
99
+ }
100
+ },
101
+ });
102
+ } catch (err) {
103
+ setUploading(false);
104
+ setMessage("Failed to upload");
105
+ }
106
+
107
+ fileIndex++;
108
+ }
109
+
110
+ setUploading(false);
111
+ setMessage("Uploaded successfully");
112
+ };
113
+
114
+ const handleUploadFileInput = () => {
115
+ fileUploader.current.click();
116
+ };
117
+
118
+ useEffect(() => {
119
+ if (params != null) {
120
+ const propertyNames = Object.keys(params);
121
+ const propertyValues = Object.values(params);
122
+
123
+ let array = [];
124
+
125
+ for (let index = 0; index < propertyNames.length; index++) {
126
+ array.push({
127
+ key: propertyNames[0],
128
+ value: propertyValues[0],
129
+ });
130
+ }
131
+
132
+ setParameters(array);
133
+ }
134
+ }, [params]);
135
+
136
+ const reloadFiles = async () => {
137
+ let response = await apiService().get(fileLoaderUri);
138
+ if (response != null && response.status == 200) {
139
+ setFilesDownloadable(response.data);
140
+ }
141
+ };
142
+
143
+ useEffect(() => {
144
+ if (fileLoaderUri != null) {
145
+ const fetchAsync = async () => {
146
+ await reloadFiles();
147
+ };
148
+ fetchAsync();
149
+ }
150
+ }, [fileLoaderUri]);
151
+
152
+ return (
153
+ <Box sx={{ marginTop: 1, display: isHidden ? "none" : "block" }}>
154
+
155
+ <input
156
+ className="inputfile"
157
+ id="file"
158
+ type="file"
159
+ name="file"
160
+ multiple={multiple}
161
+ ref={fileUploader}
162
+ onChange={handleUpload}
163
+ style={{ display: "none" }}
164
+ />
165
+
166
+ <Grid container spacing={2}>
167
+ <Grid ref={refOveride} item xs={4} onClick={handleUploadFileInput}>
168
+ <Grid
169
+ container
170
+ sx={{
171
+ padding: 2,
172
+ backgroundColor: "#ECEDED",
173
+ fontSize: "14px",
174
+ cursor: "pointer",
175
+ borderRadius: "8px",
176
+ border: "1px dashed #C8D4D5",
177
+ }}
178
+ >
179
+ <Grid item xs={2}>
180
+ <Box>
181
+ <FileCopyRoundedIcon
182
+ sx={{ fill: "#C8D4D5", width: 50, height: 50 }}
183
+ />
184
+ </Box>
185
+ </Grid>
186
+ <Grid item xs={10}>
187
+ <Box>Drag and drop files here or</Box>
188
+ <Box sx={{ marginTop: 1 }}>
189
+ <Stack direction="row" spacing={2}>
190
+ <UploadRoundedIcon
191
+ sx={{ fill: primaryColor, width: 30, height: 30 }}
192
+ />
193
+ <Typography
194
+ variant="h3"
195
+ component={"span"}
196
+ sx={{ color: primaryColor, paddingTop: 0.6 }}
197
+ >
198
+ Upload
199
+ </Typography>
200
+ </Stack>
201
+ </Box>
202
+ </Grid>
203
+ <Grid item xs={12}>
204
+ {loaded > 0 && (
205
+ <LinearProgress
206
+ variant="buffer"
207
+ value={loaded}
208
+ sx={{ marginTop: 2 }}
209
+ />
210
+ )}
211
+
212
+ {loaded == 100 && (
213
+ <Typography
214
+ variant="h3"
215
+ component={"span"}
216
+ sx={{ color: primaryColor, paddingTop: 0.6 }}>
217
+ Completed
218
+ </Typography>
219
+ )}
220
+ </Grid>
221
+ </Grid>
222
+ </Grid>
223
+
224
+ {children}
225
+
226
+ <Grid container sx={{ paddingTop: 1 }}>
227
+ {filesDownloadable != null &&
228
+ filesDownloadable.map((fileUpload, idx) => {
229
+ return (
230
+ <Grid
231
+ key={"fileDownloadable-" + idx}
232
+ item
233
+ xs={8}
234
+ sm={8}
235
+ md={5}
236
+ lg={3}
237
+ sx={{
238
+ marginLeft: 2,
239
+ padding: 1,
240
+ marginTop: 1,
241
+ backgroundColor: "#ECEDED",
242
+ position: "relative",
243
+ fontSize: "14px",
244
+ cursor: "pointer",
245
+ borderRadius: "8px",
246
+ border: "1px solid #54C7DD",
247
+ }}
248
+ onClick={async () => {
249
+ window.open(fileUpload.uri);
250
+ }}
251
+ >
252
+ <Tooltip placement="left" arrow title={fileUpload.name}>
253
+ <Stack
254
+ direction="row"
255
+ spacing={1}
256
+ display="flex"
257
+ justifyContent={"space-between"}
258
+ >
259
+ <Box display={"flex"} alignItems="center">
260
+ <FileDownloadOutlinedIcon sx={{ fill: "#92D6E3" }} />
261
+ <Box
262
+ sx={{
263
+ paddingTop: 0.6,
264
+ marginLeft: "5px",
265
+ }}
266
+ >
267
+ <Typography
268
+ sx={{
269
+ overflow: "hidden",
270
+ whiteSpace: "nowrap",
271
+ textOverflow: "ellipsis",
272
+ width:"350px"
273
+ }}
274
+ >
275
+ {fileUpload.name}
276
+ </Typography>
277
+ </Box>
278
+
279
+ <IconButton
280
+ aria-label="delete"
281
+ sx={{ position: "absolute", right: "0" }}
282
+ onClick={(evt) => {
283
+ evt.stopPropagation();
284
+ setOrderFileId(fileUpload.id);
285
+ setViewDeleteDialog(true);
286
+ }}
287
+ >
288
+ <DeleteIcon />
289
+ </IconButton>
290
+ </Box>
291
+ </Stack>
292
+ </Tooltip>
293
+ </Grid>
294
+ );
295
+ })}
296
+ </Grid>
297
+ </Grid>
298
+
299
+ <Dialog
300
+ open={viewDeleteDialog}
301
+ fullWidth={true}
302
+ maxWidth={"xs"}
303
+ onClose={() => {
304
+ setViewDeleteDialog(false);
305
+ }}
306
+ aria-labelledby="alert-dialog-title"
307
+ aria-describedby="alert-dialog-description">
308
+ <DialogTitle id="alert-dialog-title">Remove File</DialogTitle>
309
+ <DialogContent>
310
+ <DialogContentText id="alert-dialog-description">
311
+ Are you sure you want to remove this file?
312
+ </DialogContentText>
313
+ </DialogContent>
314
+ <DialogActions>
315
+ <Button
316
+ onClick={() => {
317
+ setViewDeleteDialog(false);
318
+ }}
319
+ >
320
+ No
321
+ </Button>
322
+ <Button
323
+ onClick={async () => {
324
+
325
+ let response = await apiService().delete(
326
+ deleteUri + orderFileId
327
+ );
328
+ if (response != null && response.status == 200) {
329
+ setViewDeleteDialog(false);
330
+ await reloadFiles();
331
+ }
332
+
333
+ }}
334
+ autoFocus
335
+ >
336
+ Yes
337
+ </Button>
338
+ </DialogActions>
339
+ </Dialog>
340
+
341
+ <Backdrop sx={{ color: '#fff', zIndex: 99999 }} open={uploading}>
342
+ <CircularProgress color="inherit" />
343
+ </Backdrop>
344
+ </Box>
345
+ );
346
+ }