60 lines
1.6 KiB
JavaScript
60 lines
1.6 KiB
JavaScript
import { Controller } from "@hotwired/stimulus"
|
|
|
|
export default class extends Controller {
|
|
static targets = ["cloneTicketTypes"]
|
|
static values = {
|
|
duplicateUrl: String
|
|
}
|
|
|
|
connect() {
|
|
// Get modal element from the document
|
|
this.modalElement = document.querySelector('[data-event-duplication-target="modal"]')
|
|
|
|
// Close modal when clicking outside
|
|
this.modalElement.addEventListener('click', (event) => {
|
|
if (event.target === this.modalElement) {
|
|
this.close()
|
|
}
|
|
})
|
|
}
|
|
|
|
open() {
|
|
this.modalElement.classList.remove('hidden')
|
|
document.body.classList.add('overflow-hidden')
|
|
}
|
|
|
|
close() {
|
|
this.modalElement.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()
|
|
})
|
|
}
|
|
} |