Skip to content

Instantly share code, notes, and snippets.

@mattiamanzati
Created October 9, 2015 18:04
Show Gist options
  • Save mattiamanzati/1f22549e60d58178f3b8 to your computer and use it in GitHub Desktop.
Save mattiamanzati/1f22549e60d58178f3b8 to your computer and use it in GitHub Desktop.
// utility functions for the authLogin
function authLoginStart(){
return {
type: AUTH_LOGIN_START
};
}
function authLoginSuccess(user){
return {
type: AUTH_LOGIN_SUCCESS,
user
};
}
function authLoginError(errors){
return {
type: AUTH_LOGIN_ERROR,
errors
};
}
// actual auth login
function authLoginRest(data){
return (dispatch) => {
// starts the dispatching action
dispatch(authLoginStart());
// performs the login
return login(data)
.then(
(user) => {
dispatch(authLoginSuccess(user));
dispatch(authSetToken(user.token));
dispatch(authSetUser(user));
return user;
},
(errors) => {
dispatch(authLoginError(errors));
return Promise.reject(errors);
}
);
};
}
function authLoginSubmit(formName, data){
return dispatch => {
// start submitting the form
dispatch(startSubmit(formName));
// performs the actual login <=== COMPOSITION IS HERE
return authLoginRest(data)(dispatch)
.then(
(user) => {
dispatch(stopSubmit(formName));
return user;
},
(errors) => {
dispatch(stopSubmit(formName, errors));
return Promise.reject(errors);
}
);
};
}
@gaearon
Copy link

gaearon commented Oct 9, 2015

I meant this instead:

function authLoginSubmit(formName, data){
    return dispatch => {
        dispatch(startSubmit(formName));
        return dispatch(authLoginRest(data)).then(
            (user) => {
                dispatch(stopSubmit(formName));
                return user;
            },
            (errors) => {
                dispatch(stopSubmit(formName, errors));
                return Promise.reject(errors);
            }
        );
    };
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment