import { Injectable } from '@angular/core' ; import { Effect , Actions , ofType } from '@ngrx/effects' ; import { switchMap , map , filter } from 'rxjs/operators' ; import { empty } from 'rxjs' ; import { SearchResults , Question , RouterStateUrl , SearchCriteria , QuestionStatus } from 'shared-library/shared/model' ; import { AdminActionTypes } from '../actions' ; import * as adminActions from '../actions/admin.actions' ; import { QuestionService } from 'shared-library/core/services' ; import { ROUTER_NAVIGATION } from '@ngrx/router-store' ; @ Injectable ( ) export class AdminEffects { @ Effect ( ) loadRouteQuestions$ = this . actions$ . pipe ( ofType ( ROUTER_NAVIGATION ) ) . pipe ( map ( ( action ) : RouterStateUrl => action . payload . routerState ) , filter ( ( routerState ) => routerState . url . toLowerCase ( ) . startsWith ( '/admin' ) ) ) . pipe ( switchMap ( ( routerState ) => this . svc . getQuestions ( 0 , 25 , new SearchCriteria ( ) ) . pipe ( map ( ( results ) => new adminActions . LoadQuestionsSuccess ( results ) ) ) ) ) ; @ Effect ( ) loadUnpublishedRouteQuestions$ = this . actions$ . pipe ( ofType ( ROUTER_NAVIGATION ) ) . pipe ( map ( ( action ) : RouterStateUrl => action . payload . routerState ) , filter ( ( routerState ) => routerState . url . toLowerCase ( ) . startsWith ( '/admin' ) ) ) . pipe ( switchMap ( ( routerState ) => { const filteredStatus = [ ] ; filteredStatus . push ( QuestionStatus . PENDING ) ; return this . svc . getUnpublishedQuestions ( routerState . url . toLowerCase ( ) . includes ( 'bulk' ) , filteredStatus ) . pipe ( map ( ( questions ) => new adminActions . LoadUnpublishedQuestionsSuccess ( questions ) ) ) ; } ) ) ; @ Effect ( ) loadQuestions$ = this . actions$ . pipe ( ofType ( AdminActionTypes . LOAD_QUESTIONS ) ) . pipe ( switchMap ( ( action ) => this . svc . getQuestions ( action . payload . startRow , action . payload . pageSize , action . payload . criteria ) . pipe ( map ( ( results ) => new adminActions . LoadQuestionsSuccess ( results ) ) ) ) ) ; @ Effect ( ) loadUnpublishedQuestions$ = this . actions$ . pipe ( ofType ( AdminActionTypes . LOAD_UNPUBLISHED_QUESTIONS ) ) . pipe ( switchMap ( ( action ) => this . svc . getUnpublishedQuestions ( action . payload . question_flag , action . payload . filteredStatus ) . pipe ( map ( ( questions ) => new adminActions . LoadUnpublishedQuestionsSuccess ( questions ) ) ) ) ) ; @ Effect ( ) approveQuestion$ = this . actions$ . pipe ( ofType ( AdminActionTypes . APPROVE_QUESTION ) ) . pipe ( switchMap ( ( action ) => { this . svc . approveQuestion ( action . payload . question ) ; return empty ( ) ; } ) ) ; constructor ( private actions$ , private svc ) { } }