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

73.91% Statements 17/23
50% Branches 5/10
87.5% Functions 7/8
71.42% Lines 15/21

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                                  113x 3x     3x 3x         3x 3x       3x     3x 3x       1x 1x   1x 1x         1x 1x                          
import { Component, Input, OnChanges, SimpleChanges } from '@angular/core';
import { TwinpadApiService } from '../../services/twinpad-api.service';
import { TableModule } from 'primeng/table';
import { ToastModule } from 'primeng/toast';
import { NgIf, NgClass } from '@angular/common';
import { Device, Mode } from '../../models/devices';
import { MessageService } from 'primeng/api';
import { LoaderComponent } from '../loader/loader.component';
 
 
@Component({
  selector: 'app-modes-table',
  imports: [TableModule, ToastModule, NgIf, NgClass, LoaderComponent],
  standalone: true,
  templateUrl: './modes-table.component.html',
  styleUrl: './modes-table.component.scss'
})
export class ModesTableComponent implements OnChanges{
  isLoading: boolean = false;
  _device: Device;
  @Input() set device(value: Device){
    Eif(this._device !== value){
      this._device = value;
    }
  }
  _currentModeId: number;
  @Input() set currentModeId(value: number){
    Eif(this._currentModeId !== value){
      this._currentModeId = value;
    }
  }
 
  constructor(private twinpadApiService: TwinpadApiService, private messageService: MessageService){}
 
  ngOnChanges(changes: SimpleChanges): void {
    this.isLoading = false;
    this._device = changes['device'].currentValue;
  }
 
  changeMode(newMode:Mode){
    this.isLoading = true;
    this.twinpadApiService.changeDeviceMode(this._device, newMode).subscribe({
      next: response => {
        Eif (!response.error) {
          this.messageService.add({severity: 'success', summary:  'Mode switch success', detail: response.message });
        }
        else {
          this.messageService.add({severity: 'error', summary:  'Mode switch error', detail: response.message });
        }
        this._currentModeId = this._device.current_mode_id;
        this.isLoading = false;
      },
      error: error => {
        this.isLoading = false;
        let severity: string = "error";
        if (error.status === 504) {
          severity = "warn";
        }
        this.messageService.add({severity: severity, summary:  'Mode switch error', detail: error.error.detail });
      }
    });
  }
}