/* Imports from Redux: applyMiddleware: Applies middleware to the dispatch method of the Redux store combineReducers: Merges reducers into one createStore: Creates a Redux store that holds the state tree Store: The TS Type used for the store, or state tree */ import {connectRouter, routerMiddleware, RouterState} from "connected-react-router"; import { createBrowserHistory, History, LocationState } from "history"; import throttle from "lodash.throttle"; import { applyMiddleware, combineReducers, createStore, Store } from "redux"; /* Dev-Tools for debugging and visualizing Redux State in browser */ import { composeWithDevTools } from "redux-devtools-extension"; /* Thunk Redux Thunk middleware allows you to write action creators that return a function instead of an action. The thunk can be used to delay the dispatch of an action, or to dispatch only if a certain condition is met. The inner function receives the store methods dispatch and getState as parameters. */ import thunk from "redux-thunk"; // Import reducers and state type import { alertReducer, IAlertState } from "../reducers/AlertReducer"; import { auctionAssignmentReducer, IAuctionAssignmentState } from "../reducers/AuctionAssignmentReducer"; import { auctionHouseReducer, IAuctionHouseState } from "../reducers/AuctionHouseReducer"; import { factoidReducer, IFactoidState } from "../reducers/FactoidReducer"; import { IRepoAccountState, repoAccountReducer } from "../reducers/RepoAccountReducer"; import { IRepoCompanyState, repoCompanyReducer } from "../reducers/RepoCompanyReducer"; import { ICollectionsAccountState, collectionsAccountReducer } from "../reducers/CollectionsAccountReducer"; import { IReportingState, reportingReducer } from "../reducers/ReportingReducer"; import { ISavedLocationState, savedLocationReducer } from "../reducers/SavedLocationReducer"; import { ISecurityState, securityReducer } from "../reducers/SecurityReducer"; import { ISettingsState, settingsReducer } from "../reducers/SettingsReducer"; import { IVehicleFloorPricingParametersState, vehicleFloorPricingParametersReducer } from "../reducers/VehicleFloorPricingParametersReducer"; import LocalStorageService from "../services/LocalStorageService"; import Configuration from "../shared/configuration/Configuration"; // Create an interface for the application state export interface IAppState { alertState: IAlertState; auctionAssignmentState: IAuctionAssignmentState; auctionHouseState: IAuctionHouseState; repoAccountState: IRepoAccountState; repoCompanyState: IRepoCompanyState; collectionsAccountState: ICollectionsAccountState; settingsState: ISettingsState; vehicleFloorPricingParametersState: IVehicleFloorPricingParametersState; securityState: ISecurityState; reportingState: IReportingState; factoidState: IFactoidState; savedLocationState: ISavedLocationState; router: RouterState & LocationState; } // Create the root reducer export const rootReducer = (history: History) => combineReducers({ alertState: alertReducer, auctionAssignmentState: auctionAssignmentReducer, auctionHouseState: auctionHouseReducer, repoAccountState: repoAccountReducer, repoCompanyState: repoCompanyReducer, collectionsAccountState: collectionsAccountReducer, settingsState: settingsReducer, vehicleFloorPricingParametersState: vehicleFloorPricingParametersReducer, securityState: securityReducer, reportingState: reportingReducer, factoidState: factoidReducer, savedLocationState: savedLocationReducer, router: connectRouter(history) }); export const AppRouteHistory = createBrowserHistory(); export default function configureStore(initState: any): Store { const enhancer = composeWithDevTools( applyMiddleware(routerMiddleware(AppRouteHistory), thunk ) ); const store = createStore(rootReducer(AppRouteHistory), initState, enhancer); store.subscribe(throttle(() => { LocalStorageService.saveAppState(store.getState(),Configuration.Get.statesToSave); }, 2000)); return store; }