nagasurendra commited on
Commit
3bfb127
·
verified ·
1 Parent(s): 731301f

Update templates/menu.html

Browse files
Files changed (1) hide show
  1. templates/menu.html +73 -47
templates/menu.html CHANGED
@@ -299,9 +299,10 @@
299
  <p id="modal-description" class="text-secondary"></p>
300
  <!-- Add-ons -->
301
  <div id="modal-addons" class="modal-addons mt-4">
302
- <h6>Add-ons</h6>
303
- <div id="addons-list" class="addons-container">Loading add-ons...</div>
304
  </div>
 
305
  <div class="mt-4">
306
  <h6>Special Instructions</h6>
307
  <textarea id="modal-instructions" class="form-control" placeholder="Enter any special instructions here..."></textarea>
@@ -320,43 +321,68 @@
320
  <!-- JavaScript -->
321
  <script>
322
  function showItemDetails(name, price, image, description, section, selectedCategory) {
323
- // Set modal content dynamically
324
  document.getElementById('modal-name').innerText = name;
325
- document.getElementById('modal-price').innerText = `$${price}`;
326
  document.getElementById('modal-img').src = image || '/static/placeholder.jpg';
327
  document.getElementById('modal-description').innerText = description || 'No description available.';
328
- document.getElementById('addons-list').innerHTML = 'Loading add-ons...';
329
  document.getElementById('modal-instructions').value = '';
330
- // Correctly set data attributes for section and category
 
331
  const modalSectionEl = document.getElementById('modal-section');
332
  modalSectionEl.setAttribute('data-section', section);
333
  modalSectionEl.setAttribute('data-category', selectedCategory);
334
- // Fetch add-ons dynamically based on item Name
335
- fetch(`/api/addons?item_name=${encodeURIComponent(name)}`)
 
336
  .then(response => response.json())
337
  .then(data => {
338
  const addonsList = document.getElementById('addons-list');
339
  addonsList.innerHTML = ''; // Clear previous content
 
340
  if (!data.success || !data.addons || data.addons.length === 0) {
341
- addonsList.innerHTML = 'No add-ons available.';
342
  return;
343
  }
344
- // Populate the add-ons list
 
345
  data.addons.forEach(addon => {
346
- const listItem = document.createElement('div');
347
- listItem.innerHTML = `
348
- <input type="checkbox" id="addon-${addon.Id}" value="${addon.Id}"
349
- data-name="${addon.Name}" data-price="${addon.Price__c}">
350
- <label for="addon-${addon.Id}">${addon.Name} - $${addon.Price__c}</label>
351
- `;
352
- addonsList.appendChild(listItem);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
353
  });
354
  })
355
  .catch(err => {
356
  console.error('Error fetching add-ons:', err);
357
- document.getElementById('addons-list').innerHTML = 'Unable to fetch add-ons. Please try again later.';
358
  });
359
  }
 
360
  function filterMenu() {
361
  let input = document.getElementById('searchBar').value.toLowerCase();
362
  let sections = document.querySelectorAll('.menu-section'); // Select all sections
@@ -388,37 +414,38 @@
388
  }
389
 
390
  function addToCartFromModal() {
391
- const itemName = document.getElementById('modal-name').innerText; // Get item name
392
- const itemPrice = parseFloat(document.getElementById('modal-price').innerText.replace('$', '')); // Get item price
393
- const itemImage = document.getElementById('modal-img').src; // Get item image
394
  const modalSectionEl = document.getElementById('modal-section');
395
  const section = modalSectionEl.getAttribute('data-section');
396
  const selectedCategory = modalSectionEl.getAttribute('data-category');
 
 
397
  const selectedAddOns = Array.from(
398
  document.querySelectorAll('#addons-list input[type="checkbox"]:checked')
399
  ).map(addon => ({
400
- name: addon.getAttribute('data-name'), // Add-on name
401
- price: parseFloat(addon.getAttribute('data-price')) // Add-on price
402
  }));
 
403
  const instructions = document.getElementById('modal-instructions').value;
404
- // Ensure itemName and itemPrice are present
405
  if (!itemName || !itemPrice) {
406
  alert('Failed to add item to cart. Please try again.');
407
  return;
408
  }
409
-
410
- // Create the payload to send to the backend
411
  const cartPayload = {
412
- itemName: itemName, // Food item name
413
  itemPrice: itemPrice,
414
- itemImage: itemImage, // Image URL
415
  section: section,
416
  category: selectedCategory,
417
  addons: selectedAddOns,
418
  instructions: instructions
419
  };
420
-
421
- // Send the payload to the server
422
  fetch('/cart/add', {
423
  method: 'POST',
424
  headers: {
@@ -426,24 +453,23 @@
426
  },
427
  body: JSON.stringify(cartPayload)
428
  })
429
- .then(response => response.json())
430
- .then(data => {
431
- if (data.success) {
432
- alert('Item added to cart successfully!');
433
- const modal = document.getElementById('itemModal');
434
- const modalInstance = bootstrap.Modal.getInstance(modal); // Get the Bootstrap modal instance
435
- modalInstance.hide(); // Hide the modal
436
- // window.location.href = '/menu'; // Redirect to the cart page
437
- } else {
438
- console.error('Error adding item to cart:', data.error);
439
- alert(data.error || 'Failed to add item to cart.');
440
- }
441
- })
442
- .catch(err => {
443
- console.error('Error adding item to cart:', err);
444
- alert('An error occurred while adding the item to the cart.');
445
- });
446
  }
 
447
  // Function to round reward points to a single digit
448
  function roundRewardPoints() {
449
  let rewardPointsElement = document.getElementById('reward-points');
 
299
  <p id="modal-description" class="text-secondary"></p>
300
  <!-- Add-ons -->
301
  <div id="modal-addons" class="modal-addons mt-4">
302
+ <h6>Customization Options</h6>
303
+ <div id="addons-list" class="addons-container">Loading customization options...</div>
304
  </div>
305
+
306
  <div class="mt-4">
307
  <h6>Special Instructions</h6>
308
  <textarea id="modal-instructions" class="form-control" placeholder="Enter any special instructions here..."></textarea>
 
321
  <!-- JavaScript -->
322
  <script>
323
  function showItemDetails(name, price, image, description, section, selectedCategory) {
 
324
  document.getElementById('modal-name').innerText = name;
325
+ document.getElementById('modal-price').innerText = `₹${price}`;
326
  document.getElementById('modal-img').src = image || '/static/placeholder.jpg';
327
  document.getElementById('modal-description').innerText = description || 'No description available.';
328
+ document.getElementById('addons-list').innerHTML = 'Loading customization options...';
329
  document.getElementById('modal-instructions').value = '';
330
+
331
+ // Set section and category for reference
332
  const modalSectionEl = document.getElementById('modal-section');
333
  modalSectionEl.setAttribute('data-section', section);
334
  modalSectionEl.setAttribute('data-category', selectedCategory);
335
+
336
+ // Fetch customization options based on the section
337
+ fetch(`/api/addons?item_name=${encodeURIComponent(name)}&item_section=${encodeURIComponent(section)}`)
338
  .then(response => response.json())
339
  .then(data => {
340
  const addonsList = document.getElementById('addons-list');
341
  addonsList.innerHTML = ''; // Clear previous content
342
+
343
  if (!data.success || !data.addons || data.addons.length === 0) {
344
+ addonsList.innerHTML = '<p>No customization options available.</p>';
345
  return;
346
  }
347
+
348
+ // Display customization options dynamically
349
  data.addons.forEach(addon => {
350
+ const sectionDiv = document.createElement('div');
351
+ sectionDiv.classList.add('addon-section');
352
+
353
+ // Add section title
354
+ const title = document.createElement('h6');
355
+ title.innerText = addon.name;
356
+ sectionDiv.appendChild(title);
357
+
358
+ // Create options list
359
+ const optionsContainer = document.createElement('div');
360
+
361
+ addon.options.forEach((option, index) => {
362
+ const optionId = `addon-${addon.name.replace(/\s+/g, '')}-${index}`;
363
+ const listItem = document.createElement('div');
364
+ listItem.classList.add('form-check');
365
+
366
+ listItem.innerHTML = `
367
+ <input type="checkbox" class="form-check-input addon-option" id="${optionId}" value="${option}"
368
+ data-name="${option}" data-price="${addon.extra_charge ? addon.extra_charge_amount : 0}">
369
+ <label class="form-check-label" for="${optionId}">
370
+ ${option} ${addon.extra_charge ? `(₹${addon.extra_charge_amount})` : ''}
371
+ </label>
372
+ `;
373
+ optionsContainer.appendChild(listItem);
374
+ });
375
+
376
+ sectionDiv.appendChild(optionsContainer);
377
+ addonsList.appendChild(sectionDiv);
378
  });
379
  })
380
  .catch(err => {
381
  console.error('Error fetching add-ons:', err);
382
+ document.getElementById('addons-list').innerHTML = '<p>Error loading customization options.</p>';
383
  });
384
  }
385
+
386
  function filterMenu() {
387
  let input = document.getElementById('searchBar').value.toLowerCase();
388
  let sections = document.querySelectorAll('.menu-section'); // Select all sections
 
414
  }
415
 
416
  function addToCartFromModal() {
417
+ const itemName = document.getElementById('modal-name').innerText;
418
+ const itemPrice = parseFloat(document.getElementById('modal-price').innerText.replace('', ''));
419
+ const itemImage = document.getElementById('modal-img').src;
420
  const modalSectionEl = document.getElementById('modal-section');
421
  const section = modalSectionEl.getAttribute('data-section');
422
  const selectedCategory = modalSectionEl.getAttribute('data-category');
423
+
424
+ // Collect selected add-ons
425
  const selectedAddOns = Array.from(
426
  document.querySelectorAll('#addons-list input[type="checkbox"]:checked')
427
  ).map(addon => ({
428
+ name: addon.getAttribute('data-name'),
429
+ price: parseFloat(addon.getAttribute('data-price'))
430
  }));
431
+
432
  const instructions = document.getElementById('modal-instructions').value;
433
+
434
  if (!itemName || !itemPrice) {
435
  alert('Failed to add item to cart. Please try again.');
436
  return;
437
  }
438
+
 
439
  const cartPayload = {
440
+ itemName: itemName,
441
  itemPrice: itemPrice,
442
+ itemImage: itemImage,
443
  section: section,
444
  category: selectedCategory,
445
  addons: selectedAddOns,
446
  instructions: instructions
447
  };
448
+
 
449
  fetch('/cart/add', {
450
  method: 'POST',
451
  headers: {
 
453
  },
454
  body: JSON.stringify(cartPayload)
455
  })
456
+ .then(response => response.json())
457
+ .then(data => {
458
+ if (data.success) {
459
+ alert('Item added to cart successfully!');
460
+ const modal = document.getElementById('itemModal');
461
+ const modalInstance = bootstrap.Modal.getInstance(modal);
462
+ modalInstance.hide();
463
+ } else {
464
+ alert(data.error || 'Failed to add item to cart.');
465
+ }
466
+ })
467
+ .catch(err => {
468
+ console.error('Error adding item to cart:', err);
469
+ alert('An error occurred while adding the item to the cart.');
470
+ });
 
 
471
  }
472
+
473
  // Function to round reward points to a single digit
474
  function roundRewardPoints() {
475
  let rewardPointsElement = document.getElementById('reward-points');