|
| 1 | +import { createStore, compose, combineReducers, applyMiddleware } from 'redux'; |
| 2 | +import { persistState } from 'redux-devtools'; |
| 3 | +import { routerReducer } from 'react-router-redux'; |
| 4 | +import createLogger from 'redux-logger'; |
| 5 | +import thunkMiddleware from 'redux-thunk'; |
| 6 | +import * as reducers from '../modules/reducers'; |
| 7 | +import DevTools from '../devTools/DevTools.jsx'; |
| 8 | + |
| 9 | + |
| 10 | +const loggerMiddleware = createLogger({ |
| 11 | + level : 'info', |
| 12 | + collapsed : true |
| 13 | +}); |
| 14 | + |
| 15 | +// createStore : enhancer |
| 16 | +const enhancer = compose( |
| 17 | + applyMiddleware(thunkMiddleware, loggerMiddleware), // logger after thunk to avoid undefined actions |
| 18 | + persistState(getDebugSessionKey()), |
| 19 | + DevTools.instrument() |
| 20 | +); |
| 21 | + |
| 22 | +function getDebugSessionKey() { |
| 23 | + const matches = window.location.href.match(/[?&]debug_session=([^&]+)\b/); |
| 24 | + return (matches && matches.length > 0)? matches[1] : null; |
| 25 | +} |
| 26 | + |
| 27 | +// combine reducers -> createStore reducer |
| 28 | +const reducer = combineReducers({ |
| 29 | + ...reducers, |
| 30 | + routing: routerReducer |
| 31 | +}); |
| 32 | + |
| 33 | +export default function configureStore(initialState) { |
| 34 | + const store = createStore(reducer, initialState, enhancer); |
| 35 | + // checks if webpack HMR: |
| 36 | + if (module.hot) { |
| 37 | + module.hot.accept('../modules/reducers', () => |
| 38 | + store.replaceReducer(require('../modules/reducers').default) |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + return store; |
| 43 | +} |
0 commit comments