import { Injectable } from '@angular/core' ; import { Effect , Actions , ofType } from '@ngrx/effects' ; import { Question , RouterStateUrl } from '../../../shared/model' ; import { QuestionActions , UserActions , ActionWithPayload } from '../actions' ; import { QuestionService } from '../../services' ; import { switchMap , map , filter , catchError } from 'rxjs/operators' ; import { of , empty } from 'rxjs' ; import { ROUTER_NAVIGATION } from '@ngrx/router-store' ; @ Injectable ( ) export class QuestionEffects { @ Effect ( ) loadRouteQuestionOfDay$ = this . actions$ . pipe ( ofType ( ROUTER_NAVIGATION ) ) . pipe ( map ( ( action ) : RouterStateUrl => action . payload . routerState ) , filter ( ( routerState ) => { return routerState . url === '/dashboard' ; } ) ) . pipe ( switchMap ( ( ) => this . svc . getQuestionOfTheDay ( false ) . pipe ( map ( ( question ) => this . questionActions . getQuestionOfTheDaySuccess ( question ) ) , catchError ( err => of ( this . questionActions . getQuestionOfTheDayError ( err ) ) ) ) ) ) ; @ Effect ( ) loadNextQuestionOfDay$ = this . actions$ . pipe ( ofType ( QuestionActions . GET_QUESTION_OF_THE_DAY ) ) . pipe ( switchMap ( action => this . svc . getQuestionOfTheDay ( true ) . pipe ( map ( ( question ) => this . questionActions . getQuestionOfTheDaySuccess ( question ) ) , catchError ( err => of ( this . questionActions . getQuestionOfTheDayError ( err ) ) ) ) ) ) ; @ Effect ( ) updateQuestion$ = this . actions$ . pipe ( ofType ( QuestionActions . UPDATE_QUESTION ) ) . pipe ( switchMap ( ( action ) : any => { return this . svc . saveQuestion ( action . payload ) ; } ) ) ; @ Effect ( ) loadFirstQuestion$ = this . actions$ . pipe ( ofType ( ROUTER_NAVIGATION ) ) . pipe ( map ( ( action ) : RouterStateUrl => action . payload . routerState ) , filter ( ( routerState ) => routerState . url . toLowerCase ( ) . startsWith ( '/first-question' ) ) ) . pipe ( switchMap ( ( ) => this . svc . getQuestionOfTheDay ( true ) . pipe ( map ( ( question ) => this . questionActions . getFirstQuestionSuccess ( question ) ) , catchError ( err => of ( this . questionActions . getFirstQuestionError ( err ) ) ) ) ) ) ; @ Effect ( ) deleteQuestionImage$ = this . actions$ . pipe ( ofType ( QuestionActions . DELETE_QUESTION_IMAGE ) ) . pipe ( switchMap ( ( action ) => this . svc . deleteQuestionImage ( action . payload ) . pipe ( map ( ( res ) => this . questionActions . deleteQuestionImageSuccess ( res . message ) ) ) ) ) ; constructor ( private actions$ , private questionActions , private svc ) { } }