| author | ymh <ymh.work@gmail.com> |
| Mon, 19 Jun 2017 17:30:03 +0200 | |
| changeset 54 | fad489be9c77 |
| parent 53 | d8588379529e |
| child 55 | a2761c5be551 |
| permissions | -rw-r--r-- |
| 29 | 1 |
import PouchDB from 'pouchdb' |
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
2 |
import { put, take, takeLatest, all } from 'redux-saga/effects' |
| 29 | 3 |
import * as types from '../constants/actionTypes'; |
4 |
import PouchDBFind from 'pouchdb-find'; |
|
5 |
import Immutable from 'immutable'; |
|
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
6 |
import APIClient from '../APIClient'; |
| 54 | 7 |
import config from '../config'; |
| 29 | 8 |
|
9 |
PouchDB.debug.disable(); |
|
10 |
PouchDB.plugin(PouchDBFind); |
|
11 |
||
12 |
const sessionsDB = new PouchDB('sessions'); |
|
13 |
const notesDB = new PouchDB('notes'); |
|
14 |
notesDB.createIndex({ |
|
15 |
index: { fields: ['session'] } |
|
16 |
}); |
|
17 |
||
| 54 | 18 |
const client = new APIClient(config.apiRootUrl); |
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
19 |
|
| 29 | 20 |
// --- |
21 |
||
22 |
export function* loadSessions() { |
|
23 |
const response = yield sessionsDB.allDocs({ include_docs: true }) |
|
24 |
const sessions = response.rows.map(row => row.doc) |
|
25 |
yield put({ type: types.LOAD_SESSIONS, sessions: Immutable.List(sessions) }) |
|
26 |
} |
|
27 |
||
28 |
export function* watchLoadSessions() { |
|
29 |
yield takeLatest(types.LOAD_SESSIONS_ASYNC, loadSessions) |
|
30 |
} |
|
31 |
||
32 |
// --- |
|
33 |
||
34 |
export function* createSession(action) { |
|
35 |
const response = yield sessionsDB.put(action.session); |
|
36 |
// TODO Error control |
|
37 |
const session = Object.assign({}, action.session, { rev: response.rev }); |
|
38 |
yield put({ type: types.CREATE_SESSION, session: session }) |
|
39 |
} |
|
40 |
||
41 |
export function* watchCreateSession() { |
|
42 |
yield takeLatest(types.CREATE_SESSION_ASYNC, createSession) |
|
43 |
} |
|
44 |
||
45 |
// --- |
|
46 |
||
|
30
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
47 |
export function* updateSession(action) { |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
48 |
|
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
49 |
const { _id } = action.session; |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
50 |
let session; |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
51 |
|
| 40 | 52 |
const doc = yield sessionsDB.get(_id); |
53 |
session = Object.assign({}, doc, action.values); |
|
54 |
const response = yield sessionsDB.put(session); |
|
|
30
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
55 |
|
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
56 |
yield put({ |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
57 |
type: types.UPDATE_SESSION, |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
58 |
session: Object.assign({}, session, { rev: response.rev }) |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
59 |
}) |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
60 |
} |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
61 |
|
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
62 |
export function* watchUpdateSession() { |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
63 |
yield takeLatest(types.UPDATE_SESSION_ASYNC, updateSession) |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
64 |
} |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
65 |
|
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
66 |
// --- |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
67 |
|
| 29 | 68 |
export function* addNote(action) { |
69 |
const response = yield notesDB.put(action.note); |
|
70 |
// TODO Error control |
|
71 |
const note = Object.assign({}, action.note, { rev: response.rev }); |
|
72 |
yield put({ type: types.ADD_NOTE, note: note }) |
|
73 |
} |
|
74 |
||
75 |
export function* watchAddNote() { |
|
76 |
yield takeLatest(types.ADD_NOTE_ASYNC, addNote) |
|
77 |
} |
|
78 |
||
79 |
// --- |
|
80 |
||
81 |
export function* loadNotesBySession(action) { |
|
82 |
const result = yield notesDB.find({ |
|
83 |
selector: { session: action.session._id }, |
|
84 |
// sort: ['name'] |
|
85 |
}); |
|
86 |
yield put({ type: types.LOAD_NOTES_BY_SESSION, notes: Immutable.List(result.docs) }) |
|
87 |
} |
|
88 |
||
89 |
export function* watchLoadNotesBySession() { |
|
90 |
yield takeLatest(types.LOAD_NOTES_BY_SESSION_ASYNC, loadNotesBySession) |
|
91 |
} |
|
92 |
||
93 |
// --- |
|
94 |
||
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
95 |
export function* watchLoginSubmit() { |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
96 |
while (true) { |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
97 |
const { username, password } = yield take(types.AUTH_LOGIN_SUBMIT); |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
98 |
yield put({ type: types.AUTH_LOGIN_REQUEST, username, password }); |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
99 |
} |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
100 |
} |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
101 |
|
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
102 |
function* watchLoginRequest() { |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
103 |
while (true) { |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
104 |
try { |
| 47 | 105 |
|
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
106 |
const { username, password } = yield take(types.AUTH_LOGIN_REQUEST); |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
107 |
const response = yield client.post('/api/auth/login/', { username, password }); |
| 47 | 108 |
|
109 |
localStorage.setItem('currentUser', JSON.stringify(response.user)); |
|
110 |
localStorage.setItem('token', response.token); |
|
111 |
||
112 |
const actions = [{ |
|
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
113 |
type: types.AUTH_LOGIN_SUCCESS, |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
114 |
user: response.user, |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
115 |
token: response.token, |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
116 |
meta: { |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
117 |
transition: (prevState, nextState, action) => ({ |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
118 |
pathname: '/sessions', |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
119 |
}), |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
120 |
}, |
| 47 | 121 |
}, { |
122 |
type: types.AUTH_STORE_TOKEN_ASYNC, |
|
123 |
token: response.token, |
|
124 |
}]; |
|
125 |
||
126 |
yield actions.map(action => put(action)) |
|
127 |
||
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
128 |
} catch(e) { |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
129 |
yield put({ type: types.AUTH_LOGIN_ERROR, error: e }); |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
130 |
} |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
131 |
} |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
132 |
} |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
133 |
|
| 47 | 134 |
function* watchStoreToken() { |
135 |
while (true) { |
|
136 |
const { token } = yield take(types.AUTH_STORE_TOKEN_ASYNC); |
|
137 |
yield put({ type: types.AUTH_STORE_TOKEN, token }); |
|
138 |
} |
|
139 |
} |
|
140 |
||
| 53 | 141 |
function* watchUpdateSettings() { |
142 |
while (true) { |
|
143 |
const { username, firstname, lastname } = yield take(types.USER_UPDATE_SETTINGS_ASYNC); |
|
144 |
try { |
|
145 |
yield client.put('/api/auth/user/', { |
|
146 |
username, |
|
147 |
first_name: firstname, |
|
148 |
last_name: lastname |
|
149 |
}); |
|
150 |
yield put({ type: types.USER_UPDATE_SETTINGS, firstname, lastname }); |
|
151 |
} catch (e) { |
|
152 |
||
153 |
} |
|
154 |
} |
|
155 |
} |
|
156 |
||
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
157 |
// --- |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
158 |
|
| 29 | 159 |
export default function* rootSaga() { |
160 |
yield all([ |
|
161 |
watchLoadSessions(), |
|
162 |
watchLoadNotesBySession(), |
|
163 |
watchAddNote(), |
|
164 |
watchCreateSession(), |
|
|
30
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
165 |
watchUpdateSession(), |
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
166 |
watchLoginSubmit(), |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
167 |
watchLoginRequest(), |
| 47 | 168 |
watchStoreToken(), |
| 53 | 169 |
watchUpdateSettings(), |
| 29 | 170 |
]) |
171 |
} |