lokesh341 commited on
Commit
db28e53
·
verified ·
1 Parent(s): 801f090

Update templates/menu.html

Browse files
Files changed (1) hide show
  1. templates/menu.html +100 -26
templates/menu.html CHANGED
@@ -396,7 +396,8 @@
396
  font-weight: bold;
397
  line-height: 30px;
398
  }
399
- .quantity-selector .quantity-to-add {
 
400
  width: 50px;
401
  height: 30px;
402
  font-size: 14px;
@@ -526,14 +527,17 @@
526
  <button class="btn btn-primary add-to-cart-btn" onclick="handleSoftDrinkAdd(this)">ADD</button>
527
  <div class="quantity-selector" style="display: none;">
528
  <button class="btn btn-outline-secondary decrease-btn" onclick="decreaseQuantity(this)">-</button>
529
- <span class="quantity-display">1</span>
 
 
 
 
 
530
  <button class="btn btn-outline-secondary increase-btn" onclick="increaseQuantity(this)">+</button>
531
  <select class="quantity-to-add">
532
- <option value="1">1</option>
533
- <option value="3">3</option>
534
- <option value="5">5</option>
535
- <option value="10">10</option>
536
- <option value="20">20</option>
537
  </select>
538
  </div>
539
  {% else %}
@@ -716,6 +720,45 @@
716
  }
717
  };
718
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
719
  // Populate Item Details on Page Load
720
  document.addEventListener('DOMContentLoaded', function () {
721
  const menuCards = document.querySelectorAll('.menu-card');
@@ -1147,6 +1190,12 @@
1147
  .catch(err => {
1148
  console.error('Error adding item to cart:', err);
1149
  alert('An error occurred while adding the item to the cart: ' + err.message);
 
 
 
 
 
 
1150
  });
1151
  }
1152
 
@@ -1207,9 +1256,10 @@
1207
  .catch(err => {
1208
  console.error('Error adding item to cart:', err);
1209
  alert('An error occurred while adding the item to the cart: ' + err.message);
1210
- // Revert UI if addition fails
1211
- addButton.style.display = 'block';
1212
- quantitySelector.style.display = 'none';
 
1213
  });
1214
  }
1215
 
@@ -1238,7 +1288,7 @@
1238
  category: category,
1239
  addons: [],
1240
  instructions: '',
1241
- quantity: quantityToAdd // Add the selected quantity
1242
  };
1243
 
1244
  // Disable the button to prevent multiple clicks
@@ -1272,9 +1322,9 @@
1272
  .catch(err => {
1273
  console.error('Error adding item to cart:', err);
1274
  alert('An error occurred while adding the item to the cart: ' + err.message);
1275
- // Revert quantity if addition fails
1276
- currentQuantity -= quantityToAdd;
1277
- quantityDisplay.innerText = currentQuantity;
1278
  })
1279
  .finally(() => {
1280
  button.disabled = false; // Re-enable the button
@@ -1284,14 +1334,16 @@
1284
  function decreaseQuantity(button) {
1285
  const buttonContainer = button.closest('.button-container');
1286
  const quantityDisplay = buttonContainer.querySelector('.quantity-display');
 
 
1287
  const addButton = buttonContainer.querySelector('.add-to-cart-btn');
1288
  const quantitySelector = buttonContainer.querySelector('.quantity-selector');
1289
- let quantity = parseInt(quantityDisplay.innerText);
1290
 
1291
- if (quantity <= 1) {
1292
  // Remove the item from the cart completely
1293
  const itemName = buttonContainer.getAttribute('data-item-name');
1294
- fetch(`/cart/remove?item_name=${encodeURIComponent(itemName)}`, {
1295
  method: 'POST',
1296
  headers: {
1297
  'Content-Type': 'application/json',
@@ -1309,6 +1361,7 @@
1309
  // Hide quantity selector and show ADD button
1310
  addButton.style.display = 'block';
1311
  quantitySelector.style.display = 'none';
 
1312
  } else {
1313
  console.error('Failed to remove item from cart:', data.error);
1314
  alert(data.error || 'Failed to remove item from cart.');
@@ -1317,13 +1370,19 @@
1317
  .catch(err => {
1318
  console.error('Error removing item from cart:', err);
1319
  alert('An error occurred while removing the item from the cart: ' + err.message);
 
 
 
 
 
 
1320
  });
1321
  } else {
1322
- quantity--;
1323
- quantityDisplay.innerText = quantity;
1324
 
1325
  const itemName = buttonContainer.getAttribute('data-item-name');
1326
- fetch(`/cart/remove?item_name=${encodeURIComponent(itemName)}&quantity=1`, {
1327
  method: 'POST',
1328
  headers: {
1329
  'Content-Type': 'application/json',
@@ -1337,19 +1396,21 @@
1337
  })
1338
  .then(data => {
1339
  if (data.success) {
 
1340
  updateCartUI(data.cart);
1341
  } else {
1342
  console.error('Failed to remove item from cart:', data.error);
1343
  alert(data.error || 'Failed to remove item from cart.');
1344
- quantity++;
1345
- quantityDisplay.innerText = quantity;
1346
  }
1347
  })
1348
  .catch(err => {
1349
  console.error('Error removing item from cart:', err);
1350
  alert('An error occurred while removing the item from the cart: ' + err.message);
1351
- quantity++;
1352
- quantityDisplay.innerText = quantity;
 
1353
  });
1354
  }
1355
  }
@@ -1372,12 +1433,17 @@
1372
  const addButton = container.querySelector('.add-to-cart-btn');
1373
  const quantitySelector = container.querySelector('.quantity-selector');
1374
 
1375
- const cartItem = cart.find(item => item.itemName === itemName);
 
 
 
 
1376
  if (cartItem && cartItem.quantity > 0) {
1377
  quantityDisplay.innerText = cartItem.quantity;
1378
  addButton.style.display = 'none';
1379
  quantitySelector.style.display = 'flex';
1380
  } else {
 
1381
  addButton.style.display = 'block';
1382
  quantitySelector.style.display = 'none';
1383
  }
@@ -1435,9 +1501,17 @@
1435
  updateCartUI(data.cart);
1436
  } else {
1437
  console.error('Failed to fetch cart:', data.error);
 
 
 
1438
  }
1439
  })
1440
- .catch(err => console.error('Error fetching cart:', err));
 
 
 
 
 
1441
  });
1442
  </script>
1443
  </body>
 
396
  font-weight: bold;
397
  line-height: 30px;
398
  }
399
+ .quantity-selector .quantity-to-add,
400
+ .quantity-selector .quantity-to-remove {
401
  width: 50px;
402
  height: 30px;
403
  font-size: 14px;
 
527
  <button class="btn btn-primary add-to-cart-btn" onclick="handleSoftDrinkAdd(this)">ADD</button>
528
  <div class="quantity-selector" style="display: none;">
529
  <button class="btn btn-outline-secondary decrease-btn" onclick="decreaseQuantity(this)">-</button>
530
+ <select class="quantity-to-remove">
531
+ {% for i in range(1, 21) %}
532
+ <option value="{{ i }}">{{ i }}</option>
533
+ {% endfor %}
534
+ </select>
535
+ <span class="quantity-display">0</span>
536
  <button class="btn btn-outline-secondary increase-btn" onclick="increaseQuantity(this)">+</button>
537
  <select class="quantity-to-add">
538
+ {% for i in range(1, 21) %}
539
+ <option value="{{ i }}">{{ i }}</option>
540
+ {% endfor %}
 
 
541
  </select>
542
  </div>
543
  {% else %}
 
720
  }
721
  };
722
 
723
+ // Local Storage Fallback Functions (Temporary)
724
+ function addToCartLocalStorage(payload) {
725
+ let cart = JSON.parse(localStorage.getItem('cart')) || [];
726
+ const existingItem = cart.find(item =>
727
+ item.itemName === payload.itemName &&
728
+ item.instructions === payload.instructions &&
729
+ JSON.stringify(item.addons) === JSON.stringify(payload.addons)
730
+ );
731
+ if (existingItem) {
732
+ existingItem.quantity += payload.quantity;
733
+ } else {
734
+ cart.push(payload);
735
+ }
736
+ localStorage.setItem('cart', JSON.stringify(cart));
737
+ return cart;
738
+ }
739
+
740
+ function removeFromCartLocalStorage(itemName, quantityToRemove, instructions, addons) {
741
+ let cart = JSON.parse(localStorage.getItem('cart')) || [];
742
+ const itemIndex = cart.findIndex(item =>
743
+ item.itemName === itemName &&
744
+ item.instructions === instructions &&
745
+ JSON.stringify(item.addons) === JSON.stringify(addons)
746
+ );
747
+ if (itemIndex !== -1) {
748
+ if (quantityToRemove >= cart[itemIndex].quantity) {
749
+ cart.splice(itemIndex, 1);
750
+ } else {
751
+ cart[itemIndex].quantity -= quantityToRemove;
752
+ }
753
+ }
754
+ localStorage.setItem('cart', JSON.stringify(cart));
755
+ return cart;
756
+ }
757
+
758
+ function getCartLocalStorage() {
759
+ return JSON.parse(localStorage.getItem('cart')) || [];
760
+ }
761
+
762
  // Populate Item Details on Page Load
763
  document.addEventListener('DOMContentLoaded', function () {
764
  const menuCards = document.querySelectorAll('.menu-card');
 
1190
  .catch(err => {
1191
  console.error('Error adding item to cart:', err);
1192
  alert('An error occurred while adding the item to the cart: ' + err.message);
1193
+ // Fallback to localStorage
1194
+ const cart = addToCartLocalStorage(cartPayload);
1195
+ updateCartUI(cart);
1196
+ const modal = document.getElementById('itemModal');
1197
+ const modalInstance = bootstrap.Modal.getInstance(modal);
1198
+ modalInstance.hide();
1199
  });
1200
  }
1201
 
 
1256
  .catch(err => {
1257
  console.error('Error adding item to cart:', err);
1258
  alert('An error occurred while adding the item to the cart: ' + err.message);
1259
+ // Fallback to localStorage
1260
+ const cart = addToCartLocalStorage(cartPayload);
1261
+ updateCartUI(cart);
1262
+ quantityDisplay.innerText = 1;
1263
  });
1264
  }
1265
 
 
1288
  category: category,
1289
  addons: [],
1290
  instructions: '',
1291
+ quantity: quantityToAdd
1292
  };
1293
 
1294
  // Disable the button to prevent multiple clicks
 
1322
  .catch(err => {
1323
  console.error('Error adding item to cart:', err);
1324
  alert('An error occurred while adding the item to the cart: ' + err.message);
1325
+ // Fallback to localStorage
1326
+ const cart = addToCartLocalStorage(cartPayload);
1327
+ updateCartUI(cart);
1328
  })
1329
  .finally(() => {
1330
  button.disabled = false; // Re-enable the button
 
1334
  function decreaseQuantity(button) {
1335
  const buttonContainer = button.closest('.button-container');
1336
  const quantityDisplay = buttonContainer.querySelector('.quantity-display');
1337
+ const quantityToRemoveSelect = buttonContainer.querySelector('.quantity-to-remove');
1338
+ const quantityToRemove = parseInt(quantityToRemoveSelect.value);
1339
  const addButton = buttonContainer.querySelector('.add-to-cart-btn');
1340
  const quantitySelector = buttonContainer.querySelector('.quantity-selector');
1341
+ let currentQuantity = parseInt(quantityDisplay.innerText);
1342
 
1343
+ if (currentQuantity <= quantityToRemove) {
1344
  // Remove the item from the cart completely
1345
  const itemName = buttonContainer.getAttribute('data-item-name');
1346
+ fetch(`/cart/remove?item_name=${encodeURIComponent(itemName)}&quantity=${currentQuantity}&instructions=&addons=[]`, {
1347
  method: 'POST',
1348
  headers: {
1349
  'Content-Type': 'application/json',
 
1361
  // Hide quantity selector and show ADD button
1362
  addButton.style.display = 'block';
1363
  quantitySelector.style.display = 'none';
1364
+ quantityDisplay.innerText = 0;
1365
  } else {
1366
  console.error('Failed to remove item from cart:', data.error);
1367
  alert(data.error || 'Failed to remove item from cart.');
 
1370
  .catch(err => {
1371
  console.error('Error removing item from cart:', err);
1372
  alert('An error occurred while removing the item from the cart: ' + err.message);
1373
+ // Fallback to localStorage
1374
+ const cart = removeFromCartLocalStorage(itemName, currentQuantity, '', []);
1375
+ updateCartUI(cart);
1376
+ addButton.style.display = 'block';
1377
+ quantitySelector.style.display = 'none';
1378
+ quantityDisplay.innerText = 0;
1379
  });
1380
  } else {
1381
+ currentQuantity -= quantityToRemove;
1382
+ quantityDisplay.innerText = currentQuantity;
1383
 
1384
  const itemName = buttonContainer.getAttribute('data-item-name');
1385
+ fetch(`/cart/remove?item_name=${encodeURIComponent(itemName)}&quantity=${quantityToRemove}&instructions=&addons=[]`, {
1386
  method: 'POST',
1387
  headers: {
1388
  'Content-Type': 'application/json',
 
1396
  })
1397
  .then(data => {
1398
  if (data.success) {
1399
+ alert(`Removed ${quantityToRemove} item(s) from cart successfully!`);
1400
  updateCartUI(data.cart);
1401
  } else {
1402
  console.error('Failed to remove item from cart:', data.error);
1403
  alert(data.error || 'Failed to remove item from cart.');
1404
+ currentQuantity += quantityToRemove;
1405
+ quantityDisplay.innerText = currentQuantity;
1406
  }
1407
  })
1408
  .catch(err => {
1409
  console.error('Error removing item from cart:', err);
1410
  alert('An error occurred while removing the item from the cart: ' + err.message);
1411
+ // Fallback to localStorage
1412
+ const cart = removeFromCartLocalStorage(itemName, quantityToRemove, '', []);
1413
+ updateCartUI(cart);
1414
  });
1415
  }
1416
  }
 
1433
  const addButton = container.querySelector('.add-to-cart-btn');
1434
  const quantitySelector = container.querySelector('.quantity-selector');
1435
 
1436
+ const cartItem = cart.find(item =>
1437
+ item.itemName === itemName &&
1438
+ item.instructions === '' &&
1439
+ JSON.stringify(item.addons) === JSON.stringify([])
1440
+ );
1441
  if (cartItem && cartItem.quantity > 0) {
1442
  quantityDisplay.innerText = cartItem.quantity;
1443
  addButton.style.display = 'none';
1444
  quantitySelector.style.display = 'flex';
1445
  } else {
1446
+ quantityDisplay.innerText = 0;
1447
  addButton.style.display = 'block';
1448
  quantitySelector.style.display = 'none';
1449
  }
 
1501
  updateCartUI(data.cart);
1502
  } else {
1503
  console.error('Failed to fetch cart:', data.error);
1504
+ // Fallback to localStorage
1505
+ const cart = getCartLocalStorage();
1506
+ updateCartUI(cart);
1507
  }
1508
  })
1509
+ .catch(err => {
1510
+ console.error('Error fetching cart:', err);
1511
+ // Fallback to localStorage
1512
+ const cart = getCartLocalStorage();
1513
+ updateCartUI(cart);
1514
+ });
1515
  });
1516
  </script>
1517
  </body>