Files
bigcapital/client/src/store/createStore.js
2020-11-24 11:21:42 +02:00

58 lines
1.7 KiB
JavaScript

import {
createStore as createReduxStore,
applyMiddleware,
compose,
} from 'redux';
import thunkMiddleware from 'redux-thunk';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import monitorReducerEnhancer from 'store/enhancers/monitorReducer';
import loggerMiddleware from 'middleware/logger';
import rootReducer from 'store/reducers';
const persistConfig = {
key: 'bigcapital:root',
blacklist: ['dashboard'],
storage,
};
const createStoreFactory = (initialState = {}) => {
/**
|--------------------------------------------------
| Middleware Configuration
|--------------------------------------------------
*/
const middleware = [thunkMiddleware, loggerMiddleware];
/**
|--------------------------------------------------
| Store Enhancers
|--------------------------------------------------
*/
const enhancers = [monitorReducerEnhancer];
let composeEnhancers = compose;
if (process.env.NODE_ENV === 'development') {
if (typeof window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ === 'function') {
composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__;
}
}
/**
|--------------------------------------------------
| Store Instantiation and HMR Setup
|--------------------------------------------------
*/
const store = createReduxStore(
persistReducer(persistConfig, rootReducer),
initialState,
composeEnhancers(applyMiddleware(...middleware), ...enhancers),
);
store.asyncReducers = {};
return store;
};
export const createStore = createStoreFactory;
export const store = createStoreFactory();
export const persistor = persistStore(store);