From 7ef0e832b5c5a83d116fc660c433d82f9c893739 Mon Sep 17 00:00:00 2001
From: eug-vs <eug-vs@keemail.me>
Date: Mon, 17 Aug 2020 19:24:45 +0300
Subject: fix: limit uploadProgress until request resolved

---
 src/hooks/useS3Preupload.tsx | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

(limited to 'src/hooks/useS3Preupload.tsx')

diff --git a/src/hooks/useS3Preupload.tsx b/src/hooks/useS3Preupload.tsx
index 3c98e9a..3545cd5 100644
--- a/src/hooks/useS3Preupload.tsx
+++ b/src/hooks/useS3Preupload.tsx
@@ -33,7 +33,8 @@ export default (): Hook => {
   }, [setUrl, setFile]);
 
   const handleUploadProgress = useCallback((progressEvent: ProgressEvent): void => {
-    setProgress(Math.round((progressEvent.loaded * 100) / progressEvent.total));
+    // Only allow upload progress reach 95%, and set 100% when request is resolved
+    setProgress(Math.round((progressEvent.loaded * 95) / progressEvent.total));
   }, [setProgress]);
 
   const resolve = useCallback(async (): Promise<string> => {
@@ -43,10 +44,12 @@ export default (): Hook => {
         onUploadProgress: handleUploadProgress
       };
 
+      setProgress(0.01);
       return get('/files')
         .then(response => response.data)
         .then(uploadUrl => axios.put(uploadUrl, file, config))
         .then(response => {
+          setProgress(100);
           const uri = response.config.url;
           return uri ? uri.slice(0, uri.indexOf('?')) : '';
         });
-- 
cgit v1.2.3

