diff -r fa8ef84a1780 -r dbcee57de2c6 client/src/sagas/authSaga.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/client/src/sagas/authSaga.js Mon Jun 26 15:21:06 2017 +0200 @@ -0,0 +1,73 @@ +import { put, take, all } from 'redux-saga/effects' +import * as types from '../constants/actionTypes'; + +// --- + +function* watchLoginSubmit() { + while (true) { + const { username, password } = yield take(types.AUTH_LOGIN_SUBMIT); + yield put({ type: types.AUTH_LOGIN_REQUEST, username, password }); + } +} + +function* watchLoginRequest(context) { + while (true) { + try { + + const { username, password } = yield take(types.AUTH_LOGIN_REQUEST); + const client = context.client; + const response = yield client.post('/api/auth/login/', { username, password }); + + const actions = [{ + type: types.AUTH_STORE_TOKEN_ASYNC, + token: response.token, + }, + { + type: types.AUTH_LOGIN_SUCCESS, + user: response.user, + token: response.token, + }]; + + yield all(actions.map(action => put(action))); + context.history.push('/sessions'); + + } catch(e) { + yield put({ type: types.AUTH_LOGIN_ERROR, error: e }); + } + } +} + +function* watchStoreToken() { + while (true) { + const { token } = yield take(types.AUTH_STORE_TOKEN_ASYNC); + yield put({ type: types.AUTH_STORE_TOKEN, token }); + } +} + +function* watchUpdateSettings(context) { + while (true) { + const { username, firstname, lastname } = yield take(types.USER_UPDATE_SETTINGS_ASYNC); + const client = context.client; + try { + yield client.put('/api/auth/user/', { + username, + first_name: firstname, + last_name: lastname + }); + yield put({ type: types.USER_UPDATE_SETTINGS, firstname, lastname }); + } catch (e) { + + } + } +} + +// --- + +export default function* rootSaga(context) { + yield all([ + watchLoginSubmit(), + watchLoginRequest(context), + watchStoreToken(), + watchUpdateSettings(context), + ]) +}