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