Spaces:
Sleeping
Sleeping
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Restaurant Menu</title> | |
<style> | |
body { | |
font-family: Arial, sans-serif; | |
background-color: #f8f9fa; | |
text-align: center; | |
margin: 0; | |
padding: 20px; | |
} | |
h1 { | |
color: #ff5722; | |
} | |
.menu-container { | |
display: flex; | |
flex-wrap: wrap; | |
justify-content: center; | |
margin-top: 20px; | |
} | |
.menu-item { | |
background: white; | |
padding: 15px; | |
margin: 10px; | |
border-radius: 8px; | |
box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.1); | |
width: 300px; | |
text-align: left; | |
} | |
.menu-item h3 { | |
margin: 0; | |
color: #333; | |
} | |
.menu-item p { | |
margin: 5px 0; | |
color: #555; | |
} | |
.menu-item button { | |
background-color: #ff5722; | |
color: white; | |
border: none; | |
padding: 8px; | |
cursor: pointer; | |
width: 100%; | |
border-radius: 5px; | |
} | |
.menu-item button:hover { | |
background-color: #e64a19; | |
} | |
.cart-container { | |
margin-top: 30px; | |
padding: 15px; | |
background: white; | |
border-radius: 8px; | |
width: 50%; | |
margin-left: auto; | |
margin-right: auto; | |
box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.1); | |
text-align: left; | |
} | |
.cart-container h2 { | |
text-align: center; | |
color: #ff5722; | |
} | |
.cart-item { | |
display: flex; | |
justify-content: space-between; | |
border-bottom: 1px solid #ddd; | |
padding: 5px 0; | |
} | |
.cart-total { | |
font-size: 18px; | |
font-weight: bold; | |
text-align: right; | |
margin-top: 10px; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Restaurant Menu</h1> | |
<div class="menu-container" id="menu-list"> | |
<p>Loading menu...</p> | |
</div> | |
<div class="cart-container"> | |
<h2>Your Cart</h2> | |
<div id="cart-list"> | |
<p>No items in cart.</p> | |
</div> | |
<p class="cart-total" id="cart-total">Total: $0.00</p> | |
</div> | |
<script> | |
let cart = []; | |
function fetchMenu() { | |
fetch("/menu") // Fetch from Flask API | |
.then(response => response.json()) | |
.then(data => { | |
if (data.success) { | |
let menuContainer = document.getElementById("menu-list"); | |
menuContainer.innerHTML = ""; | |
data.menu.forEach(item => { | |
let menuItem = document.createElement("div"); | |
menuItem.classList.add("menu-item"); | |
menuItem.innerHTML = ` | |
<h3>${item.name}</h3> | |
<p><strong>Category:</strong> ${item.category}</p> | |
<p><strong>Price:</strong> $${item.price.toFixed(2)}</p> | |
<p><strong>Ingredients:</strong> ${item.ingredients}</p> | |
<button onclick="addToCart('${item.name}', ${item.price})">Add to Cart</button> | |
`; | |
menuContainer.appendChild(menuItem); | |
}); | |
} else { | |
document.getElementById("menu-list").innerHTML = "<p>Error loading menu.</p>"; | |
} | |
}) | |
.catch(error => { | |
console.error("Error fetching menu:", error); | |
document.getElementById("menu-list").innerHTML = "<p>Unable to load menu.</p>"; | |
}); | |
} | |
function addToCart(name, price) { | |
cart.push({ name, price }); | |
updateCart(); | |
} | |
function updateCart() { | |
let cartContainer = document.getElementById("cart-list"); | |
let totalContainer = document.getElementById("cart-total"); | |
if (cart.length === 0) { | |
cartContainer.innerHTML = "<p>No items in cart.</p>"; | |
totalContainer.innerText = "Total: $0.00"; | |
return; | |
} | |
cartContainer.innerHTML = ""; | |
let total = 0; | |
cart.forEach((item, index) => { | |
total += item.price; | |
let cartItem = document.createElement("div"); | |
cartItem.classList.add("cart-item"); | |
cartItem.innerHTML = ` | |
<span>${item.name} - $${item.price.toFixed(2)}</span> | |
<button onclick="removeFromCart(${index})" style="background-color: red; padding: 4px; font-size: 12px;">Remove</button> | |
`; | |
cartContainer.appendChild(cartItem); | |
}); | |
totalContainer.innerText = `Total: $${total.toFixed(2)}`; | |
} | |
function removeFromCart(index) { | |
cart.splice(index, 1); | |
updateCart(); | |
} | |
window.onload = fetchMenu; | |
</script> | |
</body> | |
</html> | |