openaire-library/error/isRouteEnabled.guard.ts

24 lines
1.0 KiB
TypeScript

import { Injectable } from '@angular/core';
import { Router,CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/filter';
import { ConfigurationService } from '../utils/configuration/configuration.service';
@Injectable()
export class IsRouteEnabled implements CanActivate {
constructor(private router: Router, private config: ConfigurationService) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
let customRedirect = route.data['redirect'];
console.log("In IsRouteEnabled");
console.log(state.url.split("?")[0].substring(1));
let isEnabled = this.config.isPageEnabled(state.url.split("?")[0].substring(1));
let redirect = !!customRedirect ? customRedirect : '/error';
isEnabled.filter(enabled => !enabled)
.subscribe(() => this.router.navigate([redirect], { queryParams: { "page": state.url } }));
return isEnabled;
}
}