import { Injectable } from '@angular/core'; import { Router } from '@angular/router'; import { OAuthService } from 'angular-oauth2-oidc'; import { Store, Action } from '@ngrx/store'; import { Effect, Actions,ofType } from '@ngrx/effects'; import { Observable , defer , of } from 'rxjs'; import { withLatestFrom,mergeMap,switchMap,map,catchError} from 'rxjs/operators'; import * as appCommonActions from '../actions/app-common.actions'; import * as appCommonReducers from '../reducers/app-common.reducer'; import { ItemService } from '../services/item.service'; import { FolderService } from '../services/folder.service'; import { UserService } from '../services/user.service'; import { IItemTypes } from '../models/item.types'; import { IListItem } from '../models/list.item'; import { IUser } from '../models/user'; @Injectable() export class AppCommonEffects { @Effect({ dispatch: false }) login$: Observable = this.actions$.pipe( ofType(appCommonActions.LOGIN), withLatestFrom(this.store$.select(appCommonReducers.selectGetInitialized)), mergeMap(([action, initialized]) => { var a = (action as appCommonActions.Login); this.oauthService$.initImplicitFlow(a.url); return []; })); @Effect() loadItemTypes$: Observable = this.actions$.pipe( ofType(appCommonActions.LOADITEMTYPES), switchMap((action) => { return this.itemService$.getItemTypes().pipe( map((itemTypes: IItemTypes) => new appCommonActions.LoadItemTypesSuccess(itemTypes)), catchError(error => of(new appCommonActions.Fail(error)))) } )); @Effect() initUser$: Observable = this.actions$.pipe( ofType(appCommonActions.INITUSER), switchMap(() => { return this.userService$.getCurrentUser().pipe( map((user: IUser) => new appCommonActions.InitUserSuccess(user)), catchError(error => of(new appCommonActions.Fail(error)))) } )); @Effect() initUserSuccess$: Observable = this.actions$.pipe( ofType(appCommonActions.INITUSERSUCCESS), switchMap(() => { return of(new appCommonActions.InitRoot()); } )); @Effect() initRoot$: Observable = this.actions$.pipe( ofType(appCommonActions.INITROOT), switchMap(() => { return this.folderService$.getMyRoots().pipe( map((folders: IListItem[]) => new appCommonActions.InitRootSuccess(folders)), catchError(error => of(new appCommonActions.Fail(error)))) } )); @Effect() deleteItems$: Observable = this.actions$.pipe( ofType(appCommonActions.DELETEITEMS), switchMap((action:appCommonActions.DeleteItems) => { return this.itemService$.deleteItems(action.itemCodes).pipe( map((deletedItemCodes: string[]) => new appCommonActions.DeleteItemsSuccess(deletedItemCodes)), catchError(error => of(new appCommonActions.Fail(error)))) } )); @Effect() editItem$: Observable = this.actions$.pipe( ofType(appCommonActions.EDITITEM), withLatestFrom(this.store$.select(appCommonReducers.selectGetItemTypes)), switchMap(([action, itemtypes]) => { var a = action as appCommonActions.EditItem; var itemType = itemtypes[a.item.itemType]; var editor = itemType.editor ? itemType.editor : "property"; this.router$.navigate(['/editor',editor,'item', a.item.code]) return []; } )); @Effect() viewItem$: Observable = this.actions$.pipe( ofType(appCommonActions.VIEWITEM), withLatestFrom(this.store$.select(appCommonReducers.selectGetItemTypes)), switchMap(([action, itemtypes]) => { var a = action as appCommonActions.EditItem; var itemType = itemtypes[a.item.itemType]; var viewer = itemType.viewer; this.router$.navigate(['/viewer', viewer, 'item', a.item.code]) return []; } )); @Effect() closeMenuOnRouting$:Observable = this.actions$.pipe( ofType(appCommonActions.STARTROUTELOADING), switchMap((action) => { return of(new appCommonActions.SetMenuVisible(false)); } )); @Effect({ dispatch: false }) fail$: Observable = this.actions$.pipe( ofType(appCommonActions.FAIL), map((action) => { let failAction = action as appCommonActions.Fail; console.debug(failAction.payload) return null; })); constructor(private actions$: Actions, private store$: Store, private oauthService$: OAuthService, private itemService$: ItemService, private folderService$:FolderService, private userService$: UserService, private router$: Router) { store$.dispatch(new appCommonActions.LoadItemTypes()); } }