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

82.5% Statements 33/40
61.53% Branches 16/26
91.66% Functions 11/12
81.08% Lines 30/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 94 95 96 97 98 99 100 101 102 103 104 105 106 107                                        72x       1x 1x   1x 1x   1x     1x 1x 2x 2x   2x 2x       2x 2x           2x   2x 6x             4x   4x 4x   4x                   2x 2x   2x 2x     2x                     2x       1x 1x   1x            
import { Component, OnInit } from '@angular/core';
import { NgIf } from '@angular/common';
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: [NgIf, TableModule, RouterModule, TagModule, DatePipe, Badge, Button],
  templateUrl: './commands-table.component.html',
  styleUrl: './commands-table.component.scss'
})
 
export class CommandsTableComponent implements OnInit {
  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];
 
  constructor(private twinpadApiService: TwinpadApiService, private router: Router, private route: ActivatedRoute) { }
 
  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.getCommands(this.rows, this.offset, this.filter, this.sortBy).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'
    });
  }
}