import { isPlatformBrowser } from '@angular/common' ; import { Inject , Injectable , PLATFORM_ID } from '@angular/core' ; import { Effect } from '@ngrx/effects' ; import { DataPersistence } from '@nrwl/angular' ; import { ApolloError } from 'apollo-client' ; import { map } from 'rxjs/operators' ; import { Event } from '@medium-stories/entities' ; import { AbstractEffects } from '@medium-stories/store' ; import { EventApollo } from '../interfaces/event-apollo.interface' ; import { EventActionTypes , EventLoadCanceled , EventLoaded , EventLoadError , EventsLoadCanceled , EventsLoaded , EventsLoadError , LastEventLoadCanceled , LastEventLoaded , LastEventLoadError , LoadEvent , LoadEvents , LoadingEvent , LoadingEvents , LoadingLastEvent , LoadLastEvent } from './event.actions' ; import { EVENT_FEATURE_KEY , EventPartialState , EventState } from './event.reducer' ; @ Injectable ( ) export class EventEffects extends AbstractEffects < EventState > { @ Effect ( ) loadLastEvent$ = this . dataPersistence . fetch ( EventActionTypes . LoadLastEvent , { run : ( action , store ) => { return isPlatformBrowser ( this . platformId ) && ( ! this . getState ( store ) . eventLastLoading || action . payload ) ? new LoadingLastEvent ( ) : new LastEventLoadCanceled ( ) ; } , onError : ( action , error ) => console . error ( error . toString ( ) ) } ) ; @ Effect ( ) loadingLastEvent$ = this . dataPersistence . fetch ( EventActionTypes . LoadingLastEvent , { id : ( ) => 'loadEventLast' , run : ( action , store ) => { return this . eventApollo . loadLastEvent ( ) . pipe ( map < Event , LastEventLoaded > ( event => new LastEventLoaded ( event ) ) ) ; } , onError : ( action , error ) => new LastEventLoadError ( error ) } ) ; @ Effect ( ) loadEvent$ = this . dataPersistence . fetch ( EventActionTypes . LoadEvent , { run : ( action , store ) => { return isPlatformBrowser ( this . platformId ) && ( ! this . getState ( store ) . eventLoading || action . payload . force ) ? new LoadingEvent ( action . payload ) : new EventLoadCanceled ( ) ; } , onError : ( action , error ) => console . error ( error . toString ( ) ) } ) ; @ Effect ( ) loadingEvent$ = this . dataPersistence . fetch ( EventActionTypes . LoadingEvent , { id : ( action ) => this . getIdByPayload ( action . payload ) , run : ( action , store ) => { return this . eventApollo . loadEvent ( action . payload . id ) . pipe ( map < Event , EventLoaded > ( event => new EventLoaded ( event ) ) ) ; } , onError : ( action , error ) => new EventLoadError ( error ) } ) ; @ Effect ( ) loadEvents$ = this . dataPersistence . fetch ( EventActionTypes . LoadEvents , { run : ( action , store ) => { return isPlatformBrowser ( this . platformId ) && ( ! this . getState ( store ) . eventsLoading || action . payload . force ) ? new LoadingEvents ( action . payload ) : new EventsLoadCanceled ( ) ; } , onError : ( action , error ) => console . error ( error . toString ( ) ) } ) ; @ Effect ( ) loadingEvents$ = this . dataPersistence . fetch ( EventActionTypes . LoadingEvents , { id : ( action ) => this . getIdByPayload ( action . payload ) , run : ( action , store ) => { return this . eventApollo . loadEvents ( action . payload ) . pipe ( map < Event [ ] , EventsLoaded > ( event => new EventsLoaded ( event ) ) ) ; } , onError : ( action , error ) => new EventsLoadError ( error ) } ) ; constructor ( private dataPersistence , private eventApollo , @ Inject ( PLATFORM_ID ) private platformId ) { super ( EVENT_FEATURE_KEY ) ; } }