Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -112,7 +112,7 @@ def filter_menu(preference):
|
|
| 112 |
|
| 113 |
return html_content
|
| 114 |
|
| 115 |
-
# Create Modal Window
|
| 116 |
def create_modal_window():
|
| 117 |
add_ons = load_add_ons_from_salesforce()
|
| 118 |
add_ons_html = ""
|
|
@@ -152,6 +152,7 @@ def modal_js():
|
|
| 152 |
<script>
|
| 153 |
let cart = [];
|
| 154 |
let totalCartCost = 0;
|
|
|
|
| 155 |
function openModal(name, image2, description, price) {
|
| 156 |
const modal = document.getElementById('modal');
|
| 157 |
modal.style.display = 'block';
|
|
@@ -166,57 +167,68 @@ def modal_js():
|
|
| 166 |
document.getElementById('modal-price').innerText = price;
|
| 167 |
document.getElementById('quantity').value = 1;
|
| 168 |
document.getElementById('special-instructions').value = '';
|
|
|
|
| 169 |
resetAddOns(); // Reset add-ons when opening the modal
|
| 170 |
}
|
|
|
|
| 171 |
function closeModal() {
|
| 172 |
document.getElementById('modal').style.display = 'none';
|
| 173 |
}
|
|
|
|
| 174 |
function addToCart() {
|
| 175 |
const name = document.getElementById('modal-name').innerText;
|
| 176 |
const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
|
| 177 |
const quantity = parseInt(document.getElementById('quantity').value) || 1;
|
| 178 |
const instructions = document.getElementById('special-instructions').value;
|
|
|
|
| 179 |
const selectedAddOns = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked'));
|
| 180 |
const extras = selectedAddOns.map(extra => ({
|
| 181 |
name: extra.value,
|
| 182 |
price: parseFloat(extra.getAttribute('data-price')),
|
| 183 |
quantity: 1 // Default quantity for add-ons is 1
|
| 184 |
}));
|
|
|
|
| 185 |
const extrasCost = extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
|
| 186 |
const totalCost = (price * quantity) + extrasCost;
|
|
|
|
| 187 |
// Add the item to the cart with its specific add-ons
|
| 188 |
cart.push({ name, price, quantity, extras, instructions, totalCost });
|
| 189 |
totalCartCost += totalCost; // Update the total cost of the cart
|
|
|
|
| 190 |
updateCartButton();
|
| 191 |
updateCartTotalCost(); // Update total cost displayed
|
| 192 |
closeModal();
|
| 193 |
}
|
|
|
|
| 194 |
function updateCartButton() {
|
| 195 |
const cartButton = document.getElementById('cart-button');
|
| 196 |
cartButton.innerText = `View Cart (${cart.length} items)`;
|
| 197 |
}
|
|
|
|
| 198 |
function openCartModal() {
|
| 199 |
const cartModal = document.getElementById('cart-modal');
|
| 200 |
const cartItemsContainer = document.getElementById('cart-items');
|
| 201 |
cartItemsContainer.innerHTML = "";
|
| 202 |
cart.forEach((item, index) => {
|
| 203 |
-
const extrasList = item.extras.map(extra => `${extra.name}
|
| 204 |
cartItemsContainer.innerHTML += `
|
| 205 |
<div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 10px; border-radius: 8px;">
|
| 206 |
<h3>${item.name}</h3>
|
| 207 |
-
<p>Quantity:
|
| 208 |
<p>Extras: ${extrasList || 'None'}</p>
|
| 209 |
<p>Special Instructions: ${item.instructions || 'None'}</p>
|
| 210 |
-
<p>Total Cost:
|
| 211 |
<button onclick="removeFromCart(${index})" style="color: red;">Remove</button>
|
| 212 |
</div>
|
| 213 |
`;
|
| 214 |
});
|
| 215 |
cartModal.style.display = 'block';
|
| 216 |
}
|
|
|
|
| 217 |
function closeCartModal() {
|
| 218 |
document.getElementById('cart-modal').style.display = 'none';
|
| 219 |
}
|
|
|
|
| 220 |
function removeFromCart(index) {
|
| 221 |
totalCartCost -= cart[index].totalCost; // Deduct the cost of the removed item from total cost
|
| 222 |
cart.splice(index, 1);
|
|
@@ -224,32 +236,85 @@ def modal_js():
|
|
| 224 |
updateCartTotalCost(); // Update total cost displayed
|
| 225 |
openCartModal();
|
| 226 |
}
|
| 227 |
-
|
| 228 |
-
if (type === 'item') {
|
| 229 |
-
cart[index].quantity = parseInt(value);
|
| 230 |
-
} else if (type === 'extra') {
|
| 231 |
-
cart[index].extras[0].quantity = parseInt(value); // Assuming one add-on for simplicity
|
| 232 |
-
}
|
| 233 |
-
const item = cart[index];
|
| 234 |
-
const price = item.price;
|
| 235 |
-
const extrasCost = item.extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
|
| 236 |
-
item.totalCost = (price * item.quantity) + extrasCost;
|
| 237 |
-
document.getElementById(`item-${index}-total`).innerText = item.totalCost.toFixed(2);
|
| 238 |
-
updateCartTotalCost(); // Update total cost displayed
|
| 239 |
-
}
|
| 240 |
function updateCartTotalCost() {
|
| 241 |
const totalCostElement = document.getElementById('cart-total-cost');
|
| 242 |
totalCartCost = cart.reduce((total, item) => total + item.totalCost, 0);
|
| 243 |
totalCostElement.innerText = `Total Cart Cost: $${totalCartCost.toFixed(2)}`;
|
| 244 |
}
|
|
|
|
| 245 |
function proceedToCheckout() {
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
}
|
| 253 |
</script>
|
| 254 |
"""
|
| 255 |
return modal_script
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
return html_content
|
| 114 |
|
| 115 |
+
# Create Modal Window HTML
|
| 116 |
def create_modal_window():
|
| 117 |
add_ons = load_add_ons_from_salesforce()
|
| 118 |
add_ons_html = ""
|
|
|
|
| 152 |
<script>
|
| 153 |
let cart = [];
|
| 154 |
let totalCartCost = 0;
|
| 155 |
+
|
| 156 |
function openModal(name, image2, description, price) {
|
| 157 |
const modal = document.getElementById('modal');
|
| 158 |
modal.style.display = 'block';
|
|
|
|
| 167 |
document.getElementById('modal-price').innerText = price;
|
| 168 |
document.getElementById('quantity').value = 1;
|
| 169 |
document.getElementById('special-instructions').value = '';
|
| 170 |
+
|
| 171 |
resetAddOns(); // Reset add-ons when opening the modal
|
| 172 |
}
|
| 173 |
+
|
| 174 |
function closeModal() {
|
| 175 |
document.getElementById('modal').style.display = 'none';
|
| 176 |
}
|
| 177 |
+
|
| 178 |
function addToCart() {
|
| 179 |
const name = document.getElementById('modal-name').innerText;
|
| 180 |
const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
|
| 181 |
const quantity = parseInt(document.getElementById('quantity').value) || 1;
|
| 182 |
const instructions = document.getElementById('special-instructions').value;
|
| 183 |
+
|
| 184 |
const selectedAddOns = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked'));
|
| 185 |
const extras = selectedAddOns.map(extra => ({
|
| 186 |
name: extra.value,
|
| 187 |
price: parseFloat(extra.getAttribute('data-price')),
|
| 188 |
quantity: 1 // Default quantity for add-ons is 1
|
| 189 |
}));
|
| 190 |
+
|
| 191 |
const extrasCost = extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
|
| 192 |
const totalCost = (price * quantity) + extrasCost;
|
| 193 |
+
|
| 194 |
// Add the item to the cart with its specific add-ons
|
| 195 |
cart.push({ name, price, quantity, extras, instructions, totalCost });
|
| 196 |
totalCartCost += totalCost; // Update the total cost of the cart
|
| 197 |
+
|
| 198 |
updateCartButton();
|
| 199 |
updateCartTotalCost(); // Update total cost displayed
|
| 200 |
closeModal();
|
| 201 |
}
|
| 202 |
+
|
| 203 |
function updateCartButton() {
|
| 204 |
const cartButton = document.getElementById('cart-button');
|
| 205 |
cartButton.innerText = `View Cart (${cart.length} items)`;
|
| 206 |
}
|
| 207 |
+
|
| 208 |
function openCartModal() {
|
| 209 |
const cartModal = document.getElementById('cart-modal');
|
| 210 |
const cartItemsContainer = document.getElementById('cart-items');
|
| 211 |
cartItemsContainer.innerHTML = "";
|
| 212 |
cart.forEach((item, index) => {
|
| 213 |
+
const extrasList = item.extras.map(extra => `${extra.name} (+$${(extra.price * extra.quantity).toFixed(2)})`).join(', ');
|
| 214 |
cartItemsContainer.innerHTML += `
|
| 215 |
<div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 10px; border-radius: 8px;">
|
| 216 |
<h3>${item.name}</h3>
|
| 217 |
+
<p>Quantity: ${item.quantity}</p>
|
| 218 |
<p>Extras: ${extrasList || 'None'}</p>
|
| 219 |
<p>Special Instructions: ${item.instructions || 'None'}</p>
|
| 220 |
+
<p>Total Cost: $${item.totalCost.toFixed(2)}</p>
|
| 221 |
<button onclick="removeFromCart(${index})" style="color: red;">Remove</button>
|
| 222 |
</div>
|
| 223 |
`;
|
| 224 |
});
|
| 225 |
cartModal.style.display = 'block';
|
| 226 |
}
|
| 227 |
+
|
| 228 |
function closeCartModal() {
|
| 229 |
document.getElementById('cart-modal').style.display = 'none';
|
| 230 |
}
|
| 231 |
+
|
| 232 |
function removeFromCart(index) {
|
| 233 |
totalCartCost -= cart[index].totalCost; // Deduct the cost of the removed item from total cost
|
| 234 |
cart.splice(index, 1);
|
|
|
|
| 236 |
updateCartTotalCost(); // Update total cost displayed
|
| 237 |
openCartModal();
|
| 238 |
}
|
| 239 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
function updateCartTotalCost() {
|
| 241 |
const totalCostElement = document.getElementById('cart-total-cost');
|
| 242 |
totalCartCost = cart.reduce((total, item) => total + item.totalCost, 0);
|
| 243 |
totalCostElement.innerText = `Total Cart Cost: $${totalCartCost.toFixed(2)}`;
|
| 244 |
}
|
| 245 |
+
|
| 246 |
function proceedToCheckout() {
|
| 247 |
+
const checkoutPage = document.getElementById('checkout-page');
|
| 248 |
+
const cartSummaryContainer = document.getElementById('cart-summary');
|
| 249 |
+
cartSummaryContainer.innerHTML = "";
|
| 250 |
+
cart.forEach(item => {
|
| 251 |
+
const extrasList = item.extras.map(extra => `${extra.name} (+$${(extra.price * extra.quantity).toFixed(2)})`).join(', ');
|
| 252 |
+
cartSummaryContainer.innerHTML += `
|
| 253 |
+
<div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 10px; border-radius: 8px;">
|
| 254 |
+
<h3>${item.name}</h3>
|
| 255 |
+
<p>Quantity: ${item.quantity}</p>
|
| 256 |
+
<p>Extras: ${extrasList || 'None'}</p>
|
| 257 |
+
<p>Special Instructions: ${item.instructions || 'None'}</p>
|
| 258 |
+
<p>Total Cost: $${item.totalCost.toFixed(2)}</p>
|
| 259 |
+
</div>
|
| 260 |
+
`;
|
| 261 |
+
});
|
| 262 |
+
cartSummaryContainer.innerHTML += `<h3>Total Price: $${totalCartCost.toFixed(2)}</h3>`;
|
| 263 |
+
checkoutPage.style.display = 'block';
|
| 264 |
+
closeCartModal();
|
| 265 |
}
|
| 266 |
</script>
|
| 267 |
"""
|
| 268 |
return modal_script
|
| 269 |
+
|
| 270 |
+
# Gradio App
|
| 271 |
+
with gr.Blocks() as app:
|
| 272 |
+
with gr.Row():
|
| 273 |
+
gr.HTML("<h1 style='text-align: center;'>Welcome to Biryani Hub</h1>")
|
| 274 |
+
|
| 275 |
+
# Login Page
|
| 276 |
+
with gr.Row(visible=True) as login_page:
|
| 277 |
+
with gr.Column():
|
| 278 |
+
login_email = gr.Textbox(label="Email")
|
| 279 |
+
login_password = gr.Textbox(label="Password", type="password")
|
| 280 |
+
login_button = gr.Button("Login")
|
| 281 |
+
signup_button = gr.Button("Go to Signup")
|
| 282 |
+
login_output = gr.Textbox(label="Status")
|
| 283 |
+
|
| 284 |
+
# Signup Page
|
| 285 |
+
with gr.Row(visible=False) as signup_page:
|
| 286 |
+
with gr.Column():
|
| 287 |
+
signup_name = gr.Textbox(label="Name")
|
| 288 |
+
signup_email = gr.Textbox(label="Email")
|
| 289 |
+
signup_phone = gr.Textbox(label="Phone")
|
| 290 |
+
signup_password = gr.Textbox(label="Password", type="password")
|
| 291 |
+
submit_signup = gr.Button("Signup")
|
| 292 |
+
login_redirect = gr.Button("Go to Login")
|
| 293 |
+
signup_output = gr.Textbox(label="Status")
|
| 294 |
+
|
| 295 |
+
# Menu Page
|
| 296 |
+
with gr.Row(visible=False) as menu_page:
|
| 297 |
+
with gr.Column():
|
| 298 |
+
preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
|
| 299 |
+
menu_output = gr.HTML()
|
| 300 |
+
gr.HTML("<div id='cart-button' style='position: fixed; top: 20px; right: 20px; background: #28a745; color: white; padding: 10px 20px; border-radius: 30px; cursor: pointer; z-index: 1000;' onclick='openCartModal()'>View Cart</div>")
|
| 301 |
+
gr.HTML("<div id='cart-modal' style='display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: white; z-index: 1000; overflow-y: auto;'><div style='padding: 20px;'><div style='text-align: right;'><button onclick='closeCartModal()' style='background: none; border: none; font-size: 24px; cursor: pointer;'>×</button></div><h1>Your Cart</h1><div id='cart-items'></div><p id='cart-total-cost' style='font-size: 1.2em; font-weight: bold;'>Total Cart Cost: $0.00</p><button style='background: #ff5722; color: white; padding: 10px 20px; border-radius: 5px; border: none; cursor: pointer;' onclick='proceedToCheckout()'>Proceed to Checkout</button></div></div>")
|
| 302 |
+
gr.HTML(create_modal_window())
|
| 303 |
+
gr.HTML(modal_js())
|
| 304 |
+
|
| 305 |
+
# Checkout Page
|
| 306 |
+
with gr.Row(visible=False) as checkout_page:
|
| 307 |
+
with gr.Column():
|
| 308 |
+
gr.HTML("<h2 style='text-align: center;'>Final Order</h2>")
|
| 309 |
+
cart_summary = gr.HTML()
|
| 310 |
+
gr.Button("Confirm Order")
|
| 311 |
+
|
| 312 |
+
login_button.click(
|
| 313 |
+
lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!")
|
| 314 |
+
if login(email, password)[0] == "Login successful!" else (gr.update(), gr.update(), gr.update(), "Invalid email or password."),
|
| 315 |
+
[login_email, login_password], [login_page, menu_page, menu_output, login_output]
|
| 316 |
+
)
|
| 317 |
+
|
| 318 |
+
preference.change(lambda pref: filter_menu(pref), [preference], menu_output)
|
| 319 |
+
|
| 320 |
+
app.launch()
|