import { Injectable } from '@angular/core'; import { Actions, ofType, createEffect } from '@ngrx/effects'; import { of } from 'rxjs'; import { catchError, exhaustMap, map, withLatestFrom, switchMap, tap } from 'rxjs/operators'; import { Store, select } from '@ngrx/store'; import { NGXLogger } from 'ngx-logger'; import { buddy2, buddy2Success, buddy2Failure, group2, group2Success, group2Failure } 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, UserInfo, SSVC_TYPE_SYNC_BUDDY2_RES, SSVC_TYPE_SYNC_GROUP_RES2 } from '@ucap-webmessenger/protocol-sync'; import { regViewSuccess } from '@app/store/setting/option'; @Injectable() export class Effects { buddy2$ = createEffect( () => { let buddyList: UserInfo[]; return this.actions$.pipe( ofType(buddy2), tap(() => { buddyList = []; }), switchMap(req => { return this.syncProtocolService.buddy2(req).pipe( map(res => { switch (res.Type) { case SSVC_TYPE_SYNC_BUDDY2_DATA: buddyList.push(...(res as BuddyDetailData).buddyInfos); break; case SSVC_TYPE_SYNC_BUDDY2_RES: this.store.dispatch( buddy2Success({ buddyList, syncDate: (res as BuddyResponse).syncDate }) ); break; } }), catchError(error => of(buddy2Failure({ error }))) ); }) ); }, { dispatch: false } ); group2$ = createEffect( () => { let groupList: GroupDetailData[]; return this.actions$.pipe( ofType(group2), tap(() => { groupList = []; }), switchMap(req => { return this.syncProtocolService.group2(req).pipe( map(res => { switch (res.Type) { case SSVC_TYPE_SYNC_GROUP_DATA2: groupList.push(res as GroupDetailData); break; case SSVC_TYPE_SYNC_GROUP_RES2: this.store.dispatch( group2Success({ groupList, syncDate: (res as GroupResponse).syncDate }) ); break; } }), catchError(error => of(group2Failure({ error }))) ); }) ); }, { dispatch: false } ); constructor( private actions$: Actions, private store: Store, private syncProtocolService: SyncProtocolService, private sessionStorageService: SessionStorageService, private logger: NGXLogger ) {} }