Handling promises / callbacks in the Redux store

I am currently using action creators with Redux to dispatch user actions (such as fetching account information). These action creators return promises from an API request that, when they resolve, updates the store.

The problem I'm running into is that I want to "intercept" certain API responses, in my case, an invalid JWT token. In such cases, I want to keep the pending promise, request a login modem, and if the login is successful, try again and resolve the promise.

It is enough for me to check the response after the call fetch

like this:

Root.js - Place a modal component in the DOM

export default (props) => {
    return (
        <div>
            <Modal />
            <App />
        </div>
    )
}

      

Modal.js - handle different modal types (Approach accepted by this post )

const MODALS = {
    LOGIN: LoginModal
}

const ModalRoot = ({type, props}) => {
    if (!MODALS[type]) return null;

    return <Modal {...props} />;
}

export default connect(
    state => state.modal
)(ModalRoot);

      

reducer.js - Modal reducer

const initialState = {type: null, props: {}}
export default function modal(state = initialState, action) {
    switch (action.type) {
        case 'SHOW_MODAL':
            return Object.assign({}, state, {
                type: action.modalType,
                props: {...action.props}
            });
        case 'HIDE_MODAL':
            return initialState;
        default:
            return state;
    }
}

      

actions.js - return a promise to receive a call

const promptLogin = () => (dispatch, getState) => {
    return new Promise((resolve, reject) => {
        return dispatch(showModal('LOGIN', {
            successCallback: (user) => resolve(user)
        }));
    })
}

      

makeAPIRequest.js - make an API call, handle an invalid token

fetch(endpoint, requestConfig)
    .then(res => res.json())
    .then(data => {

        /** Invalid JWT Token */
        if (data && data.error_code === 403) {
            return store.dispatch(promptLogin())
                .then(token => {
                    return makeRequest(method, endpoint, headers, body)
                });
        }

        return data;
    });

      

The problem with this approach is that I keep the callback function (to complete the original request) in the store, which is not recommended in the redux docs.

If my Modal component is not connected to my fetch logic, and I cannot keep the Promise or callback in state (because they are not serializable), how can I continue the promise from the original request after the user logs in from the modal?

+3
javascript reactjs redux


source to share


No one has answered this question yet

See similar questions:

212
How can I display a modal dialog in Redux that does asynchronous actions?

or similar:

2350
Storing objects in HTML5 localStorage
1126
Why use Redux over Facebook Flux?
804
How do I dispatch a Redux action with a timeout?
567
Why would we use async thread middleware in Redux?
nine
dispx-thunk delivery method fired undefined action
five
Redux thunk: return promise from dispatched action
3
What is the point of Redux Promise and Redux Promise Middleware?
3
Sharing a promise (in Redux state).
0
How to handle return type dispatch with Redux Promise middleware?
0
Redux promise don't resolve promise when there is a number in action



All Articles
Loading...
X
Show
Funny
Dev
Pics