| author | ymh <ymh.work@gmail.com> |
| Mon, 19 Jun 2017 21:37:33 +0200 | |
| changeset 58 | f16a080e0bc4 |
| parent 56 | 96543c395baa |
| child 59 | 1eb52770eefa |
| permissions | -rw-r--r-- |
| 1 | 1 |
import rootReducer from '../reducers'; |
|
29
4cfeabef7d5e
Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
14
diff
changeset
|
2 |
import rootSaga from '../sagas'; |
|
4cfeabef7d5e
Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
14
diff
changeset
|
3 |
import { loadSessions } from '../actions/sessionsActions'; |
|
58
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
56
diff
changeset
|
4 |
import { compose, createStore, applyMiddleware } from 'redux'; |
|
12
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
3
diff
changeset
|
5 |
import { routerMiddleware } from 'react-router-redux'; |
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
6 |
import handleTransitions from 'redux-history-transitions'; |
|
29
4cfeabef7d5e
Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
14
diff
changeset
|
7 |
import createSagaMiddleware from 'redux-saga' |
| 1 | 8 |
import Immutable from 'immutable'; |
|
58
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
56
diff
changeset
|
9 |
import { offline } from 'redux-offline'; |
|
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
56
diff
changeset
|
10 |
import offlineDefaultConfig from 'redux-offline/lib/defaults'; |
|
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
56
diff
changeset
|
11 |
import localForage from 'localforage'; |
|
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
56
diff
changeset
|
12 |
import immutableTransform from 'redux-persist-transform-immutable' |
|
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
56
diff
changeset
|
13 |
import NoteRecord from './noteRecord' |
| 55 | 14 |
import APIClient from '../APIClient'; |
15 |
import config from '../config'; |
|
| 1 | 16 |
|
| 47 | 17 |
const token = localStorage.getItem('token'); |
18 |
const currentUser = localStorage.getItem('currentUser'); |
|
19 |
||
|
12
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
3
diff
changeset
|
20 |
const defaultState = { |
|
58
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
56
diff
changeset
|
21 |
// currentSession: null, |
|
29
4cfeabef7d5e
Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
14
diff
changeset
|
22 |
sessions: Immutable.List([]), |
|
4cfeabef7d5e
Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
14
diff
changeset
|
23 |
notes: Immutable.List([]), |
| 47 | 24 |
isAuthenticated: token !== null, |
25 |
currentUser: currentUser ? JSON.parse(currentUser) : null, |
|
26 |
token: token, |
|
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
27 |
login: { |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
28 |
loading: false, |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
29 |
success: false, |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
30 |
error: false, |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
31 |
} |
| 1 | 32 |
}; |
|
12
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
3
diff
changeset
|
33 |
|
|
58
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
56
diff
changeset
|
34 |
const immutableTransformConfig = { |
|
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
56
diff
changeset
|
35 |
records: [NoteRecord], |
|
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
56
diff
changeset
|
36 |
whitelist: ['sessions', 'notes'] |
|
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
56
diff
changeset
|
37 |
} |
|
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
56
diff
changeset
|
38 |
|
|
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
56
diff
changeset
|
39 |
const offlineConfig = { |
|
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
56
diff
changeset
|
40 |
...offlineDefaultConfig, |
|
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
56
diff
changeset
|
41 |
persistOptions: { |
|
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
56
diff
changeset
|
42 |
storage: localForage, |
|
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
56
diff
changeset
|
43 |
transforms: [immutableTransform(immutableTransformConfig)] |
|
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
56
diff
changeset
|
44 |
}, |
|
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
56
diff
changeset
|
45 |
detectNetwork: callback => callback(true), |
|
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
56
diff
changeset
|
46 |
} |
|
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
56
diff
changeset
|
47 |
|
|
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
56
diff
changeset
|
48 |
//const storeInitialState = Immutable.Map(defaultState); |
|
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
56
diff
changeset
|
49 |
//const storeInitialState = new Map(defaultState); |
|
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
56
diff
changeset
|
50 |
const storeInitialState = defaultState; |
|
12
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
3
diff
changeset
|
51 |
|
|
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
3
diff
changeset
|
52 |
export default (history, initialState = storeInitialState) => { |
|
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
3
diff
changeset
|
53 |
|
|
29
4cfeabef7d5e
Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
14
diff
changeset
|
54 |
const router = routerMiddleware(history); |
|
4cfeabef7d5e
Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
14
diff
changeset
|
55 |
const saga = createSagaMiddleware(); |
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
56 |
const transitions = handleTransitions(history); |
|
12
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
3
diff
changeset
|
57 |
|
|
58
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
56
diff
changeset
|
58 |
const store = offline(offlineConfig)(createStore)(rootReducer, initialState, compose( |
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
59 |
applyMiddleware(router, saga), |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
60 |
transitions |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
61 |
)); |
|
12
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
3
diff
changeset
|
62 |
|
| 55 | 63 |
const context = { |
|
56
96543c395baa
Use token in storage state, not in localStorage
ymh <ymh.work@gmail.com>
parents:
55
diff
changeset
|
64 |
client: new APIClient(config.apiRootUrl, store) |
| 55 | 65 |
} |
66 |
||
67 |
saga.run(rootSaga, context); |
|
|
29
4cfeabef7d5e
Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
14
diff
changeset
|
68 |
|
|
58
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
56
diff
changeset
|
69 |
//store.dispatch(loadSessions()); |
|
12
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
3
diff
changeset
|
70 |
|
|
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
3
diff
changeset
|
71 |
return store; |
|
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
3
diff
changeset
|
72 |
}; |