|
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 |
|
|
|
43 |
export function* addNote(action) { |
|
|
44 |
const response = yield notesDB.put(action.note); |
|
|
45 |
// TODO Error control |
|
|
46 |
const note = Object.assign({}, action.note, { rev: response.rev }); |
|
|
47 |
yield put({ type: types.ADD_NOTE, note: note }) |
|
|
48 |
} |
|
|
49 |
|
|
|
50 |
export function* watchAddNote() { |
|
|
51 |
yield takeLatest(types.ADD_NOTE_ASYNC, addNote) |
|
|
52 |
} |
|
|
53 |
|
|
|
54 |
// --- |
|
|
55 |
|
|
|
56 |
export function* loadNotesBySession(action) { |
|
|
57 |
const result = yield notesDB.find({ |
|
|
58 |
selector: { session: action.session._id }, |
|
|
59 |
// sort: ['name'] |
|
|
60 |
}); |
|
|
61 |
yield put({ type: types.LOAD_NOTES_BY_SESSION, notes: Immutable.List(result.docs) }) |
|
|
62 |
} |
|
|
63 |
|
|
|
64 |
export function* watchLoadNotesBySession() { |
|
|
65 |
yield takeLatest(types.LOAD_NOTES_BY_SESSION_ASYNC, loadNotesBySession) |
|
|
66 |
} |
|
|
67 |
|
|
|
68 |
// --- |
|
|
69 |
|
|
|
70 |
export default function* rootSaga() { |
|
|
71 |
yield all([ |
|
|
72 |
watchLoadSessions(), |
|
|
73 |
watchLoadNotesBySession(), |
|
|
74 |
watchAddNote(), |
|
|
75 |
watchCreateSession(), |
|
|
76 |
]) |
|
|
77 |
} |