gamemaster-toggle_controller.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { Controller } from '@hotwired/stimulus';
  2. export default class extends Controller {
  3. static values = {
  4. eventId: String, // UUIDv7 de l'événement
  5. gamemasterId: String // UUIDv7 du MJ
  6. }
  7. connect() {
  8. console.log("Stimulus: toggle gamemaster actif");
  9. this.element.addEventListener('click', this.toggleGamemaster.bind(this));
  10. }
  11. async toggleGamemaster(event) {
  12. event.preventDefault();
  13. const eventId = this.eventIdValue;
  14. const gamemasterId = this.gamemasterIdValue;
  15. if (!eventId || !gamemasterId) {
  16. console.error("UUID manquant pour event ou gamemaster");
  17. return;
  18. }
  19. const url = `/admin/event/${eventId}/configure/gamemaster/toggle/`;
  20. console.log(gamemasterId);
  21. try {
  22. const response = await fetch(url, {
  23. method: 'POST',
  24. headers: {
  25. 'Content-Type': 'application/json',
  26. 'X-Requested-With': 'XMLHttpRequest',
  27. //'X-CSRF-TOKEN': this.getCsrfToken()
  28. },
  29. body: JSON.stringify({ gamemasterId: gamemasterId })
  30. });
  31. if (!response.ok) {
  32. console.error("Erreur HTTP :", response.status);
  33. return;
  34. }
  35. const data = await response.json();
  36. const article = this.element.querySelector('article.media');
  37. if (!article) {
  38. console.error("Élément <article> introuvable");
  39. return;
  40. }
  41. if (data.status === "DISABLE") {
  42. article.style.filter = "grayscale(1)";
  43. article.style.opacity = "0.3";
  44. } else if (data.status === "ENABLE") {
  45. article.style.filter = "none";
  46. article.style.opacity = "1";
  47. } else {
  48. console.warn("Statut de réponse inattendu :", data.status);
  49. }
  50. } catch (error) {
  51. console.error("Erreur réseau :", error);
  52. }
  53. }
  54. getCsrfToken() {
  55. const token = document.querySelector('meta[name="csrf-token"]');
  56. return token ? token.content : '';
  57. }
  58. }