Skip to content

Instantly share code, notes, and snippets.

@mmazzarolo
Created August 3, 2016 07:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmazzarolo/c7b0ce9d28d61257d1d039bd0fa075a1 to your computer and use it in GitHub Desktop.
Save mmazzarolo/c7b0ce9d28d61257d1d039bd0fa075a1 to your computer and use it in GitHub Desktop.
Saga example
import { call, put } from 'redux-saga/effects'
import * as parseService from '../services/parseService'
import { actionTypes as authActionTypes } from '../reducers/authReducer'
import { actionTypes as navigationActionTypes } from '../reducers/navigationReducer'
import * as routes from '../config/routes'
export function* autoLogin (action) {
const user = yield call(parseService.currentUser)
if (user) {
yield call(loginSuccess, user)
} else {
yield put({ type: navigationActionTypes.RESET, route: routes.authScreen })
}
}
export function* signup (action) {
const { email, password } = action
try {
const user = yield call(parseService.signup, email, password)
yield put({ type: authActionTypes.SIGNUP_SUCCESS, user })
yield call(loginSuccess, user) // Random example of saga calling another saga
} catch (err) {
const error = err.message || err
yield put({ type: authActionTypes.SIGNUP_FAILURE, error })
}
}
export function* login (action) {
const { email, password } = action
try {
const user = yield call(parseService.login, email, password)
yield call(loginSuccess, user) // Random example of saga calling another saga
} catch (err) {
const error = err.message || err
yield put({ type: authActionTypes.LOGIN_FAILURE, error })
}
}
export function* logout (action) {
yield call(parseService.logout)
yield put({ type: navigationActionTypes.RESET, route: routes.authScreen })
}
// Just a random example of saga called by another saga
export function* loginSuccess (user) {
yield put({ type: authActionTypes.LOGIN_SUCCESS, user })
yield put({ type: navigationActionTypes.RESET, route: routes.museumList })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment