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

34 lines
1000 B
TypeScript

import {take, tap} from 'rxjs/operators';
import {Injectable} from '@angular/core';
import {
Router,
CanActivate,
ActivatedRouteSnapshot,
RouterStateSnapshot,
} from '@angular/router';
import {Observable, Subscription} from 'rxjs';
import {CommunityService} from '../community/community.service';
import {properties} from "../../../../environments/environment";
@Injectable()
export class ConnectCommunityGuard implements CanActivate {
sub: Subscription = null;
constructor(private router: Router,
private communityService: CommunityService) {
}
check(community: string): Observable<boolean> | boolean {
return this.communityService.isCommunityType(community).pipe(take(1), tap(isCommunity => {
if (!isCommunity) {
this.router.navigate(['errorcommunity']);
}
}));
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.check(route.queryParams['communityId']);
}
}