Project

General

Profile

1
import {take, tap} from 'rxjs/operators';
2
import {Injectable} from '@angular/core';
3
import {
4
  Router,
5
  CanActivate,
6
  ActivatedRouteSnapshot,
7
  RouterStateSnapshot,
8

    
9
} from '@angular/router';
10
import {Observable, Subscription} from 'rxjs';
11
import {CommunityService} from '../community/community.service';
12
import {properties} from "../../../../environments/environment";
13

    
14
@Injectable()
15
export class ConnectCommunityGuard implements CanActivate {
16
  sub: Subscription = null;
17

    
18
  constructor(private router: Router,
19
              private communityService: CommunityService) {
20
  }
21

    
22
  check(community: string): Observable<boolean> | boolean {
23
    return this.communityService.isCommunityTypeByState(properties, properties['communityAPI'] + community).pipe(take(1), tap(isCommunity => {
24
      if (!isCommunity) {
25
        this.router.navigate(['errorcommunity']);
26
      }
27
    }));
28
  }
29

    
30
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
31
    return this.check(route.queryParams['communityId']);
32
  }
33
}
(4-4/8)