accounts-frontend/src/storeFactory.js
2016-11-05 12:11:41 +02:00

44 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { createStore, applyMiddleware, compose } from 'redux';
// midleware, который позволяет возвращать из экшенов функции
// это полезно для работы с асинхронными действиями,
// а также дает возможность проверить какие-либо условия перед запуском экшена
// или даже вообще его не запускать в зависимости от условий
import thunk from 'redux-thunk';
import persistState from 'redux-localstorage';
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
);
const persistStateEnhancer = persistState([
'accounts',
'user'
], {key: 'redux-storage'});
/* global process: false */
let enhancer;
if (process.env.NODE_ENV === 'production') {
enhancer = compose(middlewares, persistStateEnhancer);
} else {
const DevTools = require('containers/DevTools').default;
enhancer = compose(middlewares, persistStateEnhancer, DevTools.instrument());
}
const store = createStore(reducers, {}, enhancer);
// Hot reload reducers
if (module.hot) {
module.hot.accept('reducers', () =>
store.replaceReducer(require('reducers').default)
);
}
return store;
}