1 import PouchDB from 'pouchdb' |
1 import { put, take, all } from 'redux-saga/effects' |
2 import { put, take, takeLatest, all } from 'redux-saga/effects' |
|
3 import * as types from '../constants/actionTypes'; |
2 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* updateSession(action) { |
|
44 |
|
45 const { _id } = action.session; |
|
46 let session; |
|
47 |
|
48 const doc = yield sessionsDB.get(_id); |
|
49 session = Object.assign({}, doc, action.values); |
|
50 const response = yield sessionsDB.put(session); |
|
51 |
|
52 yield put({ |
|
53 type: types.UPDATE_SESSION, |
|
54 session: Object.assign({}, session, { rev: response.rev }) |
|
55 }) |
|
56 } |
|
57 |
|
58 export function* watchUpdateSession() { |
|
59 yield takeLatest(types.UPDATE_SESSION_ASYNC, updateSession) |
|
60 } |
|
61 |
|
62 // --- |
|
63 |
|
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 |
3 |
89 // --- |
4 // --- |
90 |
5 |
91 export function* watchLoginSubmit() { |
6 export function* watchLoginSubmit() { |
92 while (true) { |
7 while (true) { |
101 |
16 |
102 const { username, password } = yield take(types.AUTH_LOGIN_REQUEST); |
17 const { username, password } = yield take(types.AUTH_LOGIN_REQUEST); |
103 const client = context.client; |
18 const client = context.client; |
104 const response = yield client.post('/api/auth/login/', { username, password }); |
19 const response = yield client.post('/api/auth/login/', { username, password }); |
105 |
20 |
106 localStorage.setItem('currentUser', JSON.stringify(response.user)); |
|
107 localStorage.setItem('token', response.token); |
|
108 |
|
109 const actions = [{ |
21 const actions = [{ |
110 type: types.AUTH_LOGIN_SUCCESS, |
22 type: types.AUTH_LOGIN_SUCCESS, |
111 user: response.user, |
23 user: response.user, |
112 token: response.token, |
24 token: response.token, |
113 meta: { |
25 // meta: { |
114 transition: (prevState, nextState, action) => ({ |
26 // transition: (prevState, nextState, action) => ({ |
115 pathname: '/sessions', |
27 // pathname: '/sessions', |
116 }), |
28 // }), |
117 }, |
29 // }, |
118 }, { |
30 }, { |
119 type: types.AUTH_STORE_TOKEN_ASYNC, |
31 type: types.AUTH_STORE_TOKEN_ASYNC, |
120 token: response.token, |
32 token: response.token, |
121 }]; |
33 }]; |
122 |
34 |
123 yield all(actions.map(action => put(action))) |
35 yield all(actions.map(action => put(action))); |
|
36 context.history.push('/sessions'); |
124 |
37 |
125 } catch(e) { |
38 } catch(e) { |
126 yield put({ type: types.AUTH_LOGIN_ERROR, error: e }); |
39 yield put({ type: types.AUTH_LOGIN_ERROR, error: e }); |
127 } |
40 } |
128 } |
41 } |