import { Hero } from '../../core' ; import * as fromHeroes from '../actions/heroes.actions' ; export interface HeroesState { data : Hero [ ] ; loaded : boolean ; loading : boolean ; } export const initialHeroesState = { data : [ ] , loaded : false , loading : false } ; param state param action export function reducer ( state = initialHeroesState , action ) { switch ( action . type ) { case fromHeroes . LOAD_HEROES : { return { ... state , loading : true } ; } case fromHeroes . LOAD_HEROES_SUCCESS : { return { ... state , data : action . payload , loaded : true , loading : false , } ; } case fromHeroes . LOAD_HEROES_ERROR : { return { ... state , loaded : false , loading : false , } ; } case fromHeroes . ADD_HERO : { return state ; } case fromHeroes . ADD_HERO_SUCCESS : { const added = action . payload ; return - 1 < state . data . findIndex ( e => e . id === added . id ) ? state : { ... state , data : [ ... state . data , added ] } ; } case fromHeroes . ADD_HERO_ERROR : { return state ; } case fromHeroes . DELETE_HERO : { const deleteId = action . payload ; return { ... state , data : state . data . filter ( h => h . id !== deleteId ) } ; } case fromHeroes . UPDATE_HERO_SUCCESS : { const updated = action . payload ; const id = updated && updated . id ; return { ... state , data : state . data . map ( h => h . id === id ? updated : h ) } ; } } return state ; }