forked from Saifullah-dev/react-file-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUploadItem.jsx
More file actions
202 lines (183 loc) · 5.4 KB
/
UploadItem.jsx
File metadata and controls
202 lines (183 loc) · 5.4 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
195
196
197
198
199
200
201
202
import { AiOutlineClose } from "react-icons/ai";
import Progress from "../../../components/Progress/Progress";
import { getFileExtension } from "../../../utils/getFileExtension";
import { useFileIcons } from "../../../hooks/useFileIcons";
import { FaRegFile } from "react-icons/fa6";
import { useEffect, useRef, useState } from "react";
import { getDataSize } from "../../../utils/getDataSize";
import { FaRegCheckCircle } from "react-icons/fa";
import { IoMdRefresh } from "react-icons/io";
import { useFiles } from "../../../contexts/FilesContext";
import {injectIntl} from "react-intl";
const UploadItem = ({
index,
fileData,
setFiles,
setIsUploading,
fileUploadConfig,
onFileUploaded,
handleFileRemove,
intl
}) => {
const [uploadProgress, setUploadProgress] = useState(0);
const [isUploaded, setIsUploaded] = useState(false);
const [isCanceled, setIsCanceled] = useState(false);
const [uploadFailed, setUploadFailed] = useState(false);
const fileIcons = useFileIcons(33);
const xhrRef = useRef();
const { onError } = useFiles();
const handleUploadError = (xhr) => {
setUploadProgress(0);
setIsUploading((prev) => ({
...prev,
[index]: false,
}));
const error = {
type: "upload",
message: intl.formatMessage({id: `uploadFail`}),
response: {
status: xhr.status,
statusText: xhr.statusText,
data: xhr.response,
},
};
setFiles((prev) =>
prev.map((file, i) => {
if (index === i) {
return {
...file,
error: error.message,
};
}
return file;
})
);
setUploadFailed(true);
onError(error, fileData.file);
};
const fileUpload = (fileData) => {
if (!!fileData.error) return;
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhrRef.current = xhr;
setIsUploading((prev) => ({
...prev,
[index]: true,
}));
xhr.upload.onprogress = (event) => {
if (event.lengthComputable) {
const progress = Math.round((event.loaded / event.total) * 100);
setUploadProgress(progress);
}
};
xhr.onload = () => {
setIsUploading((prev) => ({
...prev,
[index]: false,
}));
if (xhr.status === 200 || xhr.status === 201) {
setIsUploaded(true);
onFileUploaded(xhr.response);
resolve(xhr.response);
} else {
reject(xhr.statusText);
handleUploadError(xhr);
}
};
xhr.onerror = () => {
reject(xhr.statusText);
handleUploadError(xhr);
};
const method = fileUploadConfig?.method || "POST";
xhr.open(method, fileUploadConfig?.url, true);
const headers = fileUploadConfig?.headers;
for (let key in headers) {
xhr.setRequestHeader(key, headers[key]);
}
const formData = new FormData();
const appendData = fileData?.appendData;
for (let key in appendData) {
appendData[key] && formData.append(key, appendData[key]);
}
formData.append("file", fileData.file);
xhr.send(formData);
});
};
useEffect(() => {
// Prevent double uploads with strict mode
if (!xhrRef.current) {
fileUpload(fileData);
}
}, []);
const handleAbortUpload = () => {
if (xhrRef.current) {
xhrRef.current.abort();
setIsUploading((prev) => ({
...prev,
[index]: false,
}));
setIsCanceled(true);
setUploadProgress(0);
}
};
const handleRetry = () => {
if (fileData?.file) {
setFiles((prev) =>
prev.map((file, i) => {
if (index === i) {
return {
...file,
error: false,
};
}
return file;
})
);
fileUpload({ ...fileData, error: false });
setIsCanceled(false);
setUploadFailed(false);
}
};
// File was removed by the user beacuse it was unsupported or exceeds file size limit.
if (!!fileData.removed) {
return null;
}
//
return (
<li>
<div className="file-icon">
{fileIcons[getFileExtension(fileData.file?.name)] ?? <FaRegFile size={33} />}
</div>
<div className="file">
<div className="file-details">
<div className="file-info">
<span className="file-name text-truncate" title={fileData.file?.name}>
{fileData.file?.name}
</span>
<span className="file-size">{getDataSize(fileData.file?.size)}</span>
</div>
{isUploaded ? (
<FaRegCheckCircle title={intl.formatMessage({id: `uploaded`})} className="upload-success" />
) : isCanceled || uploadFailed ? (
<IoMdRefresh className="retry-upload" title="Retry" onClick={handleRetry} />
) : (
<div
className="rm-file"
title={`${!!fileData.error ? intl.formatMessage({id: `remove`}) : intl.formatMessage({id: `abortUpload`})}`}
onClick={!!fileData.error ? () => handleFileRemove(index) : handleAbortUpload}
>
<AiOutlineClose />
</div>
)}
</div>
<Progress
percent={uploadProgress}
isCanceled={isCanceled}
isCompleted={isUploaded}
error={fileData.error}
/>
</div>
</li>
);
};
export default injectIntl(UploadItem);