svrcekmichal / redux-axios-middleware

Redux middleware for fetching data with axios HTTP client
MIT License
920 stars 96 forks source link

No action after request finished. #61

Closed alparslanahmed closed 7 years ago

alparslanahmed commented 7 years ago

When i dispatch action with type and request everything seems ok. I am transforming request with transformRequest and after request finish onSuccess and onError methods working well. But there is no action fires with '_SUCCESS' or '_ERROR' suffix. What could be my mistake?

return {
        type: types.POST_LOGIN,
        payload: {
            request: {
                url: '/users/login',
                method: 'POST',
                data: { username, password }
            }
        }
    }

onSuccess: function (app) {

        if (!app.response.data.Response && app.response.data.Header && app.response.data.Header.errors) {
            return app.dispatch(actions.apiError(app.response.data.Header.errors));
        }

        app.dispatch(actions.ajaxReponse(app.response.data));
    }

action Object {type: "AJAX_RESPONSE", data: Object}

nmaves commented 7 years ago

Can you explain what you are trying to accomplish? Why do you want to override the onSuccess() handler?

alparslanahmed commented 7 years ago

@nmaves i wished to handle API errors from single function this means i don't need to execute notification function for showing notification to user on every action. I just wanted to write clean code. I don't know this is a proper solution but i fixed it for my code like this:

onSuccess: function ({ action, next, response, error, dispatch }, options) {

        if (!response.data.Response && response.data.Header && response.data.Header.errors) {
            dispatch(actions.apiError(response.data.Header.errors));
        } else {
            dispatch(actions.ajaxReponse(response.data));
        }

        dispatch({
            type: action.type + types._SUCCESS,
            payload: response.data.Response
        });
    }