Project

General

Profile

1 61381 k.triantaf
import { Injectable } from '@angular/core';
2
import {
3
  Router,
4
  CanActivate,
5
  ActivatedRouteSnapshot,
6
  RouterStateSnapshot,
7
  CanLoad, Route, UrlSegment, CanActivateChild, UrlTree
8
} from '@angular/router';
9
import {Observable} from 'rxjs';
10
11
import {ConnectHelper} from '../connectHelper';
12
import {properties} from "../../../../environments/environment";
13
import {CommunityService} from "../community/community.service";
14
import {map} from "rxjs/operators";
15
16
@Injectable()
17
export class IsCommunity implements CanActivate, CanActivateChild {
18
19
  constructor(private router: Router,
20
              private communityService: CommunityService) {
21
  }
22
23
  check(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
24
    let community;
25
    if(properties.isDashboard) {
26
      community = route.params['community'];
27
    } else {
28
      community = ConnectHelper.getCommunityFromDomain(properties.domain);
29
    }
30
    if (community) {
31
      return this.communityService.getCommunity(community).pipe(map(community => {
32
        if(community) {
33
          return true;
34
        } else {
35
          this.router.navigate(['error'], {queryParams: {page: state.url}});
36
          return false;
37
        }
38
      }));
39
    } else {
40
      this.router.navigate(['error'], {queryParams: {page: state.url}});
41
      return false;
42
    }
43
  }
44
45
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
46
    return this.check(route, state);
47
  }
48
49
  canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
50
    return this.check(childRoute, state);
51
  }
52
}