import { Injectable } from '@angular/core'; import { Actions, ofType, createEffect } from '@ngrx/effects'; import { of } from 'rxjs'; import { catchError, map, tap, switchMap } from 'rxjs/operators'; import { dept, deptSuccess, deptFailure } from './actions'; import { QueryProtocolService, DeptInfo, SSVC_TYPE_QUERY_DEPT_DATA, SSVC_TYPE_QUERY_DEPT_RES, DeptData } from '@ucap-webmessenger/protocol-query'; import { SessionStorageService } from '@ucap-webmessenger/web-storage'; import { Store } from '@ngrx/store'; @Injectable() export class Effects { dept$ = createEffect( () => { let departmentInfoList: DeptInfo[]; return this.actions$.pipe( ofType(dept), tap(() => { departmentInfoList = []; }), switchMap(req => { return this.queryProtocolService.dept(req).pipe( map(res => { switch (res.Type) { case SSVC_TYPE_QUERY_DEPT_DATA: departmentInfoList.push(...(res as DeptData).departmentInfos); break; case SSVC_TYPE_QUERY_DEPT_RES: this.store.dispatch( deptSuccess({ departmentInfoList }) ); break; } }), catchError(error => of(deptFailure({ error }))) ); }) ); }, { dispatch: false } ); constructor( private actions$: Actions, private store: Store, private sessionStorageService: SessionStorageService, private queryProtocolService: QueryProtocolService ) {} }