All files / app/liste-projets liste-projets.component.ts

31.25% Statements 10/32
0% Branches 0/8
36.36% Functions 4/11
30% Lines 9/30

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                                  1x 2x   2x 2x   2x 2x             1x       1x 1x                                                                                                    
 
import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import {
  FormBuilder,
  FormGroup,
  Validators,
  ReactiveFormsModule,
} from '@angular/forms';
import { ProjetService, Projet } from '../projet.service';
 
@Component({
  selector: 'app-liste-projets',
  standalone: true,
  imports: [CommonModule, ReactiveFormsModule],
  templateUrl: './liste-projets.component.html',
})
export class ListeProjetsComponent implements OnInit {
  projets: Projet[] = [];
  projetForm: FormGroup;
  editing = false;
  editedProjetId: number | null = null;
 
  constructor(private projetService: ProjetService, private fb: FormBuilder) {
    this.projetForm = this.fb.group({
      nom: ['', Validators.required],
      description: [''],
    });
  }
 
  ngOnInit(): void {
    this.chargerProjets();
  }
 
  chargerProjets(): void {
    this.projetService.getProjets().subscribe((data) => {
      this.projets = data;
    });
  }
 
  onSubmit(): void {
    Iif (this.projetForm.invalid) return;
 
    const projetData = this.projetForm.value;
 
    if (this.editing && this.editedProjetId !== null) {
      this.projetService
        .updateProjet(this.editedProjetId, projetData)
        .subscribe((response) => {
          this.chargerProjets();
          this.cancelEdit();
          alert('Projet mis à jour avec succès !');
        });
    } else {
      this.projetService.addProjet(projetData).subscribe(() => {
        this.projetForm.reset();
        this.chargerProjets();
        alert('Projet ajouté avec succès !');
      });
    }
  }
 
  onEdit(projet: Projet): void {
    this.editing = true;
    this.editedProjetId = projet.id || null;
    this.projetForm.patchValue({
      nom: projet.nom,
      description: projet.description,
    });
  }
 
  cancelEdit(): void {
    this.editing = false;
    this.editedProjetId = null;
    this.projetForm.reset();
  }
 
  onDelete(id: number): void {
    Iif (confirm('Voulez-vous vraiment supprimer ce projet ?')) {
      this.projetService.deleteProjet(id).subscribe(() => {
        this.chargerProjets();
        alert('Projet supprimé.');
      });
    }
  }
}