62 lines
1.8 KiB
JavaScript
Raw Normal View History

2016-08-18 11:41:13 +02:00
/**
2017-08-21 15:12:53 +02:00
* Create the store with dynamic reducers
2016-08-18 11:41:13 +02:00
*/
import { createStore, applyMiddleware, compose } from 'redux';
import { fromJS } from 'immutable';
import { routerMiddleware } from 'react-router-redux';
import createSagaMiddleware from 'redux-saga';
import createReducer from './reducers';
const sagaMiddleware = createSagaMiddleware();
export default function configureStore(initialState = {}, history) {
// Create the store with two middlewares
// 1. sagaMiddleware: Makes redux-sagas work
// 2. routerMiddleware: Syncs the location/URL path to the state
const middlewares = [
sagaMiddleware,
routerMiddleware(history),
];
const enhancers = [
applyMiddleware(...middlewares),
];
2017-08-21 15:12:53 +02:00
// If Redux DevTools Extension is installed use it, otherwise use Redux compose
/* eslint-disable no-underscore-dangle */
const composeEnhancers =
process.env.NODE_ENV !== 'production' &&
typeof window === 'object' &&
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
// TODO Try to remove when `react-router-redux` is out of beta, LOCATION_CHANGE should not be fired more than once after hot reloading
// Prevent recomputing reducers for `replaceReducer`
shouldHotReload: false,
name: `Strapi - Dashboard`,
2017-08-21 15:12:53 +02:00
})
: compose;
/* eslint-enable */
2016-08-18 11:41:13 +02:00
const store = createStore(
createReducer(),
fromJS(initialState),
2017-08-21 15:12:53 +02:00
composeEnhancers(...enhancers)
2016-08-18 11:41:13 +02:00
);
2017-08-21 15:12:53 +02:00
// Extensions
2016-08-18 11:41:13 +02:00
store.runSaga = sagaMiddleware.run;
2017-08-21 15:12:53 +02:00
store.injectedReducers = {}; // Reducer registry
store.injectedSagas = {}; // Saga registry
2016-08-18 11:41:13 +02:00
// Make reducers hot reloadable, see http://mxs.is/googmo
/* istanbul ignore next */
if (module.hot) {
2017-08-21 15:12:53 +02:00
module.hot.accept('./reducers', () => {
store.replaceReducer(createReducer(store.injectedReducers));
2016-08-18 11:41:13 +02:00
});
}
return store;
}