Subbu1304 commited on
Commit
e8f8a71
·
verified ·
1 Parent(s): a74e21c

Update templates/menu.html

Browse files
Files changed (1) hide show
  1. templates/menu.html +25 -25
templates/menu.html CHANGED
@@ -1047,7 +1047,7 @@ function addSoftDrinkToCart(name, price, image, section, category, index) {
1047
  // Hide the ADD button and show quantity controls
1048
  document.getElementById(`add-btn-${index}`).style.display = 'none';
1049
  document.getElementById(`quantity-control-${index}`).style.display = 'flex';
1050
- updateCartUI(data.cart);
1051
  alert('Soft Drink added to cart successfully!');
1052
  } else {
1053
  alert(data.error || 'Failed to add Soft Drink to cart.');
@@ -1186,31 +1186,31 @@ function decreaseQuantity(name, index) {
1186
 
1187
 
1188
  // Function to update cart quantity
1189
- function updateCartQuantity(name, quantity) {
1190
- fetch('/cart/update', {
1191
- method: 'POST',
1192
- headers: {
1193
- 'Content-Type': 'application/json',
1194
- },
1195
- body: JSON.stringify({ itemName: name, quantity: quantity })
1196
- })
1197
- .then(response => response.json())
1198
- .then(data => {
1199
- if (data.success) {
1200
- updateCartUI(data.cart);
1201
- } else {
1202
- alert(data.error || 'Failed to update cart quantity.');
1203
- }
1204
- })
1205
- .catch(err => {
1206
- console.error('Error updating cart quantity:', err);
1207
- alert('An error occurred while updating the cart quantity.');
1208
- });
1209
  }
1210
-
1211
- // Function to update the cart UI after changes
1212
- function updateCartUI(cart) {
1213
- // Your logic to update the cart UI (display total, items, etc.)
 
 
 
 
 
 
 
 
 
 
 
 
 
1214
  }
1215
 
1216
 
 
1047
  // Hide the ADD button and show quantity controls
1048
  document.getElementById(`add-btn-${index}`).style.display = 'none';
1049
  document.getElementById(`quantity-control-${index}`).style.display = 'flex';
1050
+ updateCartQuantity(data.cart);
1051
  alert('Soft Drink added to cart successfully!');
1052
  } else {
1053
  alert(data.error || 'Failed to add Soft Drink to cart.');
 
1186
 
1187
 
1188
  // Function to update cart quantity
1189
+ function updateCartQuantity(cart) {
1190
+ if (!Array.isArray(cart)) {
1191
+ console.error('Invalid cart data:', cart);
1192
+ return;
1193
+ }
1194
+ const cartIcon = document.getElementById('cart-icon');
1195
+ cartIcon.innerText = cart.length; // Assuming cart is an array of items
 
 
 
 
 
 
 
 
 
 
 
 
 
1196
  }
1197
+ function updateCartDisplay(cart) {
1198
+ if (!Array.isArray(cart)) {
1199
+ console.error('Invalid cart data:', cart);
1200
+ return;
1201
+ }
1202
+ // Optional: Update quantity on the cart page
1203
+ const cartCountElement = document.getElementById('cart-count');
1204
+ cartCountElement.innerText = cart.reduce((total, item)=> total+item.quantity,0); // Update cart item count //Sum of all quantities
1205
+
1206
+ // Optionally, show a small success notification that the item was added
1207
+ const successNotification = document.createElement('div');
1208
+ successNotification.classList.add('success-notification');
1209
+ successNotification.innerText = 'Item added to cart!';
1210
+ document.body.appendChild(successNotification);
1211
+ setTimeout(() => {
1212
+ successNotification.remove(); // Remove success notification after a few seconds
1213
+ }, 2000);
1214
  }
1215
 
1216