From b9576b91f5f599e5137ec40315e63d337177d556 Mon Sep 17 00:00:00 2001 From: kbe Date: Thu, 28 Aug 2025 20:56:48 +0200 Subject: [PATCH] Fix ticket quantity buttons on event page - Remove syntax error in ticket card component and improve error handling in ticket cart controller --- .../controllers/ticket_cart_controller.js | 22 ++++++++++++++++++- app/views/components/_ticket_card.html.erb | 3 ++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/app/javascript/controllers/ticket_cart_controller.js b/app/javascript/controllers/ticket_cart_controller.js index a93fdcc..e995dc6 100755 --- a/app/javascript/controllers/ticket_cart_controller.js +++ b/app/javascript/controllers/ticket_cart_controller.js @@ -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 } } diff --git a/app/views/components/_ticket_card.html.erb b/app/views/components/_ticket_card.html.erb index eac9e77..38b9e18 100755 --- a/app/views/components/_ticket_card.html.erb +++ b/app/views/components/_ticket_card.html.erb @@ -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">