client/src/sagas/authSaga.js
changeset 87 dbcee57de2c6
child 89 06f609adfbf8
equal deleted inserted replaced
86:fa8ef84a1780 87:dbcee57de2c6
       
     1 import { put, take, all } from 'redux-saga/effects'
       
     2 import * as types from '../constants/actionTypes';
       
     3 
       
     4 // ---
       
     5 
       
     6 function* watchLoginSubmit() {
       
     7   while (true) {
       
     8     const { username, password } = yield take(types.AUTH_LOGIN_SUBMIT);
       
     9     yield put({ type: types.AUTH_LOGIN_REQUEST, username, password });
       
    10   }
       
    11 }
       
    12 
       
    13 function* watchLoginRequest(context) {
       
    14   while (true) {
       
    15     try {
       
    16 
       
    17         const { username, password } = yield take(types.AUTH_LOGIN_REQUEST);
       
    18         const client = context.client;
       
    19         const response = yield client.post('/api/auth/login/', { username, password });
       
    20 
       
    21         const actions = [{
       
    22           type: types.AUTH_STORE_TOKEN_ASYNC,
       
    23           token: response.token,
       
    24         },
       
    25         {
       
    26           type: types.AUTH_LOGIN_SUCCESS,
       
    27           user: response.user,
       
    28           token: response.token,
       
    29         }];
       
    30 
       
    31         yield all(actions.map(action => put(action)));
       
    32         context.history.push('/sessions');
       
    33 
       
    34     } catch(e) {
       
    35         yield put({ type: types.AUTH_LOGIN_ERROR, error: e });
       
    36     }
       
    37   }
       
    38 }
       
    39 
       
    40 function* watchStoreToken() {
       
    41   while (true) {
       
    42     const { token } = yield take(types.AUTH_STORE_TOKEN_ASYNC);
       
    43     yield put({ type: types.AUTH_STORE_TOKEN, token });
       
    44   }
       
    45 }
       
    46 
       
    47 function* watchUpdateSettings(context) {
       
    48   while (true) {
       
    49     const { username, firstname, lastname } = yield take(types.USER_UPDATE_SETTINGS_ASYNC);
       
    50     const client = context.client;
       
    51     try {
       
    52       yield client.put('/api/auth/user/', {
       
    53         username,
       
    54         first_name: firstname,
       
    55         last_name: lastname
       
    56       });
       
    57       yield put({ type: types.USER_UPDATE_SETTINGS, firstname, lastname });
       
    58     } catch (e) {
       
    59 
       
    60     }
       
    61   }
       
    62 }
       
    63 
       
    64 // ---
       
    65 
       
    66 export default function* rootSaga(context) {
       
    67   yield all([
       
    68     watchLoginSubmit(),
       
    69     watchLoginRequest(context),
       
    70     watchStoreToken(),
       
    71     watchUpdateSettings(context),
       
    72   ])
       
    73 }