All files / src/app/components/device device.component.ts

90.47% Statements 19/21
100% Branches 4/4
80% Functions 8/10
88.88% Lines 16/18

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                                            78x     7x   7x       7x     5x 5x 5x 5x     6x 6x 3x     3x         3x       6x                   5x 5x      
import { Component, OnDestroy, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Signal } from '../../models/signals';
import { TableModule } from 'primeng/table';
import { ToastModule } from 'primeng/toast';
import { NgIf } from '@angular/common';
import { Subscription } from 'rxjs';
import { SignalsTableComponent } from '../signals-table/signals-table.component';
import { ModesTableComponent } from '../modes-table/modes-table.component';
import { Device } from '../../models/devices';
import { DeviceCardComponent } from '../device-card/device-card.component';
import { HttpErrorResponse } from '@angular/common/http';
import { ErrorComponent } from '../error/error.component';
import { TwinpadApiService } from '../../services/twinpad-api.service';
 
@Component({
    selector: 'app-device',
    imports: [TableModule, ToastModule, NgIf, SignalsTableComponent, DeviceCardComponent, ErrorComponent, ModesTableComponent],
    providers: [],
    templateUrl: './device.component.html',
    styleUrl: './device.component.scss'
})
export class DeviceComponent implements OnInit, OnDestroy {
  deviceId: string;
  device: Device;
  private deviceSubscription: Subscription = new Subscription;
  private routeSub: Subscription;
  isLoading: boolean = false;
  signals: Signal[];
  error: HttpErrorResponse;
 
  constructor(private twinpadApiService: TwinpadApiService, private route: ActivatedRoute) {}
 
  ngOnInit() {
    this.routeSub = this.route.params.subscribe(params => {
      this.deviceId = params['device_id'];
      this.deviceSubscription?.unsubscribe();
      this.deviceSubscription = this.twinpadApiService.devicesBehaviorSubject$.subscribe(
        {
          next: devices => {
            const device = devices.find(device => device?.device_id === this.deviceId);
            if (device !== undefined) {
              this.device = device;
            }
            else {
              this.twinpadApiService.getDevice(this.deviceId).subscribe({
                next: device => {
                  this.device = device;
                },
                error: error => {
                  this.error = error;
                }
              });
            }
            this.isLoading = false;
          },
          error: error => {
            this.error = error;
          }
        });
    });
  }
 
  ngOnDestroy(): void {
    this.deviceSubscription?.unsubscribe();
    this.routeSub?.unsubscribe();
  }
}