import { Injectable , Inject , } from '@angular/core' ; import { Actions , Effect , ofType , } from '@ngrx/effects' ; import { of } from 'rxjs' ; import { switchMap , map , catchError , } from 'rxjs/operators' ; import { DaffError } from '@daffodil/core' ; import { ErrorTransformer } from '@daffodil/core/state' ; import { DaffCountry , DAFF_GEOGRAPHY_ERROR_MATCHER , } from '@daffodil/geography' ; import { DaffGeographyServiceInterface , DaffGeographyDriver , } from '@daffodil/geography/driver' ; import { DaffGeographyActionTypes , DaffCountryLoad , DaffCountryLoadSuccess , DaffCountryLoadFailure , DaffCountryList , DaffCountryListSuccess , DaffCountryListFailure , } from '../actions/public_api' ; @ Injectable ( ) export class DaffGeographyEffects < T extends DaffCountry > { constructor ( private actions$ , @ Inject ( DaffGeographyDriver ) private driver , @ Inject ( DAFF_GEOGRAPHY_ERROR_MATCHER ) private errorMatcher ) { } @ Effect ( ) get$ = this . actions$ . pipe ( ofType ( DaffGeographyActionTypes . CountryLoadAction ) , switchMap ( ( action ) => this . driver . get ( action . payload ) . pipe ( map ( resp => new DaffCountryLoadSuccess ( resp ) ) , catchError ( ( error ) => of ( new DaffCountryLoadFailure ( this . errorMatcher ( error ) ) ) ) ) ) ) ; @ Effect ( ) list$ = this . actions$ . pipe ( ofType ( DaffGeographyActionTypes . CountryListAction ) , switchMap ( ( action ) => this . driver . list ( ) . pipe ( map ( resp => new DaffCountryListSuccess ( resp ) ) , catchError ( ( error ) => of ( new DaffCountryListFailure ( this . errorMatcher ( error ) ) ) ) ) ) ) ; }