admin_confirm_controller.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { Controller } from '@hotwired/stimulus';
  2. /*
  3. * Contrôleur Stimulus pour bouton de suppression avec confirmation.
  4. * Sur le premier clic, le bouton devient "Confirmer" et son href est mis à jour.
  5. */
  6. export default class extends Controller {
  7. static values = {
  8. baseUrl: String // Par exemple : "/items/delete/"
  9. }
  10. connect() {
  11. console.log("Stimulus: bouton de suppression avec confirmation");
  12. this.originalText = this.element.textContent;
  13. this.confirmed = false;
  14. this.element.addEventListener('click', this.handleClick.bind(this));
  15. }
  16. handleClick(event) {
  17. if (!this.confirmed) {
  18. event.preventDefault();
  19. // Récupère l'ID depuis data-id
  20. const id = this.element.dataset.id;
  21. if (!id) {
  22. console.error("Aucun data-id trouvé sur l'élément.");
  23. return;
  24. }
  25. // Remplace le texte et modifie le lien
  26. this.element.textContent = "Confirmer ?";
  27. this.element.href = id;
  28. this.confirmed = true;
  29. // Optionnel : annule la confirmation après X secondes
  30. setTimeout(() => {
  31. this.element.textContent = this.originalText;
  32. this.element.href = "#";
  33. this.confirmed = false;
  34. }, 5000);
  35. }
  36. // Sinon, clic confirmé => laisse le lien agir normalement
  37. }
  38. }