Close menu on navigation

This commit is contained in:
Willem Dantuma 2020-01-27 17:01:35 +01:00
parent af1a54ae07
commit 77e4093d14

View File

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