Spaces:
Runtime error
Runtime error
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Menu</title> | |
<!-- Bootstrap CSS --> | |
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> | |
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet"> | |
<style> | |
body { | |
font-family: Arial, sans-serif; | |
background-color: #fdf4e3; | |
margin: 0; | |
padding: 0; | |
display: flex; | |
flex-direction: column; | |
} | |
.container { | |
max-width: 900px; | |
} | |
.menu-card { | |
max-width: 350px; | |
border-radius: 15px; | |
overflow: hidden; | |
background-color: #fff; | |
margin: auto; | |
display: flex; | |
flex-direction: column; | |
} | |
.menu-image { | |
height: 200px; | |
width: 100%; | |
object-fit: fill; | |
border-radius: 15px 15px 0 0; | |
} | |
.card-title { | |
font-size: 1.2rem; | |
font-weight: bold; | |
margin: 10px 0; | |
} | |
.card-text { | |
font-size: 1rem; | |
color: #6c757d; | |
} | |
.addbutton .btn { | |
background-color: #28a745; | |
color: white; | |
padding: 10px 20px; | |
font-size: 16px; | |
font-weight: bold; | |
border-radius: 5px; | |
border: none; | |
transition: background-color 0.3s ease; | |
margin-left: 13px; | |
} | |
.addbutton .btn:hover { | |
background-color: #218838; | |
} | |
.customisable-text { | |
color: #28a745; | |
font-size: 12px; | |
margin-left: 5px; | |
margin-top: 1px; | |
} | |
.btn-primary { | |
font-size: 13px; | |
font-weight: bold; | |
border-radius: 5px; | |
width: 100px; | |
background-color: #0FAA39; | |
border-color: #0FAA39; | |
} | |
.btn-primary:hover { | |
background-color: #0FAA39; | |
border-color: #0FAA39; | |
} | |
.btn-primary:active, | |
.btn-primary:focus { | |
background-color: #0FAA39; | |
border-color: #ffffff; | |
box-shadow: none; | |
} | |
.view-cart-container { | |
position: fixed; | |
bottom: 20px; | |
right: 20px; | |
z-index: 999; | |
} | |
.view-cart-button { | |
background-color: #0FAA39; | |
color: #fff; | |
padding: 10px 20px; | |
border-radius: 30px; | |
font-size: 1rem; | |
font-weight: bold; | |
text-decoration: none; | |
box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1); | |
display: flex; | |
align-items: center; | |
justify-content: center; | |
} | |
.view-cart-button:hover { | |
background-color: #109835; | |
text-decoration: none; | |
} | |
.avatar-dropdown-container { | |
position: relative; | |
} | |
.avatar-icon { | |
width: 40px; | |
height: 40px; | |
border-radius: 50%; | |
background-color: #5bbfc1; | |
cursor: pointer; | |
display: flex; | |
align-items: center; | |
justify-content: center; | |
color: white; | |
font-size: 20px; | |
font-weight: bold; | |
} | |
.dropdown-menu { | |
position: absolute; | |
right: 0; | |
top: 100%; | |
background-color: #fff; | |
border-radius: 5px; | |
width: 200px; | |
box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1); | |
display: none; | |
} | |
.avatar-dropdown-container:hover .dropdown-menu { | |
display: block; | |
} | |
.avatar-dropdown-container { | |
position: absolute; | |
right: 20px; | |
top: 50%; | |
transform: translateY(-50%); | |
display: flex; | |
align-items: right; | |
justify-content: center; | |
} | |
.dropdown-menu .dropdown-item { | |
padding: 10px 15px; | |
text-decoration: none; | |
color: #333; | |
border-bottom: 1px solid #ddd; | |
display: block; | |
} | |
.dropdown-menu .dropdown-item:last-child { | |
border-bottom: none; | |
} | |
.dropdown-menu .dropdown-item:hover { | |
background-color: #f1f1f1; | |
} | |
.fixed-top-bar { | |
position: relative; | |
top: 0; | |
left: 0; | |
width: 100%; | |
height: 54px; | |
background: linear-gradient(45deg, #FFA07A, #FFB347); | |
color: white; | |
padding: 15px; | |
display: flex; | |
justify-content: space-between; | |
align-items: center; | |
z-index: 1000; | |
} | |
.search-bar-container { | |
padding: 10px; | |
position: absolute; | |
left: 20px; | |
top: 50%; | |
transform: translateY(-50%); | |
display: flex; | |
justify-content: flex-start; | |
align-items: center; | |
width: 300px; | |
} | |
.search-bar-container input { | |
width: 85%; | |
padding: 8px 10px 8px 30px; | |
font-size: 16px; | |
border-radius: 10px; | |
border: none; | |
} | |
.search-icon { | |
position: absolute; | |
left: 15px; | |
font-size: 20px; | |
color: #888; | |
} | |
.addon-section { | |
background-color: #fff; | |
border: 2px solid #6c757d; | |
border-radius: 8px; | |
padding: 12px; | |
margin-bottom: 10px; | |
} | |
.addon-section h6 { | |
margin-bottom: 10px; | |
font-size: 1.1rem; | |
font-weight: bold; | |
color: #343a40; | |
} | |
.addon-section .form-check { | |
display: inline-flex; | |
align-items: center; | |
margin-left: 10px; | |
color: #343a40; | |
} | |
.addon-section .form-check-input { | |
-webkit-appearance: none; | |
-moz-appearance: none; | |
appearance: none; | |
width: 20px; | |
height: 20px; | |
border: 2px solid #343a40; | |
border-radius: 5px; | |
background-color: #f0f0f0; | |
position: relative; | |
margin-right: 10px; | |
} | |
.addon-section .form-check-input:checked { | |
background-color: #006400; | |
border-color: #006400; | |
} | |
.addon-section .form-check-input:checked::before { | |
content: ''; | |
font-size: 14px; | |
position: absolute; | |
top: 3px; | |
left: 4px; | |
color: white; | |
} | |
.addon-section .form-check-label { | |
font-size: 16px; | |
margin-left: 5px; | |
cursor: pointer; | |
display: inline-block; | |
vertical-align: middle; | |
} | |
form.text-center.mb-4 { | |
display: flex; | |
flex-direction: column; | |
align-items: center; | |
justify-content: center; | |
margin-bottom: 5px; | |
} | |
.form-check { | |
display: inline-block; | |
margin-right: 5px; | |
margin-bottom: 0; | |
margin-top: 10px; | |
vertical-align: middle; | |
} | |
.form-check-inline { | |
display: inline-block; | |
margin-right: 5px; | |
} | |
.form-check-label { | |
display: inline-block; | |
font-size: 16px; | |
margin-left: 5px; | |
vertical-align: middle; | |
} | |
.custom-radio { | |
appearance: none; | |
-webkit-appearance: none; | |
-moz-appearance: none; | |
width: 20px; | |
height: 20px; | |
border: 3px solid #4CAF50; | |
border-radius: 50%; | |
margin-right: -5px; | |
outline: none; | |
cursor: pointer; | |
position: relative; | |
display: inline-block; | |
vertical-align: middle; | |
} | |
.custom-radio:checked { | |
background-color: #4CAF50; | |
border-color: #4CAF50; | |
} | |
.custom-radio:checked::after { | |
content: ''; | |
position: relative; | |
top: 5px; | |
left: 5px; | |
border-radius: 50%; | |
} | |
.custom-radio:hover { | |
border-color: #388E3C; | |
} | |
.modal-footer { | |
display: flex; | |
align-items: center; | |
justify-content: space-between; | |
padding: 10px; | |
} | |
.modal-footer .d-flex { | |
display: flex; | |
align-items: center; | |
gap: 10px; | |
} | |
.modal-footer .btn { | |
height: 40px; | |
padding: 0 15px; | |
} | |
.modal-footer .form-control { | |
width: 50px; | |
height: 40px; | |
text-align: center; | |
} | |
.modal-footer .btn-primary { | |
background-color: #0FAA39; | |
border-color: #0FAA39; | |
font-weight: bold; | |
padding: 10px 20px; | |
height: 40px; | |
display: flex; | |
justify-content: center; | |
align-items: center; | |
width: auto; | |
} | |
.modal-footer .btn-outline-secondary { | |
height: 40px; | |
width: 40px; | |
} | |
@media (max-width: 576px) { | |
.modal-dialog { | |
max-width: 98%; | |
} | |
.modal-footer .btn { | |
height: 35px; | |
padding: 0 12px; | |
} | |
.modal-footer .form-control { | |
width: 40px; | |
height: 35px; | |
} | |
} | |
</style> | |
</head> | |
<body> | |
<div class="fixed-top-bar"> | |
<div class="avatar-dropdown-container"> | |
<div class="avatar-icon"> | |
<span>{{ first_letter }}</span> | |
</div> | |
<div class="dropdown-menu"> | |
<a href="{{ url_for('user_details.customer_details') }}" class="dropdown-item">View Profile</a> | |
<a href="{{ url_for('orderhistory.order_history') }}" class="dropdown-item">Order History</a> | |
<a href="{{ url_for('logout') }}" class="dropdown-item">Logout</a> | |
</div> | |
</div> | |
<div class="search-bar-container"> | |
<input type="text" id="searchBar" class="form-control" placeholder="Search items or sections..." onkeyup="filterMenu()"> | |
<i class="bi bi-search search-icon"></i> | |
</div> | |
</div> | |
<form method="get" action="/menu" class="text-center mb-4"> | |
<label class="form-label fw-bold">Select a Category:</label> | |
<div class="form-check form-check-inline"> | |
{% for category in categories %} | |
<input type="radio" id="category-{{ category }}" name="category" value="{{ category }}" class="custom-radio" | |
{% if selected_category == category %}checked{% endif %} onchange="this.form.submit()"> | |
<label class="form-check-label" for="category-{{ category }}">{{ category }}</label> | |
{% endfor %} | |
</div> | |
<div class="form-check"> | |
<input type="radio" id="category-CustomizedDish" name="category" value="Customized Dish" class="custom-radio" | |
{% if selected_category == "Customized Dish" %}checked{% endif %} onchange="this.form.submit()"> | |
<label class="form-check-label" for="category-CustomizedDish">Customized Dish</label> | |
</div> | |
</form> | |
<div class="container mt-4"> | |
{% if selected_category == "Customized Dish" %} | |
<div id="custom-dish-form" class="mt-4"> | |
<h3>Create Your Custom Dish</h3> | |
<form method="POST" action="/customdish/generate_custom_dish"> | |
<div class="mb-3"> | |
<label for="custom-dish-name" class="form-label">Dish Name</label> | |
<input type="text" class="form-control" id="custom-dish-name" name="name" required> | |
</div> | |
<div class="mb-3"> | |
<label for="custom-dish-description" class="form-label">Dish Description</label> | |
<textarea class="form-control" id="custom-dish-description" name="description" required></textarea> | |
</div> | |
<button type="submit" class="btn btn-primary">Submit</button> | |
</form> | |
</div> | |
{% else %} | |
{% for section, items in ordered_menu.items() %} | |
<h3>{{ section }}</h3> | |
<div class="row"> | |
{% for item in items %} | |
<div class="col-md-6 mb-4"> | |
<div class="card menu-card"> | |
<img src="{{ item.Image1__c }}" class="card-img-top menu-image" alt="{{ item.Name }}" | |
onerror="this.src='/static/placeholder.jpg';" loading="lazy"> | |
<div class="addbutton"> | |
<div class="card-body d-flex align-items-center justify-content-between"> | |
<div> | |
<h5 class="card-title">{{ item.Name }}</h5> | |
<p class="card-text">${{ item.Price__c }}</p> | |
</div> | |
<div class="d-flex flex-column align-item-center justify-content-center"> | |
<div> | |
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#itemModal" | |
onclick="showItemDetails('{{ item.Name }}', '{{ item.Price__c }}', '{{ item.Image2__c }}', '{{ item.Description__c }}', '{{ item.Section__c }}','{{ selected_category }}')"> | |
ADD | |
</button> | |
</div> | |
<div class="w-100 text-center"> | |
{% if item.Section__c != 'Apetizer' and item.Section__c != 'Customized dish' and item.Section__c !='Soft Drinks' %} | |
<h5 class="customisable-text">Customisable</h5> | |
{% endif %} | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
{% endfor %} | |
</div> | |
{% endfor %} | |
{% endif %} | |
</div> | |
<div class="view-cart-container"> | |
<a href="{{ url_for('cart.cart') }}" class="view-cart-button"> | |
View Cart | |
</a> | |
</div> | |
<div class="modal fade" id="itemModal" tabindex="-1" aria-labelledby="itemModalLabel" aria-hidden="true"> | |
<div class="modal-dialog modal-dialog-centered"> | |
<div class="modal-content"> | |
<div class="modal-header"> | |
<h5 class="modal-title" id="itemModalLabel">Item Details</h5> | |
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> | |
</div> | |
<div class="modal-body"> | |
<img id="modal-img" class="img-fluid rounded mb-3 d-block mx-auto" alt="Item Image" style="max-height: 200px; object-fit: cover;"> | |
<h5 id="modal-name" class="fw-bold text-center"></h5> | |
<p id="modal-price" class="text-muted text-center"></p> | |
<p id="modal-description" class="text-secondary"></p> | |
<div id="first-row"> | |
<h6 id="first-row-title" style="display: none;">Customization Options</h6> | |
<div class="addon-section"> | |
<h6>Type</h6> | |
<div id="type-options"></div> | |
</div> | |
<div class="addon-section"> | |
<h6>Spice Level</h6> | |
<div id="spice-level-options"></div> | |
</div> | |
</div> | |
<div id="modal-addons" class="modal-addons mt-4"> | |
<h6 id="addons-title">Customization Options</h6> | |
<div id="addons-list" class="addons-container">Loading customization options...</div> | |
</div> | |
<div class="mt-4"> | |
<h6>Custom Request</h6> | |
<textarea id="modal-instructions" class="form-control" placeholder="Enter any special instructions here..."></textarea> | |
</div> | |
<span id="modal-section" data-section="" data-category="" style="display: none;"></span> | |
</div> | |
<div class="modal-footer d-flex align-items-center justify-content-between"> | |
<div class="d-flex align-items-center gap-2"> | |
<button type="button" class="btn btn-outline-secondary" id="decreaseQuantity">-</button> | |
<input type="text" class="form-control text-center" id="quantityInput" value="1" readonly style="width: 50px;"/> | |
<button type="button" class="btn btn-outline-secondary" id="increaseQuantity">+</button> | |
</div> | |
<button type="button" class="btn btn-primary" onclick="addToCartFromModal()">Add to Cart</button> | |
</div> | |
</div> | |
</div> | |
</div> | |
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> | |
<script> | |
function showItemDetails(name, price, image, description, section, selectedCategory) { | |
document.getElementById('modal-name').innerText = name; | |
document.getElementById('modal-price').innerText = `$${price}`; | |
document.getElementById('modal-img').src = image || '/static/placeholder.jpg'; | |
document.getElementById('modal-description').innerText = description || 'No description available.'; | |
document.getElementById('addons-list').innerHTML = 'Loading customization options...'; | |
document.getElementById('modal-instructions').value = ''; | |
const modalSectionEl = document.getElementById('modal-section'); | |
modalSectionEl.setAttribute('data-section', section); | |
modalSectionEl.setAttribute('data-category', selectedCategory); | |
document.getElementById('quantityInput').value = 1; | |
const typeOptions = document.getElementById('type-options'); | |
const spiceLevelOptions = document.getElementById('spice-level-options'); | |
const firstRow = document.getElementById('first-row'); | |
const firstRowTitle = document.getElementById('first-row-title'); | |
const addonsTitle = document.getElementById('addons-title'); | |
typeOptions.innerHTML = ''; | |
spiceLevelOptions.innerHTML = ''; | |
if (section.toLowerCase() === 'starters') { | |
firstRow.style.display = 'block'; | |
firstRowTitle.style.display = 'block'; | |
addonsTitle.style.display = 'none'; | |
} else { | |
firstRow.style.display = 'none'; | |
firstRowTitle.style.display = 'none'; | |
addonsTitle.style.display = 'block'; | |
} | |
fetch(`/api/addons?item_name=${encodeURIComponent(name)}&item_section=${encodeURIComponent(section)}`) | |
.then(response => response.json()) | |
.then(data => { | |
const addonsList = document.getElementById('addons-list'); | |
addonsList.innerHTML = ''; | |
if (!data.success || !data.addons || data.addons.length === 0) { | |
addonsList.innerHTML = '<p>No customization options available.</p>'; | |
addonsTitle.style.display = 'none'; | |
return; | |
} | |
if (section.toLowerCase() === 'starters') { | |
data.addons.forEach(addon => { | |
if (addon.name.toLowerCase() === "type") { | |
addon.options.forEach(option => { | |
const optionId = `addon-type-${option}`; | |
const optionHTML = ` | |
<div class="form-check"> | |
<input type="checkbox" class="form-check-input" id="${optionId}" value="${option}" data-name="${option}" data-group="Type"> | |
<label class="form-check-label" for="${optionId}"> | |
${option} | |
</label> | |
</div> | |
`; | |
typeOptions.innerHTML += optionHTML; | |
}); | |
} | |
if (addon.name.toLowerCase() === "spice level") { | |
addon.options.forEach(option => { | |
const optionId = `addon-spice-level-${option}`; | |
const optionHTML = ` | |
<div class="form-check"> | |
<input type="checkbox" class="form-check-input" id="${optionId}" value="${option}" data-name="${option}" data-group="Spice Level"> | |
<label class="form-check-label" for="${optionId}"> | |
${option} | |
</label> | |
</div> | |
`; | |
spiceLevelOptions.innerHTML += optionHTML; | |
}); | |
} | |
}); | |
} | |
data.addons.forEach(addon => { | |
if (section.toLowerCase() === 'starters' && | |
(addon.name.toLowerCase() === "type" || addon.name.toLowerCase() === "spice level")) { | |
return; | |
} | |
const sectionDiv = document.createElement('div'); | |
sectionDiv.classList.add('addon-section'); | |
sectionDiv.setAttribute('data-addon-name', addon.name); | |
const title = document.createElement('h6'); | |
title.innerText = addon.name; | |
sectionDiv.appendChild(title); | |
const optionsContainer = document.createElement('div'); | |
addon.options.forEach((option, index) => { | |
const optionId = `addon-${addon.name.replace(/\s+/g, '')}-${index}`; | |
const listItem = document.createElement('div'); | |
listItem.classList.add('form-check'); | |
listItem.innerHTML = ` | |
<input type="checkbox" class="form-check-input addon-option" id="${optionId}" value="${option}" | |
data-name="${option}" data-group="${addon.name}" data-price="${addon.extra_charge ? addon.extra_charge_amount : 0}"> | |
<label class="form-check-label" for="${optionId}"> | |
${option} ${addon.extra_charge ? `($${addon.extra_charge_amount})` : ''} | |
</label> | |
`; | |
optionsContainer.appendChild(listItem); | |
}); | |
sectionDiv.appendChild(optionsContainer); | |
addonsList.appendChild(sectionDiv); | |
}); | |
const startersOrder = [ | |
"Choose Preparation Style", | |
"Select Dip/Sauce", | |
"Extra Add-ons", | |
"Make it a Combo" | |
]; | |
const desiredOrder = [ | |
"Spice Level", | |
"Choose Preparation Style", | |
"Select Dip/Sauce", | |
"Extra Add-ons", | |
"Make it a Combo", | |
"Type" | |
]; | |
const orderToUse = section.toLowerCase() === 'starters' ? startersOrder : desiredOrder; | |
const sections = Array.from(addonsList.children); | |
addonsList.innerHTML = ''; | |
orderToUse.forEach(sectionName => { | |
const section = sections.find(s => s.getAttribute('data-addon-name') === sectionName); | |
if (section) { | |
addonsList.appendChild(section); | |
} | |
}); | |
sections.forEach(section => { | |
if (!orderToUse.includes(section.getAttribute('data-addon-name'))) { | |
addonsList.appendChild(section); | |
} | |
}); | |
}) | |
.catch(err => { | |
console.error('Error fetching add-ons:', err); | |
document.getElementById('addons-list').innerHTML = '<p>Error loading customization options.</p>'; | |
addonsTitle.style.display = 'none'; | |
}); | |
} | |
document.addEventListener('click', function(event) { | |
if (event.target.classList.contains('addon-option')) { | |
handleAddonClick(event.target); | |
} | |
}); | |
function handleAddonClick(checkbox) { | |
const groupName = checkbox.getAttribute('data-group'); | |
const isMultiSelectGroup = ["Extra Toppings", "Choose Raita/Sides", "Select Dip/Sauce", "Extra Add-ons", "Make it a Combo", "Beverages", "Sauces"].includes(groupName); | |
if (!isMultiSelectGroup) { | |
const checkboxes = document.querySelectorAll(`.addon-option[data-group="${groupName}"]`); | |
checkboxes.forEach(otherCheckbox => { | |
if (otherCheckbox !== checkbox) { | |
otherCheckbox.checked = false; | |
} | |
}); | |
} | |
} | |
function filterMenu() { | |
let input = document.getElementById('searchBar').value.toLowerCase(); | |
let sections = document.querySelectorAll('h3'); | |
let items = document.querySelectorAll('.menu-card'); | |
let matchedSections = new Set(); | |
items.forEach(item => { | |
let itemName = item.querySelector('.card-title').innerText.toLowerCase(); | |
let itemSection = item.closest('.row').previousElementSibling.innerText.toLowerCase(); | |
if (itemName.includes(input) || (itemSection && itemSection.includes(input))) { | |
item.style.display = 'block'; | |
matchedSections.add(item.closest('.row')); | |
} else { | |
item.style.display = 'none'; | |
} | |
}); | |
sections.forEach(section => { | |
let sectionRow = section.nextElementSibling; | |
if (matchedSections.has(sectionRow)) { | |
section.style.display = 'block'; | |
sectionRow.style.display = 'flex'; | |
} else { | |
section.style.display = 'none'; | |
sectionRow.style.display = 'none'; | |
} | |
}); | |
} | |
function addToCartFromModal() { | |
const itemName = document.getElementById('modal-name').innerText; | |
let itemPrice = parseFloat(document.getElementById('modal-price').innerText.replace('$', '')); | |
if (isNaN(itemPrice)) { | |
alert('Invalid price for the item. Please check the item details.'); | |
return; | |
} | |
const itemImage = document.getElementById('modal-img').src; | |
const modalSectionEl = document.getElementById('modal-section'); | |
const section = modalSectionEl.getAttribute('data-section'); | |
const selectedCategory = modalSectionEl.getAttribute('data-category'); | |
if (!itemName || !itemPrice || !section || !itemImage) { | |
console.error('Missing data for cart item:', { itemName, itemPrice, section, itemImage}); | |
return; | |
} | |
let selectedAddOns = Array.from( | |
document.querySelectorAll('#addons-list input[type="checkbox"]:checked') | |
).map(addon => ({ | |
name: addon.getAttribute('data-name') || 'Default Name', | |
price: parseFloat(addon.getAttribute('data-price') || 0) | |
})); | |
if (section.toLowerCase() === 'starters') { | |
const typeOptions = Array.from( | |
document.querySelectorAll('#type-options input[type="checkbox"]:checked') | |
).map(option => ({ | |
name: option.getAttribute('data-name') || 'Default Type', | |
price: 0 | |
})); | |
const spiceLevelOptions = Array.from( | |
document.querySelectorAll('#spice-level-options input[type="checkbox"]:checked') | |
).map(option => ({ | |
name: option.getAttribute('data-name') || 'Default Spice Level', | |
price: 0 | |
})); | |
selectedAddOns = [...selectedAddOns, ...typeOptions, ...spiceLevelOptions]; | |
} | |
const quantity = parseInt(document.getElementById('quantityInput').value) || 1; | |
const instructions = document.getElementById('modal-instructions').value; | |
const cartPayload = { | |
itemName: itemName, | |
itemPrice: itemPrice, | |
itemImage: itemImage, | |
section: section, | |
category: selectedCategory, | |
addons: selectedAddOns, | |
instructions: instructions, | |
quantity: quantity | |
}; | |
fetch('/cart/add', { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
}, | |
body: JSON.stringify(cartPayload) | |
}) | |
.then(response => response.json()) | |
.then(data => { | |
if (data.success) { | |
alert('Item added to cart successfully!'); | |
updateCartUI(data.cart); | |
const modal = document.getElementById('itemModal'); | |
const modalInstance = bootstrap.Modal.getInstance(modal); | |
modalInstance.hide(); | |
} else { | |
alert(data.error || 'Failed to add item to cart.'); | |
} | |
}) | |
.catch(err => { | |
console.error('Error adding item to cart:', err); | |
alert('An error occurred while adding the item to the cart.'); | |
}); | |
} | |
function updateCartUI(cart) { | |
if (!Array.isArray(cart)) { | |
console.error('Invalid cart data:', cart); | |
return; | |
} | |
const cartIcon = document.getElementById('cart-icon'); | |
if (cartIcon) { | |
cartIcon.innerText = cart.length; | |
} | |
} | |
function updateCartDisplay(cart) { | |
if (!Array.isArray(cart)) { | |
console.error('Invalid cart data:', cart); | |
return; | |
} | |
const cartCountElement = document.getElementById('cart-count'); | |
if (cartCountElement) { | |
cartCountElement.innerText = cart.reduce((total, item) => total + item.quantity, 0); | |
} | |
const successNotification = document.createElement('div'); | |
successNotification.classList.add('success-notification'); | |
successNotification.innerText = 'Item added to cart!'; | |
document.body.appendChild(successNotification); | |
setTimeout(() => { | |
successNotification.remove(); | |
}, 2000); | |
} | |
document.addEventListener('DOMContentLoaded', function () { | |
const decreaseBtn = document.getElementById('decreaseQuantity'); | |
const increaseBtn = document.getElementById('increaseQuantity'); | |
const quantityInput = document.getElementById('quantityInput'); | |
decreaseBtn.addEventListener('click', function () { | |
let currentQuantity = parseInt(quantityInput.value); | |
if (currentQuantity > 1) { | |
currentQuantity--; | |
quantityInput.value = currentQuantity; | |
} | |
}); | |
increaseBtn.addEventListener('click', function () { | |
let currentQuantity = parseInt(quantityInput.value); | |
currentQuantity++; | |
quantityInput.value = currentQuantity; | |
}); | |
}); | |
</script> | |
</body> | |
</html> |