| author | Alexandre Segura <mex.zktk@gmail.com> |
| Wed, 28 Jun 2017 12:54:48 +0200 | |
| changeset 101 | e165aa89ac82 |
| parent 100 | 6fd752d98933 |
| child 129 | d48946d164c6 |
| permissions | -rw-r--r-- |
| 1 | 1 |
import rootReducer from '../reducers'; |
|
87
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
2 |
import rootAuthSaga from '../sagas/authSaga'; |
|
100
6fd752d98933
Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
91
diff
changeset
|
3 |
import rootGroupSaga from '../sagas/groupSaga'; |
|
87
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
4 |
import networkSaga from '../sagas/networkSaga'; |
|
62
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
5 |
import { compose, createStore, applyMiddleware } from 'redux'; |
|
12
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
3
diff
changeset
|
6 |
import { routerMiddleware } from 'react-router-redux'; |
|
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'; |
|
62
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
9 |
import { offline } from 'redux-offline'; |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
10 |
import offlineDefaultConfig from 'redux-offline/lib/defaults'; |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
11 |
import localForage from 'localforage'; |
| 67 | 12 |
import immutableTransform from 'redux-persist-transform-immutable'; |
13 |
import NoteRecord from './noteRecord'; |
|
14 |
import SessionRecord from './sessionRecord'; |
|
15 |
import UserRecord from './userRecord'; |
|
|
62
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
16 |
import APIClient from '../api/APIClient'; |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
17 |
import createEffect from '../api'; |
| 55 | 18 |
import config from '../config'; |
|
87
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
19 |
import { offlineConfigInitialized } from '../actions/networkActions'; |
| 91 | 20 |
import asyncRequest from '../constants/asyncRequest'; |
|
101
e165aa89ac82
Add group dropdown, update session after group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
100
diff
changeset
|
21 |
import * as types from '../constants/actionTypes'; |
| 1 | 22 |
|
|
62
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
23 |
// const composeEnhancers = (process.env.NODE_ENV !== 'production' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) ? |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
24 |
// window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({ |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
25 |
// shouldHotReload: false, |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
26 |
// }) : compose; |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
27 |
const composeEnhancers = compose; |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
28 |
|
| 47 | 29 |
|
|
12
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
3
diff
changeset
|
30 |
const defaultState = { |
|
29
4cfeabef7d5e
Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
14
diff
changeset
|
31 |
sessions: Immutable.List([]), |
|
4cfeabef7d5e
Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
14
diff
changeset
|
32 |
notes: Immutable.List([]), |
|
100
6fd752d98933
Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
91
diff
changeset
|
33 |
groups: Immutable.List([]), |
|
62
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
34 |
isAuthenticated: false, |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
35 |
currentUser: null, |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
36 |
token: '', |
|
72
7634b424f426
Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents:
68
diff
changeset
|
37 |
autoSubmit: false, |
| 91 | 38 |
login: asyncRequest, |
39 |
register: asyncRequest, |
|
|
100
6fd752d98933
Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
91
diff
changeset
|
40 |
createGroup: asyncRequest |
| 1 | 41 |
}; |
|
12
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
3
diff
changeset
|
42 |
|
|
62
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
43 |
const immutableTransformConfig = { |
| 67 | 44 |
records: [NoteRecord, SessionRecord, UserRecord], |
|
62
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
45 |
whitelist: ['sessions', 'notes', 'currentUser'] |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
46 |
} |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
47 |
|
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
48 |
const persistOptions = { |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
49 |
storage: localForage, |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
50 |
transforms: [immutableTransform(immutableTransformConfig)], |
|
72
7634b424f426
Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents:
68
diff
changeset
|
51 |
whitelist: ['sessions', 'notes', 'isAuthenticated', 'currentUser', 'token', 'offline', 'autoSubmit'] |
|
62
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
52 |
} |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
53 |
|
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
54 |
const apiClient = new APIClient(config.apiRootUrl); |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
55 |
|
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
56 |
const offlineConfig = { |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
57 |
...offlineDefaultConfig, |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
58 |
persistOptions, |
|
74
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
72
diff
changeset
|
59 |
effect: createEffect(apiClient), |
|
62
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
60 |
} |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
61 |
|
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
62 |
const storeInitialState = { ...defaultState }; |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
63 |
|
|
12
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
3
diff
changeset
|
64 |
|
|
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
3
diff
changeset
|
65 |
export default (history, initialState = storeInitialState) => { |
|
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
3
diff
changeset
|
66 |
|
|
29
4cfeabef7d5e
Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
14
diff
changeset
|
67 |
const router = routerMiddleware(history); |
|
4cfeabef7d5e
Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
14
diff
changeset
|
68 |
const saga = createSagaMiddleware(); |
|
12
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
3
diff
changeset
|
69 |
|
|
87
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
70 |
offlineConfig.detectNetwork = callback => { saga.run(networkSaga, { callback }); }; |
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
71 |
|
|
62
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
72 |
const store = offline(offlineConfig)(createStore)(rootReducer, initialState, composeEnhancers( |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
73 |
applyMiddleware(router, saga) |
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
74 |
)); |
|
12
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
3
diff
changeset
|
75 |
|
|
62
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
76 |
apiClient.setStore(store); |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
77 |
|
| 55 | 78 |
const context = { |
|
62
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
79 |
client: apiClient, |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
80 |
history |
| 55 | 81 |
} |
82 |
||
|
87
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
83 |
saga.run(rootAuthSaga, context); |
|
100
6fd752d98933
Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
91
diff
changeset
|
84 |
saga.run(rootGroupSaga, context); |
|
87
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
85 |
|
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
86 |
store.dispatch(offlineConfigInitialized({ client: apiClient })) |
|
101
e165aa89ac82
Add group dropdown, update session after group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents:
100
diff
changeset
|
87 |
store.dispatch({ type: types.GROUP_LOAD_ASYNC }) |
|
29
4cfeabef7d5e
Store data in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
14
diff
changeset
|
88 |
|
|
12
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
3
diff
changeset
|
89 |
return store; |
|
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
3
diff
changeset
|
90 |
}; |