import * as homeActions from '../actions/home.actions' ; import { on , createReducer , Action } from '@ngrx/store' ; import { FoodItem } from '@app/shared/models/foodItem.model' ; export interface HomeReduerState { randomMeal : { [ id ] : FoodItem ; } ; loading : boolean ; loaded : boolean ; } export const initialState = { randomMeal : { } , loading : false , loaded : false } ; const homeReducerInternal = createReducer ( initialState , on ( homeActions . loadRandomMeal , ( state , { } ) => { return { ... state , loading : true } ; } ) , on ( homeActions . loadRandomMealSuccess , ( state , { payload } ) => { const values = payload . value ; const randomMeal = { } ; values . forEach ( ( item ) => { if ( item ) { randomMeal [ item . id ] = item ; } else { randomMeal [ '' ] = null ; } } ) ; return { ... state , randomMeal , loaded : true , loading : false } ; } ) ) ; export function homeReducer ( state , action ) { return homeReducerInternal ( state , action ) ; } export const getRandomMeal = ( state ) => state . randomMeal ; export const getRandomMealLoaded = ( state ) => state . loaded ; export const getRandomMealLoading = ( state ) => state . loading ;