Created
October 9, 2015 18:04
-
-
Save mattiamanzati/1f22549e60d58178f3b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I meant this instead: