diff --git a/app/javascript/controllers/event_duplication_controller.js b/app/javascript/controllers/event_duplication_controller.js new file mode 100644 index 0000000..7b9650f --- /dev/null +++ b/app/javascript/controllers/event_duplication_controller.js @@ -0,0 +1,57 @@ +import { Controller } from "@hotwired/stimulus" + +export default class extends Controller { + static targets = ["modal", "cloneTicketTypes"] + static values = { + duplicateUrl: String + } + + connect() { + // Close modal when clicking outside + this.modalTarget.addEventListener('click', (event) => { + if (event.target === this.modalTarget) { + this.close() + } + }) + } + + open() { + this.modalTarget.classList.remove('hidden') + document.body.classList.add('overflow-hidden') + } + + close() { + this.modalTarget.classList.add('hidden') + document.body.classList.remove('overflow-hidden') + } + + duplicate() { + const cloneTicketTypes = this.cloneTicketTypesTarget.checked + + // Create form data + const formData = new FormData() + formData.append('clone_ticket_types', cloneTicketTypes) + formData.append('authenticity_token', document.querySelector('meta[name="csrf-token"]').getAttribute('content')) + + // Send request to duplicate endpoint + fetch(this.duplicateUrlValue, { + method: 'POST', + body: formData, + headers: { + 'X-Requested-With': 'XMLHttpRequest' + } + }) + .then(response => { + if (response.redirected) { + window.location.href = response.url + } else { + return response.json() + } + }) + .catch(error => { + console.error('Error:', error) + alert('Erreur lors de la duplication de l\'événement.') + this.close() + }) + } +} \ No newline at end of file diff --git a/app/javascript/controllers/index.js b/app/javascript/controllers/index.js index c2e384d..f69d4f3 100755 --- a/app/javascript/controllers/index.js +++ b/app/javascript/controllers/index.js @@ -27,3 +27,6 @@ application.register("event-form", EventFormController); import CountdownController from "./countdown_controller"; application.register("countdown", CountdownController); + +import EventDuplicationController from "./event_duplication_controller"; +application.register("event-duplication", EventDuplicationController); diff --git a/app/views/promoter/events/show.html.erb b/app/views/promoter/events/show.html.erb index 930de4d..42b7058 100644 --- a/app/views/promoter/events/show.html.erb +++ b/app/views/promoter/events/show.html.erb @@ -1,64 +1,53 @@ <% content_for(:title, @event.name) %> - +
+ +