1 import PouchDB from 'pouchdb' |
1 import PouchDB from 'pouchdb' |
2 import { put, takeLatest, all } from 'redux-saga/effects' |
2 import { put, take, takeLatest, all } from 'redux-saga/effects' |
3 import * as types from '../constants/actionTypes'; |
3 import * as types from '../constants/actionTypes'; |
4 import PouchDBFind from 'pouchdb-find'; |
4 import PouchDBFind from 'pouchdb-find'; |
5 import Immutable from 'immutable'; |
5 import Immutable from 'immutable'; |
|
6 import APIClient from '../APIClient'; |
6 |
7 |
7 PouchDB.debug.disable(); |
8 PouchDB.debug.disable(); |
8 PouchDB.plugin(PouchDBFind); |
9 PouchDB.plugin(PouchDBFind); |
9 |
10 |
10 const sessionsDB = new PouchDB('sessions'); |
11 const sessionsDB = new PouchDB('sessions'); |
11 const notesDB = new PouchDB('notes'); |
12 const notesDB = new PouchDB('notes'); |
12 notesDB.createIndex({ |
13 notesDB.createIndex({ |
13 index: { fields: ['session'] } |
14 index: { fields: ['session'] } |
14 }); |
15 }); |
|
16 |
|
17 const client = new APIClient('http://localhost:8000') |
15 |
18 |
16 // --- |
19 // --- |
17 |
20 |
18 export function* loadSessions() { |
21 export function* loadSessions() { |
19 const response = yield sessionsDB.allDocs({ include_docs: true }) |
22 const response = yield sessionsDB.allDocs({ include_docs: true }) |
86 yield takeLatest(types.LOAD_NOTES_BY_SESSION_ASYNC, loadNotesBySession) |
89 yield takeLatest(types.LOAD_NOTES_BY_SESSION_ASYNC, loadNotesBySession) |
87 } |
90 } |
88 |
91 |
89 // --- |
92 // --- |
90 |
93 |
|
94 export function* watchLoginSubmit() { |
|
95 while (true) { |
|
96 const { username, password } = yield take(types.AUTH_LOGIN_SUBMIT); |
|
97 yield put({ type: types.AUTH_LOGIN_REQUEST, username, password }); |
|
98 } |
|
99 } |
|
100 |
|
101 function* watchLoginRequest() { |
|
102 while (true) { |
|
103 try { |
|
104 const { username, password } = yield take(types.AUTH_LOGIN_REQUEST); |
|
105 const response = yield client.post('/api/auth/login/', { username, password }); |
|
106 yield put({ |
|
107 type: types.AUTH_LOGIN_SUCCESS, |
|
108 user: response.user, |
|
109 token: response.token, |
|
110 meta: { |
|
111 transition: (prevState, nextState, action) => ({ |
|
112 pathname: '/sessions', |
|
113 }), |
|
114 }, |
|
115 }); |
|
116 } catch(e) { |
|
117 yield put({ type: types.AUTH_LOGIN_ERROR, error: e }); |
|
118 } |
|
119 } |
|
120 } |
|
121 |
|
122 // --- |
|
123 |
91 export default function* rootSaga() { |
124 export default function* rootSaga() { |
92 yield all([ |
125 yield all([ |
93 watchLoadSessions(), |
126 watchLoadSessions(), |
94 watchLoadNotesBySession(), |
127 watchLoadNotesBySession(), |
95 watchAddNote(), |
128 watchAddNote(), |
96 watchCreateSession(), |
129 watchCreateSession(), |
97 watchUpdateSession(), |
130 watchUpdateSession(), |
|
131 watchLoginSubmit(), |
|
132 watchLoginRequest(), |
98 ]) |
133 ]) |
99 } |
134 } |