import { Ingredient } from '../../../shared/models/ingredient.model' ; import * as ingredientActions from '../actions/ingredients.actions' ; import * as signalrActions from '../actions/signalR.actions' ; import { Action , createReducer , on } from '@ngrx/store' ; export interface IngredientReducerState { entities : { [ id ] : Ingredient ; } ; loaded : boolean ; loading : boolean ; } export const initialState = { entities : { } , loaded : false , loading : false } ; const ingredientsReducerinternal = createReducer ( initialState , on ( signalrActions . receivedIngredientAdded , ( state , { payload } ) => { if ( ! ! state . entities [ payload . id ] ) { return state ; } const ingredient = payload ; const entities = { ... state . entities , [ ingredient . id ] : ingredient } ; return { ... state , entities } ; } ) , on ( signalrActions . receivedIngredientDeleted , ( state , { payload } ) => { if ( ! state . entities [ payload ] ) { return state ; } const { [ payload ] : removed , ... entities } = state . entities ; return { ... state , entities } ; } ) , on ( ingredientActions . addIngredientSuccess , ( state , { payload } ) => { const ingredient = payload ; const entities = { ... state . entities , [ ingredient . id ] : ingredient } ; return { ... state , entities } ; } ) , on ( ingredientActions . deleteIngredientSuccess , ( state , { payload } ) => { const ingredient = payload ; const { [ ingredient . id ] : removed , ... entities } = state . entities ; return { ... state , entities } ; } ) , on ( ingredientActions . loadIngredientsSuccess , ( state , { payload } ) => { const entities = { } ; for ( const entity of payload ) { entities [ entity . id ] = entity ; } return { ... state , entities , loaded : true } ; } ) ) ; export const getIngredientItemEntities = ( state ) => state . entities ; export const getIngredientsLoaded = ( state ) => state . loaded ; export const getIngredientsLoading = ( state ) => state . loading ; export function ingredientReducer ( state , action ) { return ingredientsReducerinternal ( state , action ) ; }