Hariwarshan
Hariwarshan

Reputation: 313

How to get the status of a promise from redux?

Am using redux in my react native application. for updating some Profile data, I am dispatching an action updateProfile(data) in one of my component

in my component

values = { // some Js object values }
updateProfile(values)

in actionCreator

export const updateProfile = (details) =>(dispatch) => {

dispatch(profileLoading()) 
axios.post(SERVERURL,details)  //updating the details to the server
  .then((result)=>{            
        dispatch(addProfile(result.data[0]))   //updating the returned details to redux state
      })
  .catch((err)=>{
        dispatch(profileFailed(err.response))
      })
    })
 
}

export const profileLoading = () => ({
    type: ActionTypes.PROFILE_LOADING,
})

export const addProfile = (profile) => ({
    type: ActionTypes.ADD_PROFILE,
    payload: profile
})

export const profileFailed = (err) => ({
    type: ActionTypes.PROFILE_FAILED,
    payload: err
})

profile.js

import * as ActionTypes from './ActionTypes'

export const profiles = (state = {
isLoading:true,
errMess:null,
profiles:{ }

},action) =>{
    switch(action.type){
        case ActionTypes.ADD_PROFILE:
            return {...state, isLoading:false, errMess:null, profiles: action.payload}
        
        case ActionTypes.PROFILE_LOADING:
            return {...state, isLoading:true,errMess:null}
        
        case ActionTypes.PROFILE_FAILED:
            return {...state, isLoading:false,errMess:action.payload, profiles: {}}

        case ActionTypes.DELETE_PROFILE:
            return {...state,isLoading:false,errMess:null,profiles:{}}

        default:
            return state
        }   
        
}

after this line updateProfile(values), currently am using setTimeout like below to know the outcome of the update

updateProfile(values)
setTimeout(()=>{
    if(profiles.errMess==null){
        setCondition('completed')
        nav.navigate('some Screen')
    }
    else{
        setCondition('error')
        }
},3000)

Since I need to navigate to other screen, I cant use SetTimeOut as it will constantly create a delay even if the update get completed quickly and if the update takes more than 3 seconds it will still cause a headache. What I want is, to navigate to 'some screen' only when data gets updated to the server(like a promise) Yes i can update the state values in redux state but i need to achieve it in component level. am new to redux. Please someone assist me.

Upvotes: 1

Views: 139

Answers (1)

Nitsew
Nitsew

Reputation: 3662

I believe you can return the axios request to have access to the promise where you call it.

You can see a discussion talking about the same challenge you face here: https://github.com/reduxjs/redux-thunk/issues/61

// Action Creator
export const updateProfile = (details) =>(dispatch) => {
  dispatch(profileLoading())

  return axios.post(SERVERURL,details)
    .then((result) => {            
      dispatch(addProfile(result.data[0]))
    }).catch((err) => {
      dispatch(profileFailed(err.response))
    })
  })
}


// Usage
dispatch(updateProfile(values)).then((response) => {
  // handle successful response
}).catch((err) => {
  // handle failure
})

Upvotes: 2

Related Questions