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