checkin_controller.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { Controller } from "@hotwired/stimulus"
  2. export default class extends Controller {
  3. connect() {
  4. // Écoute tous les clics sur .open-modal -> détournement vers #lastStep + MAJ des options du formulaire
  5. document.querySelectorAll('.participant-box').forEach(element => {
  6. element.addEventListener('click', this.handleClick.bind(this))
  7. })
  8. }
  9. handleClick(event) {
  10. event.preventDefault()
  11. const participantId = event.currentTarget.dataset.id
  12. const box = event.currentTarget;
  13. const icon = box.querySelector('.icon');
  14. if (!participantId) return
  15. // Etape 1
  16. fetch(`/checkin/participant/${participantId}`, {
  17. method: 'POST',
  18. headers: {
  19. 'Accept': 'application/json'
  20. }
  21. })
  22. .then(response => {
  23. if (!response.ok) {
  24. throw new Error(`Erreur API: ${response.status}`);
  25. }
  26. return response.json();
  27. })
  28. .then(data => {
  29. // Etape 2
  30. console.log('ID du particpant cliqué :', participantId);
  31. console.log('ÉTAT DU SLOT :', data.status);
  32. if (data.status === "CHECKED") {
  33. box.classList.add('has-background-primary');
  34. box.classList.remove('has-background-danger');
  35. icon.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-check2-circle" viewBox="0 0 16 16">'+
  36. '<path d="M2.5 8a5.5 5.5 0 0 1 8.25-4.764.5.5 0 0 0 .5-.866A6.5 6.5 0 1 0 14.5 8a.5.5 0 0 0-1 0 5.5 5.5 0 1 1-11 0"/>'+
  37. '<path d="M15.354 3.354a.5.5 0 0 0-.708-.708L8 9.293 5.354 6.646a.5.5 0 1 0-.708.708l3 3a.5.5 0 0 0 .708 0z"/>'+
  38. '</svg>';
  39. } else if (data.status === "UNCHECKED") {
  40. box.classList.remove('has-background-primary');
  41. box.classList.add('has-background-danger');
  42. icon.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-circle" viewBox="0 0 16 16">'+
  43. '<path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14m0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16"/>'+
  44. '</svg>';
  45. } else {
  46. console.warn("Statut de réponse sans action :", data.status);
  47. }
  48. })
  49. .catch(error => {
  50. console.error('Erreur lors de la récupération des slots :', error);
  51. });
  52. }
  53. disconnect() {
  54. this.element.querySelectorAll('.planning-cell-free').forEach(element => {
  55. element.removeEventListener('click', this.handleClick)
  56. })
  57. }
  58. }