2 import rootAuthSaga from '../sagas/authSaga'; |
2 import rootAuthSaga from '../sagas/authSaga'; |
3 import rootGroupSaga from '../sagas/groupSaga'; |
3 import rootGroupSaga from '../sagas/groupSaga'; |
4 import rootSyncSaga from '../sagas/syncSaga'; |
4 import rootSyncSaga from '../sagas/syncSaga'; |
5 import networkSaga from '../sagas/networkSaga'; |
5 import networkSaga from '../sagas/networkSaga'; |
6 import { compose, createStore, applyMiddleware } from 'redux'; |
6 import { compose, createStore, applyMiddleware } from 'redux'; |
7 import { routerMiddleware } from 'react-router-redux'; |
7 import { connectRouter, routerMiddleware } from 'connected-react-router' |
8 import createSagaMiddleware from 'redux-saga' |
8 import createSagaMiddleware from 'redux-saga'; |
9 import Immutable from 'immutable'; |
9 import { persistStore, persistReducer } from 'redux-persist'; |
10 import {persistStore, autoRehydrate} from 'redux-persist-immutable' |
|
11 import localForage from 'localforage'; |
10 import localForage from 'localforage'; |
12 import immutableTransform from 'redux-persist-transform-immutable'; |
|
13 import NoteRecord from './noteRecord'; |
|
14 import SessionRecord from './sessionRecord'; |
|
15 import UserRecord from './userRecord'; |
|
16 import GroupRecord from './groupRecord'; |
|
17 import APIClient from '../api/APIClient'; |
11 import APIClient from '../api/APIClient'; |
18 import config from '../config'; |
12 import config from '../config'; |
19 import asyncRequest from '../constants/asyncRequest'; |
13 import asyncRequest from '../constants/asyncRequest'; |
20 |
14 |
21 const composeEnhancers = (process.env.NODE_ENV !== 'production' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) ? |
15 const composeEnhancers = (process.env.NODE_ENV !== 'production' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) ? |
23 shouldHotReload: false, |
17 shouldHotReload: false, |
24 }) : compose; |
18 }) : compose; |
25 |
19 |
26 |
20 |
27 const defaultState = { |
21 const defaultState = { |
28 sessions: Immutable.List([]), |
22 sessions: [], |
29 notes: Immutable.List([]), |
23 notes: [], |
30 groups: Immutable.List([]), |
24 groups: [], |
31 status: Immutable.Map({ |
25 status: { |
32 isSynchronizing: false, |
26 isSynchronizing: false, |
33 online: false |
27 online: false |
34 }), |
28 }, |
35 authStatus: Immutable.Map({ |
29 authStatus: { |
36 token: '', |
30 token: '', |
37 isAuthenticated: false, |
31 isAuthenticated: false, |
38 clientId: null, |
32 clientId: null, |
39 lastSync: 0, |
33 lastSync: 0, |
40 currentUser: null, |
34 currentUser: null, |
41 currentGroup: null |
35 currentGroup: null |
42 }), |
36 }, |
43 autoSubmit: false, |
37 autoSubmit: false, |
44 login: asyncRequest, |
38 login: asyncRequest, |
45 register: asyncRequest, |
39 register: asyncRequest, |
46 createGroup: asyncRequest |
40 createGroup: asyncRequest |
47 }; |
41 }; |
48 |
42 |
49 const immutableTransformConfig = { |
|
50 records: [NoteRecord, SessionRecord, UserRecord, GroupRecord], |
|
51 whitelist: ['sessions', 'notes', 'authStatus', 'groups'] |
|
52 } |
|
53 |
|
54 const persistOptions = { |
43 const persistOptions = { |
|
44 key: 'root', |
55 storage: localForage, |
45 storage: localForage, |
56 records: [NoteRecord, SessionRecord, UserRecord, GroupRecord], |
|
57 transforms: [immutableTransform(immutableTransformConfig)], |
|
58 whitelist: ['sessions', 'notes', 'autoSubmit', 'authStatus', 'groups'] |
46 whitelist: ['sessions', 'notes', 'autoSubmit', 'authStatus', 'groups'] |
59 } |
47 } |
60 |
48 |
61 const apiClient = new APIClient(config.apiRootUrl); |
49 const apiClient = new APIClient(config.apiRootUrl); |
62 |
50 |
63 const storeInitialState = Immutable.Map({ ...defaultState }); |
51 export default (history, initialState = defaultState) => { |
64 |
52 |
65 |
53 const persistedReducer = connectRouter(history)(persistReducer(persistOptions, rootReducer)); |
66 export default (history, initialState = storeInitialState) => { |
|
67 |
|
68 const router = routerMiddleware(history); |
54 const router = routerMiddleware(history); |
69 const saga = createSagaMiddleware(); |
55 const saga = createSagaMiddleware(); |
70 |
56 |
71 const store = createStore(rootReducer, initialState, composeEnhancers( |
57 const store = createStore(persistedReducer, initialState, composeEnhancers( |
72 applyMiddleware(router, saga), |
58 applyMiddleware(router, saga), |
73 autoRehydrate() |
|
74 )); |
59 )); |
75 |
60 |
76 apiClient.setStore(store); |
61 apiClient.setStore(store); |
77 |
62 |
78 const context = { |
63 const context = { |
79 client: apiClient, |
64 client: apiClient, |
80 history |
65 history |
81 } |
66 } |
82 |
67 |
83 persistStore(store, persistOptions); |
68 const persistor = persistStore(store, null, () => { |
|
69 saga.run(rootAuthSaga, context); |
|
70 saga.run(rootGroupSaga, context); |
|
71 saga.run(rootSyncSaga, context); |
|
72 saga.run(networkSaga, context); |
|
73 }); |
84 |
74 |
85 saga.run(rootAuthSaga, context); |
|
86 saga.run(rootGroupSaga, context); |
|
87 saga.run(rootSyncSaga, context); |
|
88 saga.run(networkSaga, context); |
|
89 |
75 |
90 return store; |
76 return { store, persistor }; |
91 }; |
77 }; |