feat/free-ticket #2
57
app/javascript/controllers/event_duplication_controller.js
Normal file
57
app/javascript/controllers/event_duplication_controller.js
Normal file
@@ -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()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -27,3 +27,6 @@ application.register("event-form", EventFormController);
|
|||||||
|
|
||||||
import CountdownController from "./countdown_controller";
|
import CountdownController from "./countdown_controller";
|
||||||
application.register("countdown", CountdownController);
|
application.register("countdown", CountdownController);
|
||||||
|
|
||||||
|
import EventDuplicationController from "./event_duplication_controller";
|
||||||
|
application.register("event-duplication", EventDuplicationController);
|
||||||
|
|||||||
@@ -1,62 +1,51 @@
|
|||||||
<% content_for(:title, @event.name) %>
|
<% content_for(:title, @event.name) %>
|
||||||
|
|
||||||
<script>
|
<div data-controller="event-duplication" data-event-duplication-duplicate-url-value="<%= duplicate_promoter_event_path(@event) %>">
|
||||||
document.addEventListener('DOMContentLoaded', function() {
|
<!-- Modal -->
|
||||||
const showDuplicateModalBtn = document.getElementById('showDuplicateModal');
|
<div data-event-duplication-target="modal" class="fixed inset-0 z-50 hidden overflow-y-auto">
|
||||||
const duplicateModal = document.getElementById('duplicateModal');
|
<div class="flex items-end justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0">
|
||||||
const cancelDuplicateBtn = document.getElementById('cancelDuplicate');
|
<!-- Background overlay -->
|
||||||
const confirmDuplicateBtn = document.getElementById('confirmDuplicate');
|
<div class="fixed inset-0 bg-gray-500 bg-opacity-75 transition-opacity" aria-hidden="true"></div>
|
||||||
const cloneTicketTypesCheckbox = document.getElementById('cloneTicketTypes');
|
|
||||||
|
|
||||||
// Show modal when duplicate button is clicked
|
<!-- Modal container -->
|
||||||
showDuplicateModalBtn.addEventListener('click', function() {
|
<div class="inline-block align-bottom bg-white rounded-lg text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-lg sm:w-full">
|
||||||
duplicateModal.classList.remove('hidden');
|
<div class="bg-white px-4 pt-5 pb-4 sm:p-6 sm:pb-4">
|
||||||
});
|
<div class="sm:flex sm:items-start">
|
||||||
|
<div class="mx-auto flex-shrink-0 flex items-center justify-center h-12 w-12 rounded-full bg-blue-100 sm:mx-0 sm:h-10 sm:w-10">
|
||||||
|
<i data-lucide="copy" class="h-6 w-6 text-blue-600"></i>
|
||||||
|
</div>
|
||||||
|
<div class="mt-3 text-center sm:mt-0 sm:ml-4 sm:text-left">
|
||||||
|
<h3 class="text-lg leading-6 font-medium text-gray-900" id="modal-title">
|
||||||
|
Dupliquer l'événement
|
||||||
|
</h3>
|
||||||
|
<div class="mt-2">
|
||||||
|
<p class="text-sm text-gray-500">
|
||||||
|
Choisissez les options de duplication pour "<%= @event.name %>".
|
||||||
|
</p>
|
||||||
|
|
||||||
// Hide modal when cancel button is clicked
|
<div class="mt-4">
|
||||||
cancelDuplicateBtn.addEventListener('click', function() {
|
<div class="flex items-center">
|
||||||
duplicateModal.classList.add('hidden');
|
<input data-event-duplication-target="cloneTicketTypes" id="cloneTicketTypes" type="checkbox" class="h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded" checked>
|
||||||
});
|
<label for="cloneTicketTypes" class="ml-2 block text-sm text-gray-900">
|
||||||
|
Dupliquer également les types de billets (<%= @event.ticket_types.count %> type(s))
|
||||||
// Hide modal when clicking outside the modal
|
</label>
|
||||||
window.addEventListener('click', function(event) {
|
</div>
|
||||||
if (event.target === duplicateModal) {
|
</div>
|
||||||
duplicateModal.classList.add('hidden');
|
</div>
|
||||||
}
|
</div>
|
||||||
});
|
</div>
|
||||||
|
</div>
|
||||||
// Handle duplication when confirm button is clicked
|
<div class="bg-gray-50 px-4 py-3 sm:px-6 sm:flex sm:flex-row-reverse">
|
||||||
confirmDuplicateBtn.addEventListener('click', function() {
|
<button type="button" data-action="click->event-duplication#duplicate" class="w-full inline-flex justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-blue-600 text-base font-medium text-white hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 sm:ml-3 sm:w-auto sm:text-sm">
|
||||||
const cloneTicketTypes = cloneTicketTypesCheckbox.checked;
|
Dupliquer
|
||||||
|
</button>
|
||||||
// Create form data
|
<button type="button" data-action="click->event-duplication#close" class="mt-3 w-full inline-flex justify-center rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-base font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 sm:mt-0 sm:ml-3 sm:w-auto sm:text-sm">
|
||||||
const formData = new FormData();
|
Annuler
|
||||||
formData.append('clone_ticket_types', cloneTicketTypes);
|
</button>
|
||||||
formData.append('authenticity_token', document.querySelector('meta[name="csrf-token"]').getAttribute('content'));
|
</div>
|
||||||
|
</div>
|
||||||
// Send request to duplicate endpoint
|
</div>
|
||||||
fetch('<%= duplicate_promoter_event_path(@event) %>', {
|
</div>
|
||||||
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.');
|
|
||||||
duplicateModal.classList.add('hidden');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div class="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
|
<div class="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
|
||||||
|
|
||||||
@@ -97,59 +86,11 @@
|
|||||||
Modifier
|
Modifier
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<button id="showDuplicateModal" type="button" class="w-full sm:w-auto inline-flex items-center justify-center px-4 py-2 bg-blue-600 text-white font-medium rounded-lg hover:bg-blue-700 transition-colors duration-200">
|
<button type="button" data-action="click->event-duplication#open" class="w-full sm:w-auto inline-flex items-center justify-center px-4 py-2 bg-blue-600 text-white font-medium rounded-lg hover:bg-blue-700 transition-colors duration-200">
|
||||||
<i data-lucide="copy" class="w-4 h-4 mr-2"></i>
|
<i data-lucide="copy" class="w-4 h-4 mr-2"></i>
|
||||||
Dupliquer
|
Dupliquer
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
<!-- Duplication Modal -->
|
|
||||||
<div id="duplicateModal" class="fixed inset-0 z-50 hidden overflow-y-auto">
|
|
||||||
<div class="flex items-end justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0">
|
|
||||||
<!-- Background overlay -->
|
|
||||||
<div class="fixed inset-0 transition-opacity" aria-hidden="true">
|
|
||||||
<div class="absolute inset-0 bg-gray-500 opacity-75"></div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- Modal container -->
|
|
||||||
<div class="inline-block align-bottom bg-white rounded-lg text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-lg sm:w-full">
|
|
||||||
<div class="bg-white px-4 pt-5 pb-4 sm:p-6 sm:pb-4">
|
|
||||||
<div class="sm:flex sm:items-start">
|
|
||||||
<div class="mx-auto flex-shrink-0 flex items-center justify-center h-12 w-12 rounded-full bg-blue-100 sm:mx-0 sm:h-10 sm:w-10">
|
|
||||||
<i data-lucide="copy" class="h-6 w-6 text-blue-600"></i>
|
|
||||||
</div>
|
|
||||||
<div class="mt-3 text-center sm:mt-0 sm:ml-4 sm:text-left">
|
|
||||||
<h3 class="text-lg leading-6 font-medium text-gray-900" id="modal-title">
|
|
||||||
Dupliquer l'événement
|
|
||||||
</h3>
|
|
||||||
<div class="mt-2">
|
|
||||||
<p class="text-sm text-gray-500">
|
|
||||||
Choisissez les options de duplication pour "<%= @event.name %>".
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<div class="mt-4">
|
|
||||||
<div class="flex items-center">
|
|
||||||
<input id="cloneTicketTypes" type="checkbox" class="h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded" checked>
|
|
||||||
<label for="cloneTicketTypes" class="ml-2 block text-sm text-gray-900">
|
|
||||||
Dupliquer également les types de billets (<%= @event.ticket_types.count %> type(s))
|
|
||||||
</label>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="bg-gray-50 px-4 py-3 sm:px-6 sm:flex sm:flex-row-reverse">
|
|
||||||
<button id="confirmDuplicate" type="button" class="w-full inline-flex justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-blue-600 text-base font-medium text-white hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 sm:ml-3 sm:w-auto sm:text-sm">
|
|
||||||
Dupliquer
|
|
||||||
</button>
|
|
||||||
<button id="cancelDuplicate" type="button" class="mt-3 w-full inline-flex justify-center rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-base font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 sm:mt-0 sm:ml-3 sm:w-auto sm:text-sm">
|
|
||||||
Annuler
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<% if @event.draft? %>
|
<% if @event.draft? %>
|
||||||
<% if @event.ticket_types.blank? %>
|
<% if @event.ticket_types.blank? %>
|
||||||
<%= button_to publish_promoter_event_path(@event), method: :patch, disabled: true, class: "w-full sm:w-auto inline-flex items-center justify-center px-4 py-2 bg-gray-400 text-white font-medium rounded-lg cursor-not-allowed transition-colors duration-200", title: "Vous devez créer au moins un type de billet avant de publier" do %>
|
<%= button_to publish_promoter_event_path(@event), method: :patch, disabled: true, class: "w-full sm:w-auto inline-flex items-center justify-center px-4 py-2 bg-gray-400 text-white font-medium rounded-lg cursor-not-allowed transition-colors duration-200", title: "Vous devez créer au moins un type de billet avant de publier" do %>
|
||||||
|
|||||||
Reference in New Issue
Block a user