Project

General

Profile

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