1 import rootReducer from '../reducers'; |
1 import rootReducer from '../reducers'; |
2 import rootSaga from '../sagas'; |
2 import rootSaga from '../sagas'; |
3 import { loadSessions } from '../actions/sessionsActions'; |
3 import { loadSessions } from '../actions/sessionsActions'; |
4 import { compose, createStore, applyMiddleware } from 'redux'; |
4 import { createStore, applyMiddleware, compose } from 'redux'; |
5 import { routerMiddleware } from 'react-router-redux'; |
5 import { routerMiddleware } from 'react-router-redux'; |
6 import handleTransitions from 'redux-history-transitions'; |
6 import handleTransitions from 'redux-history-transitions'; |
7 import createSagaMiddleware from 'redux-saga' |
7 import createSagaMiddleware from 'redux-saga' |
8 import Immutable from 'immutable'; |
8 import Immutable from 'immutable'; |
9 import { offline } from 'redux-offline'; |
|
10 import offlineDefaultConfig from 'redux-offline/lib/defaults'; |
|
11 import localForage from 'localforage'; |
|
12 import immutableTransform from 'redux-persist-transform-immutable' |
|
13 import NoteRecord from './noteRecord' |
|
14 import APIClient from '../APIClient'; |
9 import APIClient from '../APIClient'; |
15 import config from '../config'; |
10 import config from '../config'; |
16 |
11 |
17 const token = localStorage.getItem('token'); |
12 const token = localStorage.getItem('token'); |
18 const currentUser = localStorage.getItem('currentUser'); |
13 const currentUser = localStorage.getItem('currentUser'); |
19 |
14 |
20 const defaultState = { |
15 const defaultState = { |
21 // currentSession: null, |
16 currentSession: null, |
22 sessions: Immutable.List([]), |
17 sessions: Immutable.List([]), |
23 notes: Immutable.List([]), |
18 notes: Immutable.List([]), |
24 isAuthenticated: token !== null, |
19 isAuthenticated: token !== null, |
25 currentUser: currentUser ? JSON.parse(currentUser) : null, |
20 currentUser: currentUser ? JSON.parse(currentUser) : null, |
26 token: token, |
21 token: token, |
29 success: false, |
24 success: false, |
30 error: false, |
25 error: false, |
31 } |
26 } |
32 }; |
27 }; |
33 |
28 |
34 const immutableTransformConfig = { |
29 const storeInitialState = Immutable.Map(defaultState); |
35 records: [NoteRecord], |
|
36 whitelist: ['sessions', 'notes'] |
|
37 } |
|
38 |
|
39 const offlineConfig = { |
|
40 ...offlineDefaultConfig, |
|
41 persistOptions: { |
|
42 storage: localForage, |
|
43 transforms: [immutableTransform(immutableTransformConfig)] |
|
44 }, |
|
45 detectNetwork: callback => callback(true), |
|
46 } |
|
47 |
|
48 //const storeInitialState = Immutable.Map(defaultState); |
|
49 //const storeInitialState = new Map(defaultState); |
|
50 const storeInitialState = defaultState; |
|
51 |
30 |
52 export default (history, initialState = storeInitialState) => { |
31 export default (history, initialState = storeInitialState) => { |
53 |
32 |
54 const router = routerMiddleware(history); |
33 const router = routerMiddleware(history); |
55 const saga = createSagaMiddleware(); |
34 const saga = createSagaMiddleware(); |
56 const transitions = handleTransitions(history); |
35 const transitions = handleTransitions(history); |
57 |
36 |
58 const store = offline(offlineConfig)(createStore)(rootReducer, initialState, compose( |
37 const store = createStore(rootReducer, initialState, compose( |
59 applyMiddleware(router, saga), |
38 applyMiddleware(router, saga), |
60 transitions |
39 transitions |
61 )); |
40 )); |
62 |
41 |
63 const context = { |
42 const context = { |
64 client: new APIClient(config.apiRootUrl, store) |
43 client: new APIClient(config.apiRootUrl, store) |
65 } |
44 } |
66 |
45 |
67 saga.run(rootSaga, context); |
46 saga.run(rootSaga, context); |
68 |
47 |
69 //store.dispatch(loadSessions()); |
48 store.dispatch(loadSessions()); |
70 |
49 |
71 return store; |
50 return store; |
72 }; |
51 }; |