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