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

97.67% Statements 42/43
85.71% Branches 24/28
100% Functions 11/11
97.5% Lines 39/40

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                                  122x 7x 7x 7x       7x 7x   7x 7x 7x 7x     7x 17x 17x 17x 17x   17x     7x       17x 17x 17x       17x 17x 105x 105x 46x     17x           5x       10x 10x 10x   10x 10x   10x 7x       7x   7x     10x       21x 115x    
import { Component, OnDestroy, OnInit, inject } 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 } from '@angular/common';
import { Subscription } from 'rxjs';
 
@Component({
  selector: 'app-configurations',
  imports: [TableModule, RouterModule, LoaderComponent, CommonModule, Badge],
  templateUrl: './configurations.component.html',
  styleUrl: './configurations.component.scss'
})
 
export class ConfigurationsComponent implements OnInit, OnDestroy {
  private twinpadApiService = inject(TwinpadApiService);
  private router = inject(Router);
  private route = inject(ActivatedRoute);
 
  configurations: Configuration[];
  configurationSubscription: Subscription;
  rows: number = 10;
  offset: number = 0;
  total: number;
  sortField: string = "";
  sortOrder: number = 1;
  defaultRowOptions = [1, 10, 20, 50];
  filteredRowsPerPageOptions: number[] = [];
 
  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));
            if (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);
  }
}