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

84.44% Statements 38/45
76.19% Branches 16/21
73.33% Functions 11/15
82.5% Lines 33/40

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 117 118 119 120                                101x                         5x       5x             5x 5x 5x 3x   3x 3x 3x 3x 3x 3x 3x                   3x 3x 3x 3x 3x 3x             2x 2x   2x 2x 2x                   1x 1x   1x                   1x 1x   1x 1x 1x                          
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router, RouterModule } from '@angular/router';
import { TwinpadApiService } from '../../services/twinpad-api.service';
import { MessageService } from 'primeng/api';
import { FormBuilder, FormGroup, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { NgIf } from '@angular/common';
import { DatePicker } from 'primeng/datepicker';
import { SignalsGraphsComponent } from '../signals-graphs/signals-graphs.component';
import { DurationOption } from '../../models/signals';
 
@Component({
  selector: 'app-phase',
  imports: [NgIf, FormsModule, ReactiveFormsModule, RouterModule, DatePicker, SignalsGraphsComponent],
  templateUrl: './phase.component.html',
  styleUrl: './phase.component.scss'
})
export class PhaseComponent implements OnInit {
  phaseForm: FormGroup;
  campaignId: string;
  phaseId?: string;
  phaseName: string;
  phaseDescription: string;
  phaseStartAt: Date;
  phaseEndAt: Date;
  dataDateMin: Date;
  dataDateMax: Date;
  phaseMiddle: number;
  phaseDuration: DurationOption;
 
  constructor(private route: ActivatedRoute, private fb: FormBuilder, private twinpadApiService: TwinpadApiService, private router: Router, private messageService: MessageService) { }
 
  ngOnInit(): void {
    // Initialize the form
    this.phaseForm = this.fb.group({
      phaseName: [],
      phaseDescription: [],
      phaseDateStart: [],
      phaseDateEnd: []
    });
 
    this.campaignId = this.route.snapshot.params['campaign_id'] || null;
    this.phaseId = this.route.snapshot.params['phase_id'] || null;
    if (this.phaseId != null) {
      this.twinpadApiService.getPhase(this.phaseId).subscribe({
        next: value => {
          this.phaseForm.get("phaseName")?.setValue(value.name);
          this.phaseForm.get("phaseDescription")?.setValue(value.description);
          this.phaseForm.get("phaseDateStart")?.setValue(new Date(value.start_at));
          this.phaseForm.get("phaseDateEnd")?.setValue(new Date(value.end_at));
          this.campaignId = value.campaign_id;
          this.phaseMiddle = (value.start_at + value.end_at) / 2;
          this.phaseDuration = { label: "Full phase", duration: (value.end_at - value.start_at) / 1000 };
        },
        error: error => {
          this.messageService.add({ severity: 'error', summary: 'Get Phase', detail: error.error });
        }
      });
    }
  }
 
  getInfos() {
    this.phaseName = this.phaseForm.get("phaseName")?.value;
    this.phaseDescription = this.phaseForm.get("phaseDescription")?.value;
    this.phaseStartAt = this.phaseForm.get("phaseDateStart")?.value;
    this.phaseEndAt = this.phaseForm.get("phaseDateEnd")?.value;
    Eif (this.phaseName != null && this.phaseStartAt != null && this.phaseEndAt != null) {
      return true;
    }
    this.messageService.add({ severity: 'error', summary: 'Phase', detail: "One or more fields are required" });
    return false;
  }
 
  addPhase() {
    Eif (this.getInfos()) {
      this.twinpadApiService.addPhase(this.campaignId, this.phaseName, this.phaseStartAt.getTime(), this.phaseEndAt.getTime(), this.phaseDescription).subscribe({
        next: value => {
          const responseId = Object.values(value);
          this.messageService.add({ severity: 'success', summary: 'Add Phase', detail: "Phase successfully created" });
          this.router.navigate([`/phases/${responseId}`]);
        },
        error: error => {
          this.messageService.add({ severity: 'error', summary: 'Add Phase', detail: error.error });
        }
      });
    }
  }
 
  editPhase() {
    Eif (this.getInfos()) {
      this.twinpadApiService.editPhase(this.phaseName, this.phaseStartAt.getTime(), this.phaseEndAt.getTime(), this.campaignId, this.phaseId, this.phaseDescription).subscribe({
        next: _ => {
          this.messageService.add({ severity: 'success', summary: 'Edit Phase', detail: "Phase successfully edited" });
        },
        error: error => {
          this.messageService.add({ severity: 'error', summary: 'Edit Phase', detail: error.error });
        }
      });
    }
  }
 
  deletePhase() {
    Eif (confirm("Are you sure to delete " + this.phaseName + "?")) {
      this.twinpadApiService.deletePhase(this.phaseId).subscribe({
        next: value => {
          Eif (value) {
            this.messageService.add({ severity: 'success', summary: 'Delete Phase', detail: "Phase successfully deleted" });
            this.router.navigate(['/campaigns/' + this.campaignId]);
          }
          else {
            this.messageService.add({ severity: 'error', summary: 'Delete Phase', detail: "An error occurred while deleting phase" });
          }
        },
        error: error => {
          this.messageService.add({ severity: 'error', summary: 'Delete Phase', detail: error.error });
        }
      });
    }
  }
}