From 75f0e9ed5ad6f12359af8e649aec935e7fd27687 Mon Sep 17 00:00:00 2001
From: Bogdan Cochior <bogdan.cochior@thinslices.com>
Date: Tue, 12 Jun 2018 09:31:25 +0300
Subject: [PATCH] fix(upload): fix remove file issue

---
 .../src/components/Files/Files.js             | 24 ++++++++++---------
 .../components-faraday/src/redux/files.js     | 12 +++++++---
 2 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/packages/components-faraday/src/components/Files/Files.js b/packages/components-faraday/src/components/Files/Files.js
index 3bf85519b..19bead1fe 100644
--- a/packages/components-faraday/src/components/Files/Files.js
+++ b/packages/components-faraday/src/components/Files/Files.js
@@ -34,7 +34,7 @@ const Files = ({
   dropSortableFile,
 }) => (
   <div>
-    <Error show={error}>Error uploading file, please try again.</Error>
+    <Error show={error}> File error, please try again.</Error>
     <FileSection
       addFile={addFile('manuscripts')}
       allowedFileExtensions={['pdf', 'doc', 'docx']}
@@ -97,6 +97,7 @@ export default compose(
   lifecycle({
     componentDidMount() {
       const { version: { files }, setFiles } = this.props
+      console.log('----------------', files)
       setFiles(prev => ({
         manuscripts: get(files, 'manuscripts') || [],
         coverLetter: get(files, 'coverLetter') || [],
@@ -149,9 +150,7 @@ export default compose(
             [type]: [...files[type], file],
           }
           setFiles(newFiles)
-          setTimeout(() => {
-            changeForm('wizard', 'files', newFiles)
-          }, 1000)
+          changeForm('wizard', 'files', newFiles)
         })
         .catch(e => console.error(`Couldn't upload file.`, e))
     },
@@ -173,13 +172,16 @@ export default compose(
       version,
     }) => type => id => e => {
       e.preventDefault()
-      deleteFile(id)
-      const newFiles = {
-        ...files,
-        [type]: files[type].filter(f => f.id !== id),
-      }
-      setFiles(newFiles)
-      changeForm('wizard', 'files', files)
+      deleteFile(id, type)
+        .then(() => {
+          const newFiles = {
+            ...files,
+            [type]: files[type].filter(f => f.id !== id),
+          }
+          setFiles(newFiles)
+          changeForm('wizard', 'files', newFiles)
+        })
+        .catch(e => console.error(`Couldn't delete file.`, e))
     },
   }),
   withContext(
diff --git a/packages/components-faraday/src/redux/files.js b/packages/components-faraday/src/redux/files.js
index a296178e9..63a616ed5 100644
--- a/packages/components-faraday/src/redux/files.js
+++ b/packages/components-faraday/src/redux/files.js
@@ -80,14 +80,17 @@ export const uploadFile = (file, type, fragmentId) => dispatch => {
   )
 }
 
-export const deleteFile = fileId => dispatch => {
-  dispatch(removeRequest())
+export const deleteFile = (fileId, type = 'manuscripts') => dispatch => {
+  dispatch(removeRequest(type))
   return remove(`/files/${fileId}`)
     .then(r => {
       dispatch(removeSuccess())
       return r
     })
-    .catch(err => dispatch(removeFailure(err.message)))
+    .catch(err => {
+      dispatch(removeFailure(err.message))
+      throw err
+    })
 }
 
 export const getSignedUrl = fileId => dispatch => get(`/files/${fileId}`)
@@ -95,6 +98,7 @@ export const getSignedUrl = fileId => dispatch => get(`/files/${fileId}`)
 // reducer
 export default (state = initialState, action) => {
   switch (action.type) {
+    case REMOVE_REQUEST:
     case UPLOAD_REQUEST:
       return {
         ...state,
@@ -105,12 +109,14 @@ export default (state = initialState, action) => {
         },
       }
     case UPLOAD_FAILURE:
+    case REMOVE_FAILURE:
       return {
         ...state,
         isFetching: initialState.isFetching,
         error: action.error,
       }
     case UPLOAD_SUCCESS:
+    case REMOVE_SUCCESS:
       return {
         ...state,
         isFetching: initialState.isFetching,
-- 
GitLab