All files / src/app/components/forced-signals forced-signals.component.ts

70% Statements 35/50
57.14% Branches 16/28
78.57% Functions 11/14
69.38% Lines 34/49

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                                      111x 1x 1x 1x 1x         1x 1x   1x 1x     1x 1x 1x 1x   1x 1x       1x 1x           1x   1x 3x             3x   3x 3x   2x                   1x 1x   1x 1x     1x                     1x       1x   1x 1x                                         110x    
import { Component, inject, OnInit } from '@angular/core';
import { DatePipe } from '@angular/common';
import { ActivatedRoute, Router, RouterModule } from '@angular/router';
 
import { TableLazyLoadEvent, TableModule, TablePageEvent } from 'primeng/table';
import { TagModule } from 'primeng/tag';
import { Button } from 'primeng/button';
import { MessageService } from 'primeng/api';
 
import { TwinpadApiService } from '../../services/twinpad-api.service';
import { ForcedSignal } from '../../models/signals';
import { ListResponse } from '../../models/response';
 
@Component({
  selector: 'app-forced-signals',
  imports: [TableModule, RouterModule, TagModule, DatePipe, Button],
  templateUrl: './forced-signals.component.html',
  styleUrl: './forced-signals.component.scss'
})
export class ForcedSignalsComponent implements OnInit {
  private twinpadApiService = inject(TwinpadApiService);
  private messageService = inject(MessageService);
  private router = inject(Router);
  private route = inject(ActivatedRoute);
 
  forcedSignals: ListResponse<ForcedSignal>;
  userNamesById: Map<string, string>;
  actualUserId: string;
  offset: number = 0;
  rows: number = 10;
  filter: string;
  sortBy: string[] = ["forced_at:-1"];
  defaultRowOptions = [10, 20, 50];
 
  ngOnInit(): void {
    this.userNamesById = new Map();
    this.route.queryParamMap.subscribe(params => {
      this.rows = Number(params.get('rows')) || 10;
      this.offset = Number(params.get('offset')) || 0;
 
      const newSortBy = params.get('sortBy');
      Iif (newSortBy) {
        this.sortBy = newSortBy.split(',');
      }
 
      this.getUsers();
      this.getData();
    });
  }
 
  getUsers() {
    // get distinct users
    this.twinpadApiService.getUsers().subscribe({
      next: value => {
        for (const user of value) {
          this.userNamesById.set(user.id, `${user.firstname} ${user.lastname}`);
        }
      }
    });
  }
 
  getData() {
    this.twinpadApiService.getProfile().subscribe({
      next: user => {
        Eif (user?.is_admin) {
          this.twinpadApiService.getForcedSignals(this.rows, this.offset, this.sortBy).subscribe({
            next: value => {
              this.forcedSignals = value;
            },
            error: err => { console.log(err.error); }
          });
        }
      }
    });
  }
 
  loadForcedSignals(event: TableLazyLoadEvent) {
    Eif (event.first !== null && event.first !== undefined) {
      this.offset = event.first;
    }
    Eif (event.rows !== null && event.rows !== undefined) {
      this.rows = event.rows;
    }
 
    Iif (event.sortField !== undefined && event.sortField !== null) {
      if (typeof (event.sortField) === 'string') {
        this.sortBy = [event.sortField];
      }
      else {
        this.sortBy = event.sortField;
      }
      if (event.sortOrder != null) {
        this.sortBy[0] += ':' + event.sortOrder;
      }
    }
    this.getData();
  }
 
  unForceSignal(signalId: string) {
    this.twinpadApiService.sendSignalUnforce(signalId).subscribe({
      next: _ => {
        this.messageService.add({ severity: 'success', summary: 'Unforce successful', detail: signalId + ' reset to normal value' });
        this.getData();
      },
      error: error => {
        let severity: string = "error";
        if (error.status === 504) {
          severity = "warn";
        }
        this.messageService.add({ severity: severity, summary: 'Command error', detail: error.message });
        this.getData();
      }
    });
  }
 
  onTableEvent(event: TablePageEvent) {
    this.offset = event.first;
    this.rows = event.rows;
 
    this.router.navigate([], {
      queryParams: { offset: this.offset, rows: this.rows },
      queryParamsHandling: 'merge'
    });
  }
}