client/src/store/configureStore.js
changeset 62 b2514a9bcd49
parent 59 1eb52770eefa
child 67 9206af01f5e5
equal deleted inserted replaced
61:7586b4a11c32 62:b2514a9bcd49
     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 { compose, createStore, applyMiddleware } from 'redux';
     4 import { createStore, applyMiddleware, compose } from 'redux';
       
     5 import { routerMiddleware } from 'react-router-redux';
     4 import { routerMiddleware } from 'react-router-redux';
     6 import handleTransitions from 'redux-history-transitions';
       
     7 import createSagaMiddleware from 'redux-saga'
     5 import createSagaMiddleware from 'redux-saga'
     8 import Immutable from 'immutable';
     6 import Immutable from 'immutable';
     9 import APIClient from '../APIClient';
     7 import { offline } from 'redux-offline';
       
     8 import offlineDefaultConfig from 'redux-offline/lib/defaults';
       
     9 import localForage from 'localforage';
       
    10 import immutableTransform from 'redux-persist-transform-immutable'
       
    11 import NoteRecord from './noteRecord'
       
    12 import SessionRecord from './sessionRecord'
       
    13 import APIClient from '../api/APIClient';
       
    14 import createEffect from '../api';
    10 import config from '../config';
    15 import config from '../config';
    11 
    16 
    12 const token = localStorage.getItem('token');
    17 // const composeEnhancers = (process.env.NODE_ENV !== 'production' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) ?
    13 const currentUser = localStorage.getItem('currentUser');
    18 //     window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
       
    19 //         shouldHotReload: false,
       
    20 //     }) : compose;
       
    21 const composeEnhancers = compose;
       
    22 
    14 
    23 
    15 const defaultState = {
    24 const defaultState = {
    16   currentSession: null,
       
    17   sessions: Immutable.List([]),
    25   sessions: Immutable.List([]),
    18   notes: Immutable.List([]),
    26   notes: Immutable.List([]),
    19   isAuthenticated: token !== null,
    27   isAuthenticated: false,
    20   currentUser: currentUser ? JSON.parse(currentUser) : null,
    28   currentUser: null,
    21   token: token,
    29   token: '',
    22   login: {
    30   login: Immutable.Map({
    23     loading: false,
    31     loading: false,
    24     success: false,
    32     success: false,
    25     error: false,
    33     error: false,
    26   }
    34   })
    27 };
    35 };
    28 
    36 
    29 const storeInitialState = Immutable.Map(defaultState);
    37 const immutableTransformConfig = {
       
    38   records: [NoteRecord, SessionRecord],
       
    39   whitelist: ['sessions', 'notes', 'currentUser']
       
    40 }
       
    41 
       
    42 const persistOptions = {
       
    43   storage: localForage,
       
    44   transforms: [immutableTransform(immutableTransformConfig)],
       
    45   whitelist: ['sessions', 'notes', 'isAuthenticated', 'currentUser', 'token']
       
    46 }
       
    47 
       
    48 const apiClient = new APIClient(config.apiRootUrl);
       
    49 
       
    50 const offlineConfig = {
       
    51   ...offlineDefaultConfig,
       
    52   persistOptions,
       
    53   effect: createEffect(apiClient)
       
    54 //  detectNetwork: callback => callback(true),
       
    55 }
       
    56 
       
    57 const storeInitialState = { ...defaultState };
       
    58 
    30 
    59 
    31 export default (history, initialState = storeInitialState) => {
    60 export default (history, initialState = storeInitialState) => {
    32 
    61 
    33   const router = routerMiddleware(history);
    62   const router = routerMiddleware(history);
    34   const saga = createSagaMiddleware();
    63   const saga = createSagaMiddleware();
    35   const transitions = handleTransitions(history);
       
    36 
    64 
    37   const store = createStore(rootReducer, initialState, compose(
    65   const store = offline(offlineConfig)(createStore)(rootReducer, initialState, composeEnhancers(
    38     applyMiddleware(router, saga),
    66     applyMiddleware(router, saga)
    39     transitions
       
    40   ));
    67   ));
    41 
    68 
       
    69   apiClient.setStore(store);
       
    70 
    42   const context = {
    71   const context = {
    43       client: new APIClient(config.apiRootUrl, store)
    72     client: apiClient,
       
    73     history
    44   }
    74   }
    45 
    75 
    46   saga.run(rootSaga, context);
    76   saga.run(rootSaga, context);
    47 
    77 
    48   store.dispatch(loadSessions());
       
    49 
       
    50   return store;
    78   return store;
    51 };
    79 };