3
\$\begingroup\$

I'm trying to dispatch to my reducers the current status of my request. So far I'm wondering how refined the status publishing of my thunk action.

I feel like my code is very verbose and I wonder if I can reduce the line quantity of it.

I inform my store that the request have reached some step by dispatching a corresponding status.

Here my code :

// set the status available for my code with function arguments planned
export const postFileStatus = [  
    { type: 'POST_FILE',status: request},
    { type: 'POST_FILE', status: success, response: res },
    { type: 'POST_FILE', status: error, error: err }

]

// dispatch the thunk to the redux middleware
const thunkFile = () => dispatch => {

// inform the request have started by retrieve the "request" status on my file
{ dispatch(postFileStatus.find(step => step.status === "request"));
 Axios.post("http://localhost:7500/api/files", {
      "word": state.word,
      "data": state.base64Data,
      "id" : state.wordId
    }, 
    { "Content-Type":"multipart/form-data"}
  )

  // inform the request have succeed by retrieve the "success" status on my file
  .then(res =>  {dispatch(postFileStatus.find(step => step.status === "success"))
  console.log(res) })
  .catch(err => {  

    // inform the request have failed by retrieve the "error" status on my file
    dispatch(postFileStatus.find(step => step.status === "error"))
    console.log(" error :", error.response) 
});

}
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.