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

67.74% Statements 21/31
50% Branches 6/12
81.81% Functions 9/11
61.53% Lines 16/26

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                                37x       2x 2x   2x 2x     2x     2x 2x 2x 2x   2x     2x   2x 2x                                   2x                           4x        
import { Component, OnDestroy, OnInit } from '@angular/core';
import { TableModule, TablePageEvent } from 'primeng/table';
import { BadgeModule } from 'primeng/badge';
import { Device } from '../../models/devices';
import { TwinpadApiService } from '../../services/twinpad-api.service';
import { ActivatedRoute, Router, RouterModule } from '@angular/router';
import { LoaderComponent } from '../loader/loader.component';
import { DecimalPipe, NgFor, NgIf } from '@angular/common';
import { Subscription } from 'rxjs';
 
@Component({
    selector: 'app-devices',
    imports: [TableModule, RouterModule, LoaderComponent, NgIf, NgFor, DecimalPipe, BadgeModule],
    templateUrl: './devices.component.html',
    styleUrl: './devices.component.css'
})
export class DevicesComponent implements OnInit, OnDestroy {
 
  devices: Device[];
  deviceSubscription: Subscription;
  rows: number = 10;
  offset: number = 0;
  status: string;
  defaultRowOptions = [10, 20, 50];
  filteredRowsPerPageOptions: number[] = [];
 
 
  constructor(private twinpadApiService: TwinpadApiService, private router: Router, private route: ActivatedRoute) {}
 
  ngOnInit() {
    this.route.queryParams.subscribe(params => {
      this.rows = params['rows'] ? +params['rows'] : 10;
      this.offset = params['offset']? +params['offset'] : 0;
      this.status = params['status']? params['status'] : undefined;
    });
    this.updateRowsPerPageOptions();
 
 
    this.deviceSubscription = this.twinpadApiService.callPeriodically(() => {return this.twinpadApiService.getDevices();}, 3000)
      .subscribe({'next': value => {
        this.devices=value;
        Iif(this.status !== undefined){
          let order: number;
          switch(this.status){
            case "up":
              order = -1;
              break;
            case "down":
              order = 1;
              break;
          }
          this.devices.sort((a: Device, b: Device) => {
            return a.status?.toString().toLowerCase().localeCompare(b.status?.toString().toLowerCase()) * order;
          });
        }
      }
    });
  }
  ngOnDestroy(){
    this.deviceSubscription?.unsubscribe();
  }
 
  onTableEvent(event: TablePageEvent){
    this.offset = event.first;
    this.rows = event.rows;
 
    this.router.navigate([], {
      queryParams: { offset: this.offset, rows: this.rows },
      queryParamsHandling: 'merge'
    });
  }
 
  updateRowsPerPageOptions(){
    this.filteredRowsPerPageOptions = [...new Set([...this.defaultRowOptions, this.rows])].sort((a, b) => a - b);
  }
 
}