From 2fd43f3caac5c743a626dd55eac19b6dbec0ada3 Mon Sep 17 00:00:00 2001 From: 65160381 <65160381@go.buu.ac.th> Date: Tue, 25 Mar 2025 04:36:13 +0700 Subject: [PATCH] 10.2 --- public/edit-order.html | 46 +++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/public/edit-order.html b/public/edit-order.html index 012d668..fc03a29 100644 --- a/public/edit-order.html +++ b/public/edit-order.html @@ -59,7 +59,8 @@ ${order.products.map(product => ` <li> ${product.name} - $${product.price} x ${product.quantity} - <input type="number" value="${product.quantity}" min="1" id="product-quantity-${product.id}" /> + <input type="number" id="quantity-${product.product_id}" value="${product.quantity}" min="1"> + <button onclick="updateProductQuantity(${product.product_id})">Update Quantity</button> </li> `).join('')} </ul> @@ -72,7 +73,6 @@ // Update order details async function updateOrder() { - // Get orderId from the URL query string const urlParams = new URLSearchParams(window.location.search); const orderId = urlParams.get('orderId'); @@ -84,15 +84,6 @@ const status = document.getElementById('order-status').value; const shippingAddress = document.getElementById('order-shipping-address').value; - // Get updated quantities of each product - const updatedProducts = []; - const productsList = document.querySelectorAll('#order-products li'); - productsList.forEach(productItem => { - const productId = productItem.dataset.productId; - const quantity = document.getElementById(`product-quantity-${productId}`).value; - updatedProducts.push({ productId, quantity }); - }); - try { const response = await fetch(`/api/orders/${orderId}`, { method: 'PUT', @@ -101,8 +92,7 @@ }, body: JSON.stringify({ status: status, - shipping_address: shippingAddress, - products: updatedProducts + shipping_address: shippingAddress }) }); @@ -119,9 +109,37 @@ } } + // Update product quantity in order + async function updateProductQuantity(productId) { + const newQuantity = document.getElementById(`quantity-${productId}`).value; + + const urlParams = new URLSearchParams(window.location.search); + const orderId = urlParams.get('orderId'); + + try { + const response = await fetch(`/api/orders/${orderId}/products/${productId}`, { + method: 'PUT', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ quantity: newQuantity }) + }); + + if (response.ok) { + alert('Product quantity updated!'); + loadOrderDetails(); // Reload the order details after update + } else { + const errorMessage = await response.text(); + alert('Error updating product quantity: ' + errorMessage); + } + } catch (error) { + console.error('Error updating product quantity:', error); + alert('Error updating product quantity'); + } + } + // Load order details when page loads window.onload = loadOrderDetails; </script> - </body> </html> -- GitLab