import { Injectable } from '@angular/core' ; import { Actions , Effect , ofType } from '@ngrx/effects' ; import { of } from 'rxjs' ; import { catchError , concatMap , map } from 'rxjs/operators' ; import { HeroesDataService } from '../../data-services' ; import * as HeroActions from '../actions' ; import { HeroAction } from '../actions' ; import { DeleteHeroSuccess , DeleteHero , AddHero } from '../actions' ; @ Injectable ( ) export class HeroesEffects { constructor ( private actions$ , private dataService ) { } @ Effect ( ) loadHeroes$ = this . actions$ . pipe ( ofType ( HeroActions . LOAD_HEROES ) , concatMap ( ( ) => this . dataService . getAll ( ) . pipe ( map ( heroes => new HeroActions . LoadHeroesSuccess ( heroes ) ) , catchError ( error => of ( new HeroActions . LoadHeroesError ( error ) ) ) ) ) ) ; @ Effect ( ) addHero$ = this . actions$ . pipe ( ofType ( HeroActions . ADD_HERO ) , concatMap ( ( action ) => this . dataService . add ( action . payload ) . pipe ( map ( hero => new HeroActions . AddHeroSuccess ( hero ) ) , catchError ( error => of ( new HeroActions . AddHeroError ( error ) ) ) ) ) ) ; @ Effect ( ) deleteHero$ = this . actions$ . pipe ( ofType ( HeroActions . DELETE_HERO ) , concatMap ( ( action ) => this . dataService . delete ( action . payload ) . pipe ( map ( hero => new HeroActions . DeleteHeroSuccess ( ) ) , catchError ( error => of ( new HeroActions . DeleteHeroError ( error ) ) ) ) ) ) ; @ Effect ( ) updateHero$ = this . actions$ . pipe ( ofType ( HeroActions . UPDATE_HERO ) , concatMap ( ( action ) => this . dataService . update ( action . payload ) . pipe ( map ( hero => new HeroActions . UpdateHeroSuccess ( hero || action . payload ) ) , catchError ( error => of ( new HeroActions . UpdateHeroError ( error ) ) ) ) ) ) ; }