import { StoreDevtoolsModule } from '@ngrx/store-devtools'; import { EffectsModule } from '@ngrx/effects'; import { MetaReducer, StoreModule, ActionReducer } from '@ngrx/store'; import { storeLogger } from 'ngrx-store-logger'; import { NgModule } from '@angular/core'; import { localStorageSync } from 'ngrx-store-localstorage'; import { environment } from 'projects/core/src//environment'; import { routerReducer, StoreRouterConnectingModule, ROUTER_REQUEST, RouterRequestAction, } from '@ngrx/router-store'; import { userFeatureKey } from 'projects/core/src//auth/@store/user.reducer'; import { logOut } from 'projects/core/src//auth/@store/user.actions'; import { OIDC_STORAGE_KEY } from 'projects/core/src//auth/@store/oidc.effects'; export function localStorageSyncReducer( reducer: ActionReducer ): ActionReducer { return localStorageSync({ keys: [OIDC_STORAGE_KEY, userFeatureKey, ...environment.syncLocalStorage], rehydrate: true, })(reducer); } export function logger(reducer: ActionReducer): ActionReducer { // default, no options return storeLogger()(reducer); } export function resetState( reducer: (state: any, action: any) => any ): (state: any, action: any) => any { return (state, action): any => { let isLoginPreloader = false; let isLogout = false; if (action.type === ROUTER_REQUEST) { const navigationAction = action as RouterRequestAction; isLoginPreloader = navigationAction.payload.event.url.indexOf('/login-preloader') === 0; } if (action.type === logOut.type) { isLogout = true; } if (isLoginPreloader || isLogout) { state = undefined; } return reducer(state, action); }; } export const metaReducers: MetaReducer[] = [ ...(environment.production ? [] : [logger]), resetState, localStorageSyncReducer, ]; const devModules = environment.production ? [] : [ StoreDevtoolsModule.instrument({ maxAge: 10, }), ]; @NgModule({ imports: [ StoreModule.forRoot( { router: routerReducer, }, { metaReducers, runtimeChecks: { strictStateImmutability: true, strictActionImmutability: true, }, } ), EffectsModule.forRoot([]), // Connects RouterModule with StoreModule StoreRouterConnectingModule.forRoot(), ...devModules, ], providers: [], }) export class MwCoreStoreModule {}