Fix ticket quantity buttons on event page - Remove syntax error in ticket card component and improve error handling in ticket cart controller
This commit is contained in:
@@ -16,6 +16,11 @@ export default class extends Controller {
|
||||
const ticketTypeId = event.params.ticketTypeId
|
||||
const max = parseInt(event.params.max)
|
||||
const input = this.quantityTargetFor(ticketTypeId)
|
||||
|
||||
if (!input) {
|
||||
console.error(`Could not find input for ticket type ID: ${ticketTypeId}`)
|
||||
return
|
||||
}
|
||||
|
||||
const current = parseInt(input.value) || 0
|
||||
if (current < max) {
|
||||
@@ -27,6 +32,11 @@ export default class extends Controller {
|
||||
decreaseQuantity(event) {
|
||||
const ticketTypeId = event.params.ticketTypeId
|
||||
const input = this.quantityTargetFor(ticketTypeId)
|
||||
|
||||
if (!input) {
|
||||
console.error(`Could not find input for ticket type ID: ${ticketTypeId}`)
|
||||
return
|
||||
}
|
||||
|
||||
const current = parseInt(input.value) || 0
|
||||
if (current > 0) {
|
||||
@@ -38,6 +48,12 @@ export default class extends Controller {
|
||||
updateQuantityFromInput(event) {
|
||||
const input = event.target
|
||||
const ticketTypeId = input.dataset.ticketTypeId
|
||||
|
||||
if (!ticketTypeId) {
|
||||
console.error('Missing ticket type ID on input element')
|
||||
return
|
||||
}
|
||||
|
||||
const max = parseInt(input.max)
|
||||
const quantity = parseInt(input.value) || 0
|
||||
|
||||
@@ -308,6 +324,10 @@ export default class extends Controller {
|
||||
|
||||
// Helper method to find quantity input by ticket type ID
|
||||
quantityTargetFor(ticketTypeId) {
|
||||
return document.querySelector(`#quantity_${ticketTypeId}`)
|
||||
const element = document.querySelector(`#quantity_${ticketTypeId}`)
|
||||
if (!element) {
|
||||
console.warn(`Could not find quantity input for ticket type ID: ${ticketTypeId}`)
|
||||
}
|
||||
return element
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,8 @@
|
||||
data-ticket-cart-target="quantity"
|
||||
data-ticket-type-id="<%= id %>"
|
||||
data-name="<%= name %>"
|
||||
data-price="<%= price_cents %>">
|
||||
data-price="<%= price_cents %>"
|
||||
data-action="change->ticket-cart#updateQuantityFromInput">
|
||||
<button type="button"
|
||||
class="w-8 h-8 rounded-full bg-gray-100 hover:bg-gray-200 flex items-center justify-center transition-colors duration-200"
|
||||
data-action="click->ticket-cart#increaseQuantity"
|
||||
|
||||
Reference in New Issue
Block a user