| /******************************************************************************** |
| * Copyright (c) 2020 Contributors to the Eclipse Foundation |
| * |
| * See the NOTICE file(s) distributed with this work for additional |
| * information regarding copyright ownership. |
| * |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0 |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| ********************************************************************************/ |
| |
| import {Injectable} from "@angular/core"; |
| import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree} from "@angular/router"; |
| import {select, Store} from "@ngrx/store"; |
| import {Observable} from "rxjs"; |
| import {filter, map, switchMap} from "rxjs/operators"; |
| import {isLoadingSelector, isOfficialInChargeSelector} from "../../../store"; |
| |
| @Injectable({providedIn: "root"}) |
| export class NewStatementRouteGuardService implements CanActivate { |
| |
| public readonly isInitialized$ = this.store.pipe( |
| select(isLoadingSelector), |
| filter((isLoading) => !isLoading) |
| ); |
| |
| public readonly isAllowed$ = this.store.pipe( |
| select(isOfficialInChargeSelector) |
| ); |
| |
| private readonly redirectUrlTree = this.router.createUrlTree(["/"]); |
| |
| public constructor(private readonly store: Store, private readonly router: Router) { |
| } |
| |
| public canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> { |
| return this.isInitialized$.pipe( |
| switchMap(() => this.isAllowed$), |
| map((isAllowed) => isAllowed ? isAllowed : this.redirectUrlTree) |
| ); |
| } |
| |
| } |