All files / src/app app.component.ts

55% Statements 22/40
37.5% Branches 6/16
69.23% Functions 9/13
52.63% Lines 20/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118                                                  219x 109x     109x   109x   109x 109x 109x     109x 3548x   258x 258x 212x     46x           3x       109x 109x   109x                       109x                             109x   200x                                                          
import { Component, OnDestroy, OnInit } from '@angular/core';
import { RouterOutlet, ActivatedRoute, Router, NavigationEnd } from '@angular/router';
import { filter } from 'rxjs';
import { NgClass, NgIf } from '@angular/common';
 
 
import { HeaderComponent } from './components/header/header.component';
import { LeftbarComponent } from './components/leftbar/leftbar.component';
import { FooterComponent } from './components/footer/footer.component';
import { ToastModule } from 'primeng/toast';
import { TwinPadStatus } from './models/status';
import { HttpErrorResponse } from '@angular/common/http';
import { GlobalErrorComponent } from './components/global-error/global-error.component';
import { TwinpadApiService } from './services/twinpad-api.service';
import { Button } from 'primeng/button';
import { ImagePreloaderService } from './services/resolvers/images.service';
 
 
@Component({
  selector: 'app-root',
  imports: [RouterOutlet, NgIf, HeaderComponent, LeftbarComponent, FooterComponent, ToastModule, GlobalErrorComponent, NgClass, Button],
  providers: [],
  templateUrl: './app.component.html',
  styleUrl: './app.component.scss'
})
export class AppComponent implements OnInit, OnDestroy{
  title = 'Twinpad';
  display_bars: boolean;
 
  isCloudReachable: boolean = true;
  error: HttpErrorResponse;
  isCollapsed: boolean = false;
 
  constructor(private route: ActivatedRoute, private router: Router,
     private twinpadApiService: TwinpadApiService,
    private imagePreloaderService: ImagePreloaderService
  ) {
 
    router.events
      .pipe(filter(event => event instanceof NavigationEnd))
      .subscribe((_) => {
        const data = route.snapshot.firstChild!.data;
        if ('bars' in data && data['bars']) {
          this.display_bars = true;
        }
        else {
          this.display_bars = false;
        }
      });
  }
 
  toggleSidebar() {
    this.isCollapsed = !this.isCollapsed;
  }
 
  ngOnInit(){
    this.deviceSubscribe();
    this.statusSubscribe();
 
    this.imagePreloaderService.preloadImages([
      '/assets/logo.png',
      '/assets/backgrounds/background4.jpg',
      '/assets/svg/alien-invasion.svg',
      '/assets/svg/towing.svg',
      '/assets/svg/server-down.svg',
      '/assets/svg/bug.svg'
    ]);
 
  }
 
  deviceSubscribe(){
    this.twinpadApiService.devicesBehaviorSubject$?.subscribe({
      error: error => {
        this.error = error;
        if (this.error.status === 401){
          this.twinpadApiService.logout();
          this.twinpadApiService.redirectToLogin(this.router);
        }
        else if(this.isTokenAvailable()) {
            this.deviceSubscribe();
        }
      }
    });
  }
 
  statusSubscribe(){
    this.twinpadApiService.cloudStatusBehaviorSubject$?.subscribe({
      next: (status: TwinPadStatus) => {
        this.isCloudReachable = status.services.backend === "up";
      },
      error: error => {
        this.error = error;
        if (this.error.status == 401){
          this.twinpadApiService.logout();
          this.twinpadApiService.redirectToLogin(this.router);
        }
        else if(this.isTokenAvailable()) {
            this.deviceSubscribe();
        }
      }
    });
  }
 
  isTokenAvailable(){
    const tokenExpired = this.twinpadApiService.isTokenValid();
    if(!tokenExpired) {
      window.location.reload();
    }
    return true;
  }
 
  ngOnDestroy(): void {
    this.twinpadApiService.cloudStatusSubscription?.unsubscribe();
    this.twinpadApiService.devicesSubscription?.unsubscribe();
  }
 
}