Skip to content

Instantly share code, notes, and snippets.

@jacks205
Created June 5, 2017 23:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacks205/3ca87f79bf078a2d8d1397463ae8d4e8 to your computer and use it in GitHub Desktop.
Save jacks205/3ca87f79bf078a2d8d1397463ae8d4e8 to your computer and use it in GitHub Desktop.
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