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

95.23% Statements 40/42
82.14% Branches 23/28
100% Functions 12/12
94.59% Lines 35/37

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                                  100x     4x 4x   4x 4x 4x 4x     4x     4x 13x 13x 13x 13x   13x     4x       13x 13x 13x       13x 13x 33x 33x       13x           2x       9x 9x 9x   9x 9x   9x 6x       6x   6x     9x       12x      
import { Component, OnDestroy, OnInit } from '@angular/core';
import { TableLazyLoadEvent, TableModule } from 'primeng/table';
import { Badge } from 'primeng/badge';
import { Configuration } from '../../models/configuration';
import { TwinpadApiService } from '../../services/twinpad-api.service';
import { ActivatedRoute, Params, Router, RouterModule } from '@angular/router';
import { LoaderComponent } from '../loader/loader.component';
import { CommonModule, NgIf } from '@angular/common';
import { Subscription } from 'rxjs';
 
@Component({
  selector: 'app-configurations',
  imports: [TableModule, RouterModule, LoaderComponent, NgIf, CommonModule, Badge],
  templateUrl: './configurations.component.html',
  styleUrl: './configurations.component.scss'
})
 
export class ConfigurationsComponent implements OnInit, OnDestroy {
  configurations: Configuration[];
  configurationSubscription: Subscription;
  rows: number = 10;
  offset: number = 0;
  total: number;
  sortField: string = "";
  sortOrder: number = 1;
  defaultRowOptions = [1, 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.sortField = params["sort_field"] ?? "";
      this.sortOrder = params["sort_order"] ?? 1;
 
      this.getData();
    });
 
    this.updateRowsPerPageOptions();
  }
 
  getData() {
    this.configurationSubscription?.unsubscribe();
    this.configurationSubscription = this.twinpadApiService.callPeriodically(() => {
      return this.twinpadApiService.getConfigs(this.rows, this.offset, this.sortField !== "" ? this.sortField + ":" + this.sortOrder : null);
    }, 20000)
      .subscribe({
        'next': value => {
          this.configurations = value.items;
          for (const config of this.configurations) {
            config.device_ids = config.in_use_by_devices.map(deviceId => deviceId.slice(8));
            Iif (config.device_ids.length === 0) {
              config.device_ids.push(config.config.target_device_id.slice(8));
            }
          }
          this.total = value.total;
        }
      });
  }
 
  ngOnDestroy() {
    this.configurationSubscription?.unsubscribe();
  }
 
  onTableEvent(event: TableLazyLoadEvent) {
    const params: Params = {};
    Eif (event.first !== undefined) {
      params["offset"] = event.first;
    }
    Eif (event.rows !== undefined && event.rows !== null) {
      params["rows"] = event.rows;
    }
    if (event.sortField !== null && event.sortField !== undefined) {
      Iif (Array.isArray(event.sortField)) {
        params["sort_field"] = event.sortField[0];
      }
      else {
        params["sort_field"] = event.sortField;
      }
      params["sort_order"] = event.sortOrder ?? 1;
    }
 
    this.router.navigate([], {queryParams: params});
  }
 
  updateRowsPerPageOptions() {
    this.filteredRowsPerPageOptions = [...new Set([...this.defaultRowOptions, this.rows])].sort((a, b) => a - b);
  }
}