Created
June 5, 2017 23:55
-
-
Save jacks205/3ca87f79bf078a2d8d1397463ae8d4e8 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
import Rx from 'rxjs' | |
import { loginUser, retrieveCurrentUser } from '../api/users' | |
import { combineEpics } from 'redux-observable' | |
import { | |
LOGIN_USER, LOGIN_USER_CANCELLED, LOGIN_USER_SUCCESS, | |
RETRIEVE_CURRENT_USER, RETRIEVE_CURRENT_USER_CANCELLED | |
} from '../constants/UserActionTypes' | |
import { | |
loginUserSuccess, loginUserError, | |
retrieveCurrentUser as retrieveCurrentUserAction, retrieveCurrentUserSuccess, retrieveCurrentUserError | |
} from '../actions/users' | |
const loginUserEpic = action$ => | |
action$.ofType(LOGIN_USER) //When users are logging in | |
.mergeMap(action => | |
loginUser(action.email, action.password) //Send login POST request | |
.map(payload => loginUserSuccess(payload.data)) // Notify store/reducer of request being fulfilled | |
.takeUntil(action$.ofType(LOGIN_USER_CANCELLED)) // Cancel request if AUTHENTICATE_CANCELLED is dispatched | |
.catch(error => Rx.Observable.of(loginUserError(error))) // dispatch authentication error if request fails | |
) | |
const loginUserSuccessEpic = action$ => | |
action$.ofType(LOGIN_USER_SUCCESS) | |
.map(() => retrieveCurrentUserAction()) | |
const retrieveCurrentUserEpic = action$ => | |
action$.ofType(RETRIEVE_CURRENT_USER) | |
.mergeMap(action => | |
retrieveCurrentUser() | |
.map(payload => {console.log(payload); return retrieveCurrentUserSuccess(payload.data)}) | |
.takeUntil(action$.ofType(RETRIEVE_CURRENT_USER_CANCELLED)) | |
.catch(error => Rx.Observable.of(retrieveCurrentUserError(error))) | |
) | |
const users = combineEpics( | |
loginUserEpic, | |
loginUserSuccessEpic, | |
retrieveCurrentUserEpic | |
) | |
export default users |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment