client/src/sagas/index.js
author ymh <ymh.work@gmail.com>
Tue, 20 Jun 2017 14:13:15 +0200
changeset 62 b2514a9bcd49
parent 59 1eb52770eefa
permissions -rw-r--r--
migrate to redux-offline + various optimisation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
     1
import { put, take, all } from 'redux-saga/effects'
29
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     2
import * as types from '../constants/actionTypes';
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     3
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     4
// ---
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     5
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 40
diff changeset
     6
export function* watchLoginSubmit() {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 40
diff changeset
     7
  while (true) {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 40
diff changeset
     8
    const { username, password } = yield take(types.AUTH_LOGIN_SUBMIT);
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 40
diff changeset
     9
    yield put({ type: types.AUTH_LOGIN_REQUEST, username, password });
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 40
diff changeset
    10
  }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 40
diff changeset
    11
}
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 40
diff changeset
    12
55
a2761c5be551 put APIClient in context
ymh <ymh.work@gmail.com>
parents: 54
diff changeset
    13
function* watchLoginRequest(context) {
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 40
diff changeset
    14
  while (true) {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 40
diff changeset
    15
    try {
47
64428c7ebc19 Store token & user.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    16
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 40
diff changeset
    17
        const { username, password } = yield take(types.AUTH_LOGIN_REQUEST);
57
2e4e9f9ebc4f Take client from context
ymh <ymh.work@gmail.com>
parents: 56
diff changeset
    18
        const client = context.client;
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 40
diff changeset
    19
        const response = yield client.post('/api/auth/login/', { username, password });
47
64428c7ebc19 Store token & user.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    20
64428c7ebc19 Store token & user.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    21
        const actions = [{
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 40
diff changeset
    22
          type: types.AUTH_LOGIN_SUCCESS,
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 40
diff changeset
    23
          user: response.user,
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 40
diff changeset
    24
          token: response.token,
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    25
        //   meta: {
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    26
        //     transition: (prevState, nextState, action) => ({
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    27
        //       pathname: '/sessions',
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    28
        //     }),
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    29
        //   },
47
64428c7ebc19 Store token & user.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    30
        }, {
64428c7ebc19 Store token & user.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    31
          type: types.AUTH_STORE_TOKEN_ASYNC,
64428c7ebc19 Store token & user.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    32
          token: response.token,
64428c7ebc19 Store token & user.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    33
        }];
64428c7ebc19 Store token & user.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    34
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    35
        yield all(actions.map(action => put(action)));
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    36
        context.history.push('/sessions');
47
64428c7ebc19 Store token & user.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    37
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 40
diff changeset
    38
    } catch(e) {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 40
diff changeset
    39
        yield put({ type: types.AUTH_LOGIN_ERROR, error: e });
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 40
diff changeset
    40
    }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 40
diff changeset
    41
  }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 40
diff changeset
    42
}
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 40
diff changeset
    43
47
64428c7ebc19 Store token & user.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    44
function* watchStoreToken() {
64428c7ebc19 Store token & user.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    45
  while (true) {
64428c7ebc19 Store token & user.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    46
    const { token } = yield take(types.AUTH_STORE_TOKEN_ASYNC);
64428c7ebc19 Store token & user.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    47
    yield put({ type: types.AUTH_STORE_TOKEN, token });
64428c7ebc19 Store token & user.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    48
  }
64428c7ebc19 Store token & user.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    49
}
64428c7ebc19 Store token & user.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    50
57
2e4e9f9ebc4f Take client from context
ymh <ymh.work@gmail.com>
parents: 56
diff changeset
    51
function* watchUpdateSettings(context) {
53
d8588379529e Add settings page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 47
diff changeset
    52
  while (true) {
d8588379529e Add settings page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 47
diff changeset
    53
    const { username, firstname, lastname } = yield take(types.USER_UPDATE_SETTINGS_ASYNC);
57
2e4e9f9ebc4f Take client from context
ymh <ymh.work@gmail.com>
parents: 56
diff changeset
    54
    const client = context.client;
53
d8588379529e Add settings page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 47
diff changeset
    55
    try {
d8588379529e Add settings page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 47
diff changeset
    56
      yield client.put('/api/auth/user/', {
d8588379529e Add settings page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 47
diff changeset
    57
        username,
d8588379529e Add settings page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 47
diff changeset
    58
        first_name: firstname,
d8588379529e Add settings page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 47
diff changeset
    59
        last_name: lastname
d8588379529e Add settings page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 47
diff changeset
    60
      });
d8588379529e Add settings page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 47
diff changeset
    61
      yield put({ type: types.USER_UPDATE_SETTINGS, firstname, lastname });
d8588379529e Add settings page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 47
diff changeset
    62
    } catch (e) {
d8588379529e Add settings page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 47
diff changeset
    63
d8588379529e Add settings page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 47
diff changeset
    64
    }
d8588379529e Add settings page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 47
diff changeset
    65
  }
d8588379529e Add settings page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 47
diff changeset
    66
}
d8588379529e Add settings page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 47
diff changeset
    67
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 40
diff changeset
    68
// ---
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 40
diff changeset
    69
55
a2761c5be551 put APIClient in context
ymh <ymh.work@gmail.com>
parents: 54
diff changeset
    70
export default function* rootSaga(context) {
29
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    71
  yield all([
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 40
diff changeset
    72
    watchLoginSubmit(),
55
a2761c5be551 put APIClient in context
ymh <ymh.work@gmail.com>
parents: 54
diff changeset
    73
    watchLoginRequest(context),
47
64428c7ebc19 Store token & user.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    74
    watchStoreToken(),
57
2e4e9f9ebc4f Take client from context
ymh <ymh.work@gmail.com>
parents: 56
diff changeset
    75
    watchUpdateSettings(context),
29
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    76
  ])
4cfeabef7d5e Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    77
}