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

78% Statements 39/50
64.28% Branches 18/28
81.81% Functions 9/11
76.08% Lines 35/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                                          107x   5x       5x 5x 5x 5x 5x 5x 5x         5x     4x 4x 4x 4x   4x 4x     4x 4x   4x     4x 1x   4x 4x           6x 6x   5x 5x                 2x 2x 2x   2x 2x     2x                     2x                           8x      
import { Component, Input, OnInit } from '@angular/core';
import { Event } from '../../models/events';
import { TwinpadApiService } from '../../services/twinpad-api.service';
import { ListResponse } from '../../models/response';
import { LoaderComponent } from '../loader/loader.component';
import { EmptyPlaceholderComponent } from '../empty-placeholder/empty-placeholder.component';
import { DatePipe } from '@angular/common';
import { TableModule, TableLazyLoadEvent, TablePageEvent } from 'primeng/table';
import { NgIf } from '@angular/common';
import { ButtonModule } from 'primeng/button';
import { ActivatedRoute, Router, RouterLink } from '@angular/router';
import { HttpErrorResponse } from '@angular/common/http';
import { ErrorComponent } from '../error/error.component';
 
@Component({
    selector: 'app-events',
    imports: [ErrorComponent, LoaderComponent, EmptyPlaceholderComponent, DatePipe, NgIf, TableModule, ButtonModule, RouterLink],
    providers: [],
    templateUrl: './events.component.html',
    styleUrl: './events.component.scss'
})
export class EventsComponent implements OnInit{
 
  @Input() inputEventRuleId: string|null = null;
 
  isLoading: boolean;
  eventsResponse: ListResponse<Event>;
  offset: number = 0;
  limit: number = 10;
  timestampMin: number|null = null;
  timestampMax: number|null = null;
  sortBy: string[] = ["timestamp"];
  defaultRowOptions = [10, 20, 50];
  filteredRowsPerPageOptions: number[] = [];
 
  error: HttpErrorResponse;
 
 
  constructor(private twinpadApiService: TwinpadApiService, private route: ActivatedRoute, private router: Router){}
 
  ngOnInit(): void {
    this.isLoading = true;
    this.route.queryParamMap.subscribe(params => {
      this.limit = 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.timestampMin = Number(timestampMin);
      }
      if (timestampMax !== null){
        this.timestampMax = Number(timestampMax);
      }
      this.updateRowsPerPageOptions();
      this.getData();
 
    });
  }
 
  getData(){
    this.isLoading = true;
    this.twinpadApiService.getEvents(this.limit, this.offset, this.sortBy, this.timestampMin, this.timestampMax, this.inputEventRuleId)
    .subscribe({next: (value) => {
      this.eventsResponse = value;
      this.isLoading = false;
    },
    error: (error)=>{
      this.error = error;
    }
  });
  }
 
  loadEvents(event: TableLazyLoadEvent){
    this.isLoading = true;
    Eif (event.first !== null && event.first !== undefined){
      this.offset = event.first;
    }
    Eif (event.rows !== null && event.rows !== undefined){
      this.limit = 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();
  }
 
  onTableChange(event: TablePageEvent){
    this.offset = event.first;
    this.limit = event.rows;
 
    this.router.navigate([], {
      queryParams: { offset: this.offset, rows: this.limit },
      queryParamsHandling: 'merge'
    });
  }
 
  updateRowsPerPageOptions(){
    this.filteredRowsPerPageOptions = [...new Set([...this.defaultRowOptions, this.limit])].sort((a, b) => a - b);
  }
}