openaire-library/connect/communityGuard/connectCommunityGuard.guard.ts

46 lines
1.4 KiB
TypeScript

import {filter, mergeMap} from 'rxjs/operators';
import { Injectable } from '@angular/core';
import {
Router,
CanActivate,
ActivatedRouteSnapshot,
RouterStateSnapshot,
CanLoad,
Route
} from '@angular/router';
import {Observable, Subscription} from 'rxjs';
import {CommunityService} from '../community/community.service';
import { EnvironmentSpecificService} from '../../utils/properties/environment-specific.service';
import {ConnectHelper} from '../connectHelper';
import {properties} from "../../../../environments/environment";
@Injectable()
export class ConnectCommunityGuard implements CanActivate {
sub: Subscription = null;
constructor(private router: Router,
private communityService: CommunityService,
private propertiesService: EnvironmentSpecificService) {
}
check(community: string): Observable<boolean> | boolean {
const obs = this.communityService.isCommunityTypeByState(properties, properties['communityAPI'] + community);
this.sub = obs.pipe(filter(enabled => !enabled))
.subscribe(() => this.router.navigate(['errorcommunity']));
return obs;
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.check(route.queryParams['communityId']);
}
canDeactivate() {
if(this.sub) {
this.sub.unsubscribe();
}
return true;
}
}