import { createReducer , on } from '@ngrx/store' ; import { StageActions } from '../actions' ; import { Animal } from '../models' ; export const stageFeatureKey = 'stage' ; export interface State { animals : Animal [ ] ; score : number ; display : boolean ; gameState : string ; } const initialState = { animals : [ ] , score : 0 , display : false , gameState : 'PENDING' , } ; export const reducer = createReducer ( initialState , on ( StageActions . setGameState , ( state , { gameState } ) => ( { ... state , gameState } ) ) , on ( StageActions . clearAnimalList , ( state ) => ( { ... state , animals : [ ] } ) ) , on ( StageActions . appendAnimalToList , ( state , { animal } ) => ( { ... state , animals : [ ... state . animals , animal ] } ) ) , on ( StageActions . deleteAnimalFromList , ( state , { code } ) => { const idx = state . animals . findIndex ( ( animal ) => animal . code === code ) ; state . animals . splice ( idx , 1 ) ; return { ... state , animals : [ ... state . animals ] } ; } ) ) ; export const selectScore = ( state ) : number => state . score ; export const selectGameState = ( state ) : string => state . gameState ; export const selectAnimalsList = ( state ) : Animal [ ] => state . animals ;