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