import { Injectable } from '@angular/core' ; import { Effect , Actions , ofType } from '@ngrx/effects' ; import { switchMap , map , filter , catchError , mergeMap , take } from 'rxjs/operators' ; import { SocialService , StatsService , AchievementService , QuestionService } from 'shared-library/core/services' ; import { Subscribers , Blog , RouterStateUrl , SystemStats , AchievementRule , Question } from 'shared-library/shared/model' ; import { DashboardActionTypes } from '../actions' ; import * as dashboardActions from '../actions/dashboard.actions' ; import { UploadTaskSnapshot } from '@angular/fire/storage/interfaces' ; import { of } from 'rxjs' ; import { ROUTER_NAVIGATION } from '@ngrx/router-store' ; import { Store } from '@ngrx/store' ; import { AppState } from '../../../store' ; import { coreState } from 'shared-library/core/store' ; @ Injectable ( ) export class DashboardEffects { @ Effect ( ) addSubscription$ = this . actions$ . pipe ( ofType ( DashboardActionTypes . ADD_SUBSCRIBER ) ) . pipe ( switchMap ( ( action ) => this . socialService . checkSubscription ( action . payload . subscription ) . pipe ( map ( isSubscribed => { if ( isSubscribed ) { return new dashboardActions . CheckSubscriptionStatus ( true ) ; } else { this . socialService . saveSubscription ( action . payload . subscription ) ; return new dashboardActions . CheckSubscriptionStatus ( false ) ; } } ) , catchError ( err => of ( new dashboardActions . AddSubscriberError ( err ) ) ) ) ) ) ; @ Effect ( ) getTotalSubscription$ = this . actions$ . pipe ( ofType ( DashboardActionTypes . TOTAL_SUBSCRIBER ) ) . pipe ( switchMap ( ( action ) => this . socialService . getTotalSubscription ( ) . pipe ( map ( ( totalCount ) => new dashboardActions . GetTotalSubscriberSuccess ( totalCount ) ) ) ) ) ; @ Effect ( ) loadSocialScoreShareUrl$ = this . actions$ . pipe ( ofType ( DashboardActionTypes . LOAD_SOCIAL_SCORE_SHARE_URL ) ) . pipe ( switchMap ( ( action ) => this . socialService . generateScoreShareImage ( action . payload . imageBlob , action . payload . userId ) . pipe ( map ( ( imageUrl ) => new dashboardActions . LoadSocialScoreShareUrlSuccess ( imageUrl ) ) ) ) ) ; @ Effect ( ) getBlogs$ = this . actions$ . pipe ( ofType ( ROUTER_NAVIGATION ) ) . pipe ( map ( ( action ) : RouterStateUrl => action . payload . routerState ) , filter ( ( routerState ) => routerState . url . toLowerCase ( ) . startsWith ( '/' ) ) ) . pipe ( switchMap ( ( ) => this . socialService . loadBlogs ( ) . pipe ( map ( ( blogs ) => new dashboardActions . LoadBlogsSuccess ( blogs ) ) , catchError ( err => of ( new dashboardActions . LoadBlogsError ( err ) ) ) ) ) ) ; @ Effect ( ) LoadLeaderBoardInfo$ = this . actions$ . pipe ( ofType ( DashboardActionTypes . LOAD_LEADERBOARD ) ) . pipe ( switchMap ( ( action ) => this . statsService . loadLeaderBoardStat ( action . payload . data ) . pipe ( map ( ( score ) => { return new dashboardActions . LoadLeaderBoardSuccess ( score ) ; } ) ) ) ) ; @ Effect ( ) LoadSystemStat$ = this . actions$ . pipe ( ofType ( DashboardActionTypes . LOAD_SYSTEM_STAT ) ) . pipe ( switchMap ( ( action ) => this . statsService . loadSystemStat ( ) . pipe ( map ( ( stat ) => new dashboardActions . LoadSystemStatSuccess ( stat ) ) , catchError ( err => of ( new dashboardActions . LoadSystemStatError ( err ) ) ) ) ) ) ; @ Effect ( ) getAchievements$ = this . actions$ . pipe ( ofType ( DashboardActionTypes . LOAD_ACHIEVEMENTS ) ) . pipe ( switchMap ( ( action ) => this . achievementService . getAchievements ( ) . pipe ( map ( ( achievements ) => { return new dashboardActions . LoadAchievementsSuccess ( achievements ) ; } ) ) ) ) ; @ Effect ( ) loadUserLatestPublishedRouteQuestions$ = this . actions$ . pipe ( ofType ( ROUTER_NAVIGATION ) ) . pipe ( map ( ( action ) : RouterStateUrl => action . payload . routerState ) , filter ( ( routerState ) => routerState . url . toLowerCase ( ) . startsWith ( '/' ) ) , mergeMap ( ( routerState ) => this . store . select ( coreState ) . pipe ( map ( s => s . user ) , filter ( u => ! ! u ) , take ( 1 ) , map ( user => user . userId ) ) ) ) . pipe ( switchMap ( ( id ) => { return this . questionService . getUserLatestQuestion ( id ) . pipe ( map ( ( question ) => new dashboardActions . LoadUserLatestPublishedQuestionSuccess ( question ) ) ) ; } ) ) ; constructor ( private actions$ , private socialService , private statsService , private achievementService , private questionService , private store ) { } }