From 9d99cd572adebbe5e6998283c52eb761a2f20b43 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 7 Oct 2025 16:39:03 -0300 Subject: [PATCH] fix --- public/buyModal.js | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/public/buyModal.js b/public/buyModal.js index 4a3614e..57a5280 100644 --- a/public/buyModal.js +++ b/public/buyModal.js @@ -119,8 +119,6 @@ function createModal() { return modal; } - - function openModal(productHref) { const modal = document.getElementById("buyNowModal"); modal.style.display = "flex"; @@ -132,16 +130,26 @@ function openModal(productHref) { confirmation.style.display = "none"; // Parse productHref - let product = "Unknown"; - let price = "0"; let id = ""; + let price = "0"; + let product = "Unknown"; - if (productHref.includes("/")) { - const parts = productHref.split("/").filter(Boolean); - id = parts[0] || ""; - price = parts[1] || "0"; - product = parts.slice(2).join("/"); // Join remaining parts for product name - product = decodeURIComponent(product); // Decode URL-encoded spaces etc. + try { + const urlObj = new URL(productHref); + const pathParts = urlObj.pathname.split("/").filter(Boolean); + + if (pathParts.length === 2) { + // No ID in URL: /price/product + price = pathParts[0]; + product = decodeURIComponent(pathParts[1]); + } else if (pathParts.length >= 3) { + // ID present: /id/price/product + id = pathParts[0]; + price = pathParts[1]; + product = decodeURIComponent(pathParts.slice(2).join("/")); + } + } catch (e) { + console.error("Invalid URL:", productHref); } // Show/hide location select if ID starts with "_" @@ -174,6 +182,8 @@ function openModal(productHref) { + + function handleFormSubmit() { const form = document.getElementById("buyForm"); const confirmation = document.getElementById("confirmation");