All files / src/app/components/device-deployer device-deployer.component.ts

80.76% Statements 21/26
100% Branches 4/4
69.23% Functions 9/13
80% Lines 20/25

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                                      111x             3x 3x 3x 3x 3x       3x 4x 4x 1x     3x 3x 3x 3x 1x                             3x 3x       1x   1x                 108x    
import { Component, OnInit, OnDestroy, inject } from '@angular/core';
import { TwinpadApiService } from '../../services/twinpad-api.service';
import { ActivatedRoute, Router, RouterLink } from '@angular/router';
import { Subscription } from 'rxjs';
import { DeviceDeployer, DeviceFromDeployer } from '../../models/devices';
import { NgClass, NgForOf } from '@angular/common';
import { LoaderComponent } from '../loader/loader.component';
import { InputTextModule } from 'primeng/inputtext';
import { FloatLabelModule } from 'primeng/floatlabel';
import { FormsModule } from '@angular/forms';
import { MessageService } from 'primeng/api';
 
 
@Component({
  selector: 'app-device-deployer',
  imports: [LoaderComponent, NgForOf, NgClass, RouterLink, InputTextModule, FloatLabelModule, FormsModule],
  templateUrl: './device-deployer.component.html',
  styleUrl: './device-deployer.component.scss'
})
export class DeviceDeployerComponent implements OnInit, OnDestroy {
 
  private routeSub: Subscription;
  devicesSubscription: Subscription;
  deviceDeployerId: string;
  deviceDeployer: DeviceDeployer;
  devices: DeviceFromDeployer[];
  devicesError: boolean = false;
  private messageService = inject(MessageService);
  twinpadApiService = inject(TwinpadApiService);
  route = inject(ActivatedRoute);
  router = inject(Router);
 
 
  ngOnInit() {
    this.routeSub = this.route.params.subscribe(params => {
      this.deviceDeployerId = params['device_deployer_id'];
      if (this.deviceDeployerId == "new") {
        this.deviceDeployer = new DeviceDeployer;
      }
      else{
        this.twinpadApiService.getDeviceDeployer(this.deviceDeployerId).subscribe({next: (deployer) =>{
          this.deviceDeployer = deployer;
          this.devicesSubscription = this.twinpadApiService.callPeriodically(
            () => this.twinpadApiService.getDevicesFromDeployer(this.deviceDeployerId), 2000).subscribe({next: devices => {
              this.devices = devices;
            },
            error: _ =>{
              this.devicesError = true;
              this.devices = [];
            }
          });
        }});
 
      }
 
    });
  }
 
  ngOnDestroy(): void {
    this.routeSub?.unsubscribe();
    this.devicesSubscription?.unsubscribe();
  }
 
  addDeployer(){
    this.twinpadApiService.addDeployer(this.deviceDeployer).subscribe({
      next: deployer => {
        this.router.navigate(['/device-deployers/', deployer.id]);
      },
      error: error => this.messageService.add({severity: 'error', summary:  'Cannot create deployer', detail: error.error })});
  }
 
  deleteDeployer(){
    this.twinpadApiService.deleteDeployer(this.deviceDeployerId).subscribe({next: _ => {
      this.router.navigate(['/device-deployers/']);
    }});
  }
}