Skip to content
Snippets Groups Projects
app.component.ts 2.28 KiB
Newer Older
Anurag Vats's avatar
Anurag Vats committed
import { Component } from '@angular/core';
import {Router} from "@angular/router";
import { NavigationEnd } from '@angular/router';
import {AuthService} from "./shared/auth/auth.service";
import {EventEmitterService} from "./shared/event.emitter.service";
Anurag Vats's avatar
Anurag Vats committed

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'frontend';
  isAdmin: boolean = false;
  isLoggedIn: boolean = false;
  url: string = '';

  constructor(private router: Router, private authService: AuthService,
              private eventEmitterService: EventEmitterService) {
    // @ts-ignore
    router.events.subscribe((event:Event) => {
      if(event instanceof NavigationEnd ){
        // @ts-ignore
        this.url = event.url;
        if(this.url === '/station'){
          this.title = 'Stations';
        } else if (this.url === '/station/client'){
          this.title = 'Clients';
        } else if (this.url === '/station/host'){
          this.title = 'Hosts';
        } else if (this.url === '/station/sensor'){
          this.title = 'Sensors';
        } else if (this.url === '/admin'){
          this.title = 'Admin Page';
        }
      }
    });
    this.isAdmin = this.authService.isAdmin();
    this.isLoggedIn = this.authService.isLoggedIn();

    if (this.eventEmitterService.subVar == undefined) {
      this.eventEmitterService.subVar = this.eventEmitterService.
      invokeAppComponentFunction.subscribe((name:string) => {
        this.checkLoggedInUser();
      });
    }
  }

  logout() {
    localStorage.removeItem('token');
    localStorage.removeItem('role');
    this.isAdmin = false;
    this.isLoggedIn = false;
    this.router.navigate(['/login']);
  }

  private checkLoggedInUser() {
    this.isAdmin = this.authService.isAdmin();
    this.isLoggedIn = this.authService.isLoggedIn();
  }
  navigate(i: number) {
    if(i === 0){
      this.router.navigate(['/station']);
    } else if (i === 1){
      this.router.navigate(['/station/host']);
    } else if (i === 2){
      this.router.navigate(['/station/client']);
    } else if (i === 3){
      this.router.navigate(['/station/sensor']);
    } else if (i === 4){
      this.router.navigate(['/admin']);
    } else if (i === 5){
      this.router.navigate(['/']);