import { HttpErrorResponse } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; import { Actions , Effect , ofType } from '@ngrx/effects' ; import { DataPersistence } from '@nrwl/angular' ; import { map } from 'rxjs/operators' ; import { RepositoryDataService } from '../services/repository-data.service' ; import { fromRepositoryActions } from './repository.actions' ; import { RepositoryPartialState } from './repository.reducer' ; @ Injectable ( ) export class RepositoryEffects { @ Effect ( ) getRepositoryCollection$ = this . dp . fetch ( fromRepositoryActions . Types . GetRepositoryCollection , { run : ( action ) => { return this . repositoryDataService . getRepositoryCollection ( ) . pipe ( map ( data => new fromRepositoryActions . GetRepositoryCollectionSuccess ( data ) ) ) ; } , onError : ( action , error ) => { return new fromRepositoryActions . GetRepositoryCollectionFail ( error ) ; } } ) ; @ Effect ( ) addRepository$ = this . dp . fetch ( fromRepositoryActions . Types . AddRepository , { run : ( action ) => { return this . repositoryDataService . addRepository ( action . payload ) . pipe ( map ( resp => new fromRepositoryActions . AddRepositorySuccess ( { repository : resp } ) ) ) ; } , onError : ( action , error ) => { return new fromRepositoryActions . AddRepositoryFail ( error ) ; } } ) ; @ Effect ( ) addRepositorySuccess$ = this . actions$ . pipe ( ofType ( fromRepositoryActions . Types . AddRepositorySuccess ) , map ( ( ) => new fromRepositoryActions . GetRepositoryCollection ( ) ) ) ; @ Effect ( ) deleteRepository$ = this . dp . fetch ( fromRepositoryActions . Types . DeleteRepository , { run : ( action ) => { return this . repositoryDataService . deleteRepository ( action . payload ) . pipe ( map ( ( ) => new fromRepositoryActions . DeleteRepositorySuccess ( ) ) ) ; } , onError : ( action , error ) => { return new fromRepositoryActions . DeleteRepositoryFail ( error ) ; } } ) ; @ Effect ( ) deleteRepositorySuccess$ = this . actions$ . pipe ( ofType ( fromRepositoryActions . Types . DeleteRepositorySuccess ) , map ( ( ) => new fromRepositoryActions . GetRepositoryCollection ( ) ) ) ; @ Effect ( ) editRepository$ = this . dp . fetch ( fromRepositoryActions . Types . EditRepository , { run : ( action ) => { return this . repositoryDataService . editRepository ( action . payload ) . pipe ( map ( ( ) => new fromRepositoryActions . EditRepositorySuccess ( ) ) ) ; } , onError : ( action , error ) => { return new fromRepositoryActions . EditRepositoryFail ( error ) ; } } ) ; @ Effect ( ) editRepositorySuccess$ = this . actions$ . pipe ( ofType ( fromRepositoryActions . Types . EditRepositorySuccess ) , map ( ( ) => new fromRepositoryActions . GetRepositoryCollection ( ) ) ) ; constructor ( private dp , private repositoryDataService , private actions$ ) { } }