All files / src/app app.component.ts

54.54% Statements 24/44
38.88% Branches 7/18
64.28% Functions 9/14
52.38% Lines 22/42

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 119 120 121 122 123 124 125 126 127                                                    118x 59x       59x       59x   59x 59x 59x     59x 1928x   144x 144x 119x     25x                   59x 59x   59x                       59x 117x                             59x   108x 108x 108x                                                              
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 { Device } from './models/devices';
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;
 
  status: TwinPadStatus;
  cloudStatus: boolean = true;
  devices: Device[];
  error: HttpErrorResponse;
  nextCloudRequest: number;
  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({
      next: (devices: Device[]) => {this.devices = devices;},
      error: error => {
        this.error = error;
        if (this.error.status === 401){
          this.twinpadApiService.logout();
          this.router.navigate(["login"]);
        }
        else if(this.isTokenAvailable()) {
            this.deviceSubscribe();
        }
      }
    });
  }
 
  statusSubscribe(){
    this.twinpadApiService.cloudStatusBehaviorSubject$?.subscribe({
      next: (value: TwinPadStatus) => {
        this.status = value;
        this.cloudStatus = this.status.services.backend === "up";
        Iif(!this.cloudStatus){
          this.nextCloudRequest =  Date.now() + 1000*this.twinpadApiService.statusRefreshTime;
        }
      },
      error: error => {
        this.error = error;
        if (this.error.status == 401){
          this.twinpadApiService.logout();
          this.router.navigate(["login"]);
        }
        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();
  }
 
}