| author | Alexandre Segura <mex.zktk@gmail.com> |
| Wed, 14 Jun 2017 12:28:09 +0200 | |
| changeset 30 | 4d93f4ed95bc |
| parent 29 | 4cfeabef7d5e |
| child 40 | 7f940dbb60a6 |
| permissions | -rw-r--r-- |
| 29 | 1 |
import PouchDB from 'pouchdb' |
2 |
import { put, takeLatest, all } from 'redux-saga/effects' |
|
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 |
|
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
48 |
const response = yield sessionsDB.get(_id).then(function(doc) { |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
49 |
session = Object.assign({}, doc, action.values); |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
50 |
return sessionsDB.put(session); |
|
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 |
|
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
53 |
yield put({ |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
54 |
type: types.UPDATE_SESSION, |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
55 |
session: Object.assign({}, session, { rev: response.rev }) |
|
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 |
|
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
59 |
export function* watchUpdateSession() { |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
60 |
yield takeLatest(types.UPDATE_SESSION_ASYNC, updateSession) |
|
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 |
// --- |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
64 |
|
| 29 | 65 |
export function* addNote(action) { |
66 |
const response = yield notesDB.put(action.note); |
|
67 |
// TODO Error control |
|
68 |
const note = Object.assign({}, action.note, { rev: response.rev }); |
|
69 |
yield put({ type: types.ADD_NOTE, note: note }) |
|
70 |
} |
|
71 |
||
72 |
export function* watchAddNote() { |
|
73 |
yield takeLatest(types.ADD_NOTE_ASYNC, addNote) |
|
74 |
} |
|
75 |
||
76 |
// --- |
|
77 |
||
78 |
export function* loadNotesBySession(action) { |
|
79 |
const result = yield notesDB.find({ |
|
80 |
selector: { session: action.session._id }, |
|
81 |
// sort: ['name'] |
|
82 |
}); |
|
83 |
yield put({ type: types.LOAD_NOTES_BY_SESSION, notes: Immutable.List(result.docs) }) |
|
84 |
} |
|
85 |
||
86 |
export function* watchLoadNotesBySession() { |
|
87 |
yield takeLatest(types.LOAD_NOTES_BY_SESSION_ASYNC, loadNotesBySession) |
|
88 |
} |
|
89 |
||
90 |
// --- |
|
91 |
||
92 |
export default function* rootSaga() { |
|
93 |
yield all([ |
|
94 |
watchLoadSessions(), |
|
95 |
watchLoadNotesBySession(), |
|
96 |
watchAddNote(), |
|
97 |
watchCreateSession(), |
|
|
30
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
98 |
watchUpdateSession(), |
| 29 | 99 |
]) |
100 |
} |