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