Skip to content

Instantly share code, notes, and snippets.

@mattiamanzati
Created February 28, 2016 19:27
Show Gist options
  • Save mattiamanzati/eb15f97ccf248fd8a35c to your computer and use it in GitHub Desktop.
Save mattiamanzati/eb15f97ccf248fd8a35c to your computer and use it in GitHub Desktop.
import {login} from './api';
import {take, put, fork, call, race} from 'redux-saga/effects';
import {LOGIN_REQUEST, LOGIN_SUBMIT, LOGIN_SUCCESS, LOGIN_ERROR} from './actions';
import {loginRequest, loginError, loginSuccess} from './actions';
import {startSubmit, stopSubmit} from '../form/actions';
import {clearState} from '../router/actions';
function* handleLoginSubmit(){
// run the daemon
while(true){
// wait for a login submit
var {payload} = yield take(LOGIN_SUBMIT);
// start submitting the form
yield put(startSubmit("authLogin"));
// put a login request
yield put(loginRequest(payload));
// wait for a response
var {error, success} = yield race({
success: take(LOGIN_SUCCESS),
error: take(LOGIN_ERROR)
});
// if not an error, pop the screen
if(!error){
// finalize the form
yield put(stopSubmit("authLogin"));
yield put(clearState());
}else{
// finalize the form
yield put(stopSubmit("authLogin", error.payload));
}
}
}
function* handleLoginRequest(){
// run the daemon
while(true){
try{
// wait for a login request
var {payload} = yield take(LOGIN_REQUEST);
// call the api
var user = yield call(login, payload);
// call the success
yield put(loginSuccess(user));
}catch(e){
// call the error
yield put(loginError(e));
}
}
}
export default function* auth(getState){
yield fork(handleLoginRequest);
yield fork(handleLoginSubmit);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment