import { Injectable } from '@angular/core'; import { Actions, ofType, createEffect } from '@ngrx/effects'; import { of } from 'rxjs'; import { catchError, exhaustMap, map, withLatestFrom } from 'rxjs/operators'; import { Store, select } from '@ngrx/store'; import { NGXLogger } from 'ngx-logger'; import { buddy2, buddy2Success, buddy2Failure, buddy2Data, group2, group2Success, group2Failure, group2Data } from './actions'; import { SessionStorageService } from '@ucap-webmessenger/web-storage'; import { SyncProtocolService, SSVC_TYPE_SYNC_BUDDY2_DATA, BuddyResponse, BuddyDetailData, SSVC_TYPE_SYNC_GROUP_DATA2, GroupDetailData, GroupResponse } from '@ucap-webmessenger/protocol-sync'; import { regViewSuccess } from '@app/store/setting/option'; @Injectable() export class Effects { regViewSuccess$ = createEffect(() => this.actions$.pipe( ofType(regViewSuccess), withLatestFrom( this.store.pipe( select(state => { this.logger.debug('state', state); return state.messenger.sync.buddy2SyncDate as string; }) ) ), map(([action, buddy2SyncDate]) => buddy2({ syncDate: buddy2SyncDate })) ) ); buddy2$ = createEffect(() => this.actions$.pipe( ofType(buddy2), exhaustMap(req => this.syncProtocolService.buddy2(req).pipe( map(res => { switch (res.Type) { case SSVC_TYPE_SYNC_BUDDY2_DATA: return buddy2Data({ data: res as BuddyDetailData }); } return buddy2Success({ res: res as BuddyResponse }); }), catchError(error => of(buddy2Failure({ error }))) ) ) ) ); buddy2SuccessToGroup2$ = createEffect(() => this.actions$.pipe( ofType(buddy2Success), withLatestFrom( this.store.pipe( select(state => { this.logger.debug('state', state); return state.messenger.sync.group2SyncDate as string; }) ) ), map(([action, group2SyncDate]) => group2({ syncDate: group2SyncDate })) ) ); group2$ = createEffect(() => this.actions$.pipe( ofType(group2), exhaustMap(req => this.syncProtocolService.group2(req).pipe( map(res => { switch (res.Type) { case SSVC_TYPE_SYNC_GROUP_DATA2: return group2Data({ data: res as GroupDetailData }); } return group2Success({ res: res as GroupResponse }); }), catchError(error => of(group2Failure({ error }))) ) ) ) ); constructor( private actions$: Actions, private store: Store, private syncProtocolService: SyncProtocolService, private sessionStorageService: SessionStorageService, private logger: NGXLogger ) {} }