Subbu1304 commited on
Commit
29fa242
·
verified ·
1 Parent(s): 061a460

Update templates/menu.html

Browse files
Files changed (1) hide show
  1. templates/menu.html +93 -9
templates/menu.html CHANGED
@@ -963,7 +963,46 @@ document.addEventListener('DOMContentLoaded', function () {
963
  });
964
 
965
  // Function to add Soft Drink to cart directly
966
- function addSoftDrinkToCart(name, price, image, section, category, index) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
967
  const itemPrice = parseFloat(price);
968
  const quantity = 1; // Default quantity
969
 
@@ -978,6 +1017,7 @@ function addSoftDrinkToCart(name, price, image, section, category, index) {
978
  quantity: quantity
979
  };
980
 
 
981
  fetch('/cart/add', {
982
  method: 'POST',
983
  headers: {
@@ -988,10 +1028,10 @@ function addSoftDrinkToCart(name, price, image, section, category, index) {
988
  .then(response => response.json())
989
  .then(data => {
990
  if (data.success) {
991
- // Hide the ADD button and show quantity controls
992
  document.getElementById(`add-btn-${index}`).style.display = 'none';
993
  document.getElementById(`quantity-control-${index}`).style.display = 'flex';
994
- updateCartUI(data.cart);
995
  alert('Soft Drink added to cart successfully!');
996
  } else {
997
  alert(data.error || 'Failed to add Soft Drink to cart.');
@@ -1013,16 +1053,37 @@ function increaseQuantity(name, index) {
1013
  }
1014
 
1015
  // Function to decrease quantity
1016
- function decreaseQuantity(name, index) {
 
 
 
 
 
 
 
 
 
 
 
 
 
1017
  let quantityElement = document.getElementById(`quantity-${index}`);
1018
  let currentQuantity = parseInt(quantityElement.innerText);
1019
- if (currentQuantity > 1) {
 
 
1020
  currentQuantity--;
1021
  quantityElement.innerText = currentQuantity;
1022
- updateCartQuantity(name, currentQuantity);
1023
- } else {
1024
- // Remove from cart and revert to ADD button
1025
- removeFromCart(name, index);
 
 
 
 
 
 
1026
  }
1027
  }
1028
  // Existing updateCartUI function (unchanged, but included for reference)
@@ -1036,6 +1097,29 @@ function updateCartUI(cart) {
1036
  cartIcon.innerText = cart.length; // Update cart icon if it exists
1037
  }
1038
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1039
 
1040
  </script>
1041
 
 
963
  });
964
 
965
  // Function to add Soft Drink to cart directly
966
+ // function addSoftDrinkToCart(name, price, image, section, category, index) {
967
+ // const itemPrice = parseFloat(price);
968
+ // const quantity = 1; // Default quantity
969
+
970
+ // const cartPayload = {
971
+ // itemName: name,
972
+ // itemPrice: itemPrice,
973
+ // itemImage: image || '/static/placeholder.jpg',
974
+ // section: section,
975
+ // category: category,
976
+ // addons: [], // No add-ons for Soft Drinks
977
+ // instructions: '', // No instructions for Soft Drinks
978
+ // quantity: quantity
979
+ // };
980
+
981
+ // fetch('/cart/add', {
982
+ // method: 'POST',
983
+ // headers: {
984
+ // 'Content-Type': 'application/json',
985
+ // },
986
+ // body: JSON.stringify(cartPayload)
987
+ // })
988
+ // .then(response => response.json())
989
+ // .then(data => {
990
+ // if (data.success) {
991
+ // // Hide the ADD button and show quantity controls
992
+ // document.getElementById(`add-btn-${index}`).style.display = 'none';
993
+ // document.getElementById(`quantity-control-${index}`).style.display = 'flex';
994
+ // updateCartUI(data.cart);
995
+ // alert('Soft Drink added to cart successfully!');
996
+ // } else {
997
+ // alert(data.error || 'Failed to add Soft Drink to cart.');
998
+ // }
999
+ // })
1000
+ // .catch(err => {
1001
+ // console.error('Error adding Soft Drink to cart:', err);
1002
+ // alert('An error occurred while adding the Soft Drink to the cart.');
1003
+ // });
1004
+ // }
1005
+ function addSoftDrinkToCart(name, price, image, section, category, index) {
1006
  const itemPrice = parseFloat(price);
1007
  const quantity = 1; // Default quantity
1008
 
 
1017
  quantity: quantity
1018
  };
1019
 
1020
+ // Add item to cart
1021
  fetch('/cart/add', {
1022
  method: 'POST',
1023
  headers: {
 
1028
  .then(response => response.json())
1029
  .then(data => {
1030
  if (data.success) {
1031
+ // Hide the "Add" button and show quantity controls
1032
  document.getElementById(`add-btn-${index}`).style.display = 'none';
1033
  document.getElementById(`quantity-control-${index}`).style.display = 'flex';
1034
+ updateCartQuantity(data.cart);
1035
  alert('Soft Drink added to cart successfully!');
1036
  } else {
1037
  alert(data.error || 'Failed to add Soft Drink to cart.');
 
1053
  }
1054
 
1055
  // Function to decrease quantity
1056
+ // function decreaseQuantity(name, index) {
1057
+ // let quantityElement = document.getElementById(`quantity-${index}`);
1058
+ // let currentQuantity = parseInt(quantityElement.innerText);
1059
+ // if (currentQuantity > 1) {
1060
+ // currentQuantity--;
1061
+ // quantityElement.innerText = currentQuantity;
1062
+ // updateCartQuantity(name, currentQuantity);
1063
+ // } else {
1064
+ // // Remove from cart and revert to ADD button
1065
+ // removeFromCart(name, index);
1066
+ // }
1067
+ // }
1068
+
1069
+ function decreaseQuantity(name, index) {
1070
  let quantityElement = document.getElementById(`quantity-${index}`);
1071
  let currentQuantity = parseInt(quantityElement.innerText);
1072
+
1073
+ // If quantity > 0, decrease the quantity
1074
+ if (currentQuantity > 0) {
1075
  currentQuantity--;
1076
  quantityElement.innerText = currentQuantity;
1077
+
1078
+ // If quantity becomes 0, show "Add" button and hide quantity controls
1079
+ if (currentQuantity === 0) {
1080
+ document.getElementById(`add-btn-${index}`).style.display = 'block'; // Show "Add" button
1081
+ document.getElementById(`quantity-control-${index}`).style.display = 'none'; // Hide quantity controls
1082
+ removeFromCart(name, index); // Remove item from the cart when quantity reaches 0
1083
+ } else {
1084
+ // Otherwise, update cart quantity
1085
+ updateCartQuantity(name, currentQuantity);
1086
+ }
1087
  }
1088
  }
1089
  // Existing updateCartUI function (unchanged, but included for reference)
 
1097
  cartIcon.innerText = cart.length; // Update cart icon if it exists
1098
  }
1099
  }
1100
+
1101
+ // Remove Item from Cart when quantity reaches 0
1102
+ function removeFromCart(name, index) {
1103
+ fetch('/cart/remove_item', {
1104
+ method: 'DELETE',
1105
+ headers: { 'Content-Type': 'application/json' },
1106
+ body: JSON.stringify({ item_name: name.trim() })
1107
+ })
1108
+ .then(response => response.json())
1109
+ .then(data => {
1110
+ if (data.success) {
1111
+ updateCartDisplay(data.cart); // Update the cart UI after removal
1112
+ } else {
1113
+ alert(data.error || "Failed to remove item from cart.");
1114
+ }
1115
+ })
1116
+ .catch(err => console.error("Error removing item from cart:", err));
1117
+ }
1118
+
1119
+
1120
+
1121
+
1122
+
1123
 
1124
  </script>
1125