Created
August 3, 2016 07:51
-
-
Save mmazzarolo/c7b0ce9d28d61257d1d039bd0fa075a1 to your computer and use it in GitHub Desktop.
Saga example
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 { 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