| author | ymh <ymh.work@gmail.com> |
| Thu, 15 Jun 2017 15:27:36 +0200 | |
| changeset 40 | 7f940dbb60a6 |
| parent 30 | 4d93f4ed95bc |
| child 44 | 3b20e2b584fe |
| 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 |
|
| 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 |
||
91 |
export default function* rootSaga() { |
|
92 |
yield all([ |
|
93 |
watchLoadSessions(), |
|
94 |
watchLoadNotesBySession(), |
|
95 |
watchAddNote(), |
|
96 |
watchCreateSession(), |
|
|
30
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
97 |
watchUpdateSession(), |
| 29 | 98 |
]) |
99 |
} |