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

83.72% Statements 36/43
76.19% Branches 16/21
73.33% Functions 11/15
82.05% Lines 32/39

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                            57x 5x                     5x       5x             5x 5x 5x 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, inject } 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';
 
@Component({
  selector: 'app-phase',
  imports: [NgIf, FormsModule, ReactiveFormsModule, RouterModule, DatePicker],
  templateUrl: './phase.component.html',
  styleUrl: './phase.component.scss'
})
export class PhaseComponent implements OnInit {
  private messageService = inject(MessageService);
  phaseForm: FormGroup;
  campaignId : string;
  phaseId?: string;
  phaseName: string;
  phaseDescription: string;
  phaseStartAt: Date;
  phaseEndAt: Date;
  dataDateMin: Date;
  dataDateMax: Date;
 
  constructor(private route: ActivatedRoute, private fb: FormBuilder, private twinpadApiService: TwinpadApiService, private router: Router){}
 
  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;
        },
        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 many 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});
        }
      });
    }
  }
}