Skip to content

Commit

Permalink
fix: progress in correctly set
Browse files Browse the repository at this point in the history
  • Loading branch information
awgaan authored and varl committed Mar 23, 2020
1 parent 2ec221a commit 6398c92
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/utils/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ const uploadFile = (
file,
format,
type,
setLoading,
setProgress,
setAlerts,
addEntry
) => {
setLoading(true);
setProgress(1);
const errorHandler = e => {
console.error('sendFile error: ', e);
setAlerts([
Expand All @@ -134,7 +134,7 @@ const uploadFile = (
),
},
]);
setLoading(false);
setProgress(0);
};

try {
Expand Down Expand Up @@ -173,6 +173,7 @@ const uploadFile = (
if (id == -1) {
errorHandler(msg);
}
setProgress(0);
},
e => {
let message = i18n.t('An unknown error occurred');
Expand All @@ -181,11 +182,12 @@ const uploadFile = (
message = response.message;
} catch (e2) {}
errorHandler(message);
setProgress(0);
},
setProgress,
format
);
xhr.send(file);
setLoading(false);
} catch (e) {
errorHandler(e);
}
Expand Down
15 changes: 12 additions & 3 deletions src/utils/xhr.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { getMimeType } from './mime';

const getUploadXHR = (url, upload, type, onResponse, onError, format) => {
const getUploadXHR = (
url,
upload,
type,
onResponse,
onError,
setProgress,
format
) => {
const xhr = new XMLHttpRequest();
const contentType = getMimeType(format);

Expand All @@ -13,7 +21,7 @@ const getUploadXHR = (url, upload, type, onResponse, onError, format) => {
);

xhr.onreadystatechange = onReadyStateChange(xhr, type, onResponse, onError);
xhr.upload.onprogress = onProgress;
xhr.upload.onprogress = onProgress(setProgress);
return xhr;
};

Expand Down Expand Up @@ -65,10 +73,11 @@ const extractIdAndMessage = (xhr, importType) => {
return { id: -1, msg: undefined };
};

const onProgress = evt => {
const onProgress = setProgress => evt => {
if (evt.lengthComputable) {
const percentComplete = parseInt((evt.loaded / evt.total) * 100);
const stats = { ...evt, percentComplete };
setProgress(Math.max(1, percentComplete));
}
};

Expand Down

0 comments on commit 6398c92

Please sign in to comment.