Skip to content
Snippets Groups Projects
app.reducer.ts 976 B
Newer Older
  • Learn to ignore specific revisions
  • François Agneray's avatar
    François Agneray committed
    import { ActionReducerMap, ActionReducer, MetaReducer } from '@ngrx/store';
    
    import { environment } from '../environments/environment';
    import * as fromRouter from '@ngrx/router-store';
    
    export interface State {
    
    François Agneray's avatar
    François Agneray committed
        router: fromRouter.RouterReducerState;
    
    François Agneray's avatar
    François Agneray committed
    }
    
    export const reducers: ActionReducerMap<State> = {
    
    François Agneray's avatar
    François Agneray committed
        router: fromRouter.routerReducer
    
    François Agneray's avatar
    François Agneray committed
    };
    
    export function logger(reducer: ActionReducer<State>): ActionReducer<State> {
    
    Tifenn Guillas's avatar
    Tifenn Guillas committed
        return (state: State, action: any): State => {
    
    François Agneray's avatar
    François Agneray committed
            console.group(action.type);
            const nextState = reducer(state, action);
            console.log(`%c prev state`, `color: #9E9E9E; font-weight: bold`, state);
            console.log(`%c action`, `color: #03A9F4; font-weight: bold`, action);
            console.log(`%c next state`, `color: #4CAF50; font-weight: bold`, nextState);
            console.groupEnd();
    
            return nextState;
        };
    }
    
    export const metaReducers: MetaReducer<State>[] = !environment.production
        ? [logger]
        : [];