DSatishchandra commited on
Commit
69ed1dd
·
verified ·
1 Parent(s): 73b3e90

Update templates/menu.html

Browse files
Files changed (1) hide show
  1. templates/menu.html +57 -49
templates/menu.html CHANGED
@@ -506,56 +506,64 @@
506
 
507
 
508
  function addToCartFromModal() {
509
- const itemName = document.getElementById('modal-name').innerText;
510
- const itemPrice = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
511
- const itemImage = document.getElementById('modal-img').src;
512
- const modalSectionEl = document.getElementById('modal-section');
513
- const section = modalSectionEl.getAttribute('data-section');
514
- const selectedCategory = modalSectionEl.getAttribute('data-category');
515
- // Collect selected add-ons
516
- const selectedAddOns = Array.from(
517
- document.querySelectorAll('#addons-list input[type="checkbox"]:checked')
518
- ).map(addon => ({
519
- name: addon.getAttribute('data-name'),
520
- price: parseFloat(addon.getAttribute('data-price'))
521
- }));
522
- const instructions = document.getElementById('modal-instructions').value;
523
- if (!itemName || !itemPrice) {
524
- alert('Failed to add item to cart. Please try again.');
525
- return;
526
- }
527
- const cartPayload = {
528
- itemName: itemName,
529
- itemPrice: itemPrice,
530
- itemImage: itemImage,
531
- section: section,
532
- category: selectedCategory,
533
- addons: selectedAddOns,
534
- instructions: instructions
535
- };
536
- fetch('/cart/add', {
537
- method: 'POST',
538
- headers: {
539
- 'Content-Type': 'application/json',
540
- },
541
- body: JSON.stringify(cartPayload)
542
- })
543
- .then(response => response.json())
544
- .then(data => {
545
- if (data.success) {
546
- alert('Item added to cart successfully!');
547
- const modal = document.getElementById('itemModal');
548
- const modalInstance = bootstrap.Modal.getInstance(modal);
549
- modalInstance.hide();
550
- } else {
551
- alert(data.error || 'Failed to add item to cart.');
552
- }
553
- })
554
- .catch(err => {
555
- console.error('Error adding item to cart:', err);
556
- alert('An error occurred while adding the item to the cart.');
557
- });
 
 
558
  }
 
 
 
 
 
 
559
  // Function to round reward points to a single digit
560
  function roundRewardPoints() {
561
  let rewardPointsElement = document.getElementById('reward-points');
 
506
 
507
 
508
  function addToCartFromModal() {
509
+ const itemName = document.getElementById('modal-name').innerText;
510
+ const itemPrice = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
511
+ const itemImage = document.getElementById('modal-img').src;
512
+ const modalSectionEl = document.getElementById('modal-section');
513
+ const section = modalSectionEl.getAttribute('data-section');
514
+ const selectedCategory = modalSectionEl.getAttribute('data-category');
515
+
516
+ // Collect selected add-ons
517
+ const selectedAddOns = Array.from(
518
+ document.querySelectorAll('#addons-list input[type="checkbox"]:checked')
519
+ ).map(addon => ({
520
+ name: addon.getAttribute('data-name'),
521
+ price: parseFloat(addon.getAttribute('data-price'))
522
+ }));
523
+
524
+ const instructions = document.getElementById('modal-instructions').value;
525
+
526
+ // If necessary information is missing, alert the user
527
+ if (!itemName || !itemPrice) {
528
+ alert('Failed to add item to cart. Please try again.');
529
+ return;
530
+ }
531
+
532
+ // Prepare data for the cart
533
+ const cartPayload = {
534
+ itemName: itemName,
535
+ itemPrice: itemPrice,
536
+ itemImage: itemImage,
537
+ section: section,
538
+ category: selectedCategory,
539
+ addons: selectedAddOns,
540
+ instructions: instructions
541
+ };
542
+
543
+ // Send the cart data to the server
544
+ fetch('/cart/add', {
545
+ method: 'POST',
546
+ headers: {
547
+ 'Content-Type': 'application/json',
548
+ },
549
+ body: JSON.stringify(cartPayload)
550
+ })
551
+ .then(response => response.json())
552
+ .then(data => {
553
+ if (data.success) {
554
+ alert('Item added to cart successfully!');
555
+ const modal = document.getElementById('itemModal');
556
+ const modalInstance = bootstrap.Modal.getInstance(modal);
557
+ modalInstance.hide();
558
+ } else {
559
+ alert(data.error || 'Failed to add item to cart.');
560
  }
561
+ })
562
+ .catch(err => {
563
+ console.error('Error adding item to cart:', err);
564
+ alert('An error occurred while adding the item to the cart.');
565
+ });
566
+ }
567
  // Function to round reward points to a single digit
568
  function roundRewardPoints() {
569
  let rewardPointsElement = document.getElementById('reward-points');