2016-06-18 20:30:45 +05:30
|
|
|
|
import { createStore, applyMiddleware, compose } from 'redux';
|
|
|
|
|
// midleware, который позволяет возвращать из экшенов функции
|
|
|
|
|
// это полезно для работы с асинхронными действиями,
|
|
|
|
|
// а также дает возможность проверить какие-либо условия перед запуском экшена
|
|
|
|
|
// или даже вообще его не запускать в зависимости от условий
|
|
|
|
|
import thunk from 'redux-thunk';
|
2016-10-31 12:21:38 +05:30
|
|
|
|
import persistState from 'redux-localstorage';
|
2016-06-18 20:30:45 +05:30
|
|
|
|
import { syncHistory } from 'react-router-redux';
|
|
|
|
|
import { browserHistory } from 'react-router';
|
|
|
|
|
|
|
|
|
|
import reducers from 'reducers';
|
|
|
|
|
|
|
|
|
|
export default function storeFactory() {
|
|
|
|
|
const reduxRouterMiddleware = syncHistory(browserHistory);
|
|
|
|
|
const middlewares = applyMiddleware(
|
|
|
|
|
reduxRouterMiddleware,
|
|
|
|
|
thunk
|
|
|
|
|
);
|
2016-10-31 12:21:38 +05:30
|
|
|
|
const persistStateEnhancer = persistState([
|
2016-11-05 15:41:41 +05:30
|
|
|
|
'accounts',
|
|
|
|
|
'user'
|
2016-10-31 12:21:38 +05:30
|
|
|
|
], {key: 'redux-storage'});
|
2016-06-18 20:30:45 +05:30
|
|
|
|
|
|
|
|
|
/* global process: false */
|
|
|
|
|
let enhancer;
|
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
2016-10-31 12:21:38 +05:30
|
|
|
|
enhancer = compose(middlewares, persistStateEnhancer);
|
2016-06-18 20:30:45 +05:30
|
|
|
|
} else {
|
2016-06-18 22:16:07 +05:30
|
|
|
|
const DevTools = require('containers/DevTools').default;
|
2016-10-31 12:21:38 +05:30
|
|
|
|
enhancer = compose(middlewares, persistStateEnhancer, DevTools.instrument());
|
2016-06-18 20:30:45 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const store = createStore(reducers, {}, enhancer);
|
|
|
|
|
|
|
|
|
|
// Hot reload reducers
|
|
|
|
|
if (module.hot) {
|
|
|
|
|
module.hot.accept('reducers', () =>
|
|
|
|
|
store.replaceReducer(require('reducers').default)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return store;
|
|
|
|
|
}
|