import { createStore, applyMiddleware, compose } from 'redux'; import thunk from 'redux-thunk'; import { createHashHistory } from 'history'; import { routerMiddleware, routerActions } from 'connected-react-router'; import { createLogger } from 'redux-logger'; import createRootReducer from '../reducers'; import * as pageActions from '../actions/pageName'; import * as authActions from '../actions/auth'; import * as togglesActions from '../actions/toggle'; import * as usersActions from '../actions/users'; import * as productsActions from '../actions/products'; import * as invoiceActions from '../actions/invoice'; import * as reportsActions from '../actions/reports'; import { stateType } from '../reducers/types'; declare global { interface Window { __REDUX_DEVTOOLS_EXTENSION_COMPOSE__: ( // eslint-disable-next-line @typescript-eslint/no-explicit-any obj: Record ) => Function; } interface NodeModule { hot?: { accept: (path: string, cb: () => void) => void; }; } } const history = createHashHistory(); const rootReducer = createRootReducer(history); const configureStore = (initialState?: stateType) => { // Redux Configuration const middleware = []; const enhancers = []; // Thunk Middleware middleware.push(thunk); // Logging Middleware const logger = createLogger({ level: 'info', collapsed: true }); // Skip redux logs in console during the tests if (process.env.NODE_ENV !== 'test') { middleware.push(logger); } // Router Middleware const router = routerMiddleware(history); middleware.push(router); // Redux DevTools Configuration const actionCreators = { ...pageActions, ...authActions, ...togglesActions, ...usersActions, ...routerActions, ...productsActions, ...invoiceActions, ...reportsActions }; // If Redux DevTools Extension is installed use it, otherwise use Redux compose /* eslint-disable no-underscore-dangle */ const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({ // Options: http://extension.remotedev.io/docs/API/Arguments.html actionCreators }) : compose; /* eslint-enable no-underscore-dangle */ // Apply Middleware & Compose Enhancers enhancers.push(applyMiddleware(...middleware)); const enhancer = composeEnhancers(...enhancers); // Create Store const store = createStore(rootReducer, initialState, enhancer); if (module.hot) { module.hot.accept( '../reducers', // eslint-disable-next-line global-require () => store.replaceReducer(require('../reducers').default) ); } return store; }; export default { configureStore, history };