All files / src/app/components/commands-table commands-table.component.ts

80.85% Statements 38/47
60% Branches 18/30
90.9% Functions 10/11
80.43% Lines 37/46

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                                        111x 1x 1x 1x         1x 1x   1x 1x           1x 1x 2x 2x   2x 2x       2x 2x   2x     2x       2x 2x           2x   2x 6x             4x   4x 4x   4x                   2x 2x   2x 2x     2x                     2x       1x 1x   1x       110x    
import { Component, OnInit, inject } from '@angular/core';
 
import { TableLazyLoadEvent, TableModule, TablePageEvent } from 'primeng/table';
import { TwinpadApiService } from '../../services/twinpad-api.service';
import { ActivatedRoute, Router, RouterModule } from '@angular/router';
import { Command } from '../../models/commands';
import { ListResponse } from '../../models/response';
import { TagModule } from 'primeng/tag';
import { Badge } from 'primeng/badge';
import { Button } from 'primeng/button';
import { DatePipe } from '@angular/common';
 
 
@Component({
  selector: 'app-commands-table',
  imports: [TableModule, RouterModule, TagModule, DatePipe, Badge, Button],
  templateUrl: './commands-table.component.html',
  styleUrl: './commands-table.component.scss'
})
 
export class CommandsTableComponent implements OnInit {
  private twinpadApiService = inject(TwinpadApiService);
  private router = inject(Router);
  private route = inject(ActivatedRoute);
 
  commands: ListResponse<Command>;
  userNamesById: Map<string, string>;
  actualUserId: string;
  offset: number = 0;
  rows: number = 10;
  filter: string;
  sortBy: string[] = ["timestamp:-1"];
  defaultRowOptions = [10, 20, 50];
 
  minTimestamp: number;
  maxTimestamp: number;
 
  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(',');
      }
 
      const timestampMin = params.get('min_timestamp');
      const timestampMax = params.get('max_timestamp');
 
      Iif (timestampMin !== null){
        this.minTimestamp = Number(timestampMin);
      }
      Iif (timestampMax !== null){
        this.maxTimestamp = Number(timestampMax);
      }
 
      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.getCommands(this.rows, this.offset, this.filter, this.sortBy, this.minTimestamp, this.maxTimestamp).subscribe({
            next: value => {
              this.commands = value;
            },
            error: err => { console.log(err.error); }
          });
        }
      }
    });
  }
 
  loadCommands(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();
  }
 
  onTableEvent(event: TablePageEvent) {
    this.offset = event.first;
    this.rows = event.rows;
 
    this.router.navigate([], {
      queryParams: { offset: this.offset, rows: this.rows },
      queryParamsHandling: 'merge'
    });
  }
}