import { compose } from '@ngrx/core/compose'; import { ActionReducer, combineReducers, Action } from '@ngrx/store'; import { storeLogger } from 'ngrx-store-logger'; import * as fromRouter from '@ngrx/router-store'; import { LibState, LIB_REDUCERS } from '../../lib/lib-reducers'; import { storeFreeze } from 'ngrx-store-freeze'; import { environment } from '../../environments/environment'; export interface CustomAction extends Action { type: string; payload?: any; } export interface AppState extends LibState { routerReducer: fromRouter.RouterReducerState; } export const reducers = Object.assign({}, { router: fromRouter.routerReducer, }, LIB_REDUCERS); // Generate a reducer to set the root state in dev mode for HMR function stateSetter (reducer: ActionReducer): ActionReducer { return function (state, action: CustomAction) { if (action.type === 'SET_ROOT_STATE') { return action.payload; } return reducer(state, action); }; } const DEV_REDUCERS = [stateSetter, storeFreeze]; // TODO: adjust STORE_DEV_TOOLS // if (['logger', 'both'].includes(STORE_DEV_TOOLS)) { // set in constants.js file of project root // DEV_REDUCERS.push(storeLogger()); // } const developmentReducer = compose(...DEV_REDUCERS, combineReducers)(reducers); const productionReducer = combineReducers(reducers); export function rootReducer (state: any, action: any) { if (environment.production) { return productionReducer(state, action); } else { return developmentReducer(state, action); } }