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

90% Statements 18/20
66.66% Branches 4/6
87.5% Functions 7/8
87.5% Lines 14/16

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                                      62x 3x 3x     3x 3x         3x 3x       3x     3x 3x       1x   1x 1x 1x                  
import { ChangeDetectorRef, Component, Input, OnChanges, SimpleChanges } from '@angular/core';
import { Router } from '@angular/router';
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 { Subscription } from 'rxjs';
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{
  private deviceSubscription: Subscription = new Subscription;
  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 router: Router, private messageService: MessageService, private changeDetectorRef: ChangeDetectorRef){}
 
  ngOnChanges(changes: SimpleChanges): void {
    this.isLoading = false;
    this._device = changes['device'].currentValue;
  }
 
  changeMode(newMode:Mode){
    this.twinpadApiService.changeDeviceMode(this._device, newMode).subscribe({
      next: _ => {
        this.isLoading = true;
        this.messageService.add({severity: 'success', summary:  'Mode switch sent', detail: 'Switch mode command has been successfully sent to '+this._device.name });
        this._currentModeId = this._device.current_mode_id;
      },
      error: error => {
        console.log(error);
        this.messageService.add({severity: 'error', summary:  'Mode switch error', detail: error.message });
      }
    });
  }
}