nagasurendra commited on
Commit
3850ba9
·
verified ·
1 Parent(s): c6ad41e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -20
app.py CHANGED
@@ -184,10 +184,11 @@ def modal_js():
184
  }));
185
  const extrasCost = extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
186
  const totalCost = (price * quantity) + extrasCost;
 
187
  cart.push({ name, price, quantity, extras, instructions, totalCost });
188
- totalCartCost += totalCost;
189
  updateCartButton();
190
- updateCartTotalCost();
191
  closeModal();
192
  }
193
  function updateCartButton() {
@@ -217,24 +218,24 @@ def modal_js():
217
  document.getElementById('cart-modal').style.display = 'none';
218
  }
219
  function removeFromCart(index) {
220
- totalCartCost -= cart[index].totalCost;
221
  cart.splice(index, 1);
222
  updateCartButton();
223
- updateCartTotalCost();
224
  openCartModal();
225
  }
226
  function updateCartItem(index, type, value) {
227
  if (type === 'item') {
228
  cart[index].quantity = parseInt(value);
229
  } else if (type === 'extra') {
230
- cart[index].extras[0].quantity = parseInt(value);
231
  }
232
  const item = cart[index];
233
  const price = item.price;
234
  const extrasCost = item.extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
235
  item.totalCost = (price * item.quantity) + extrasCost;
236
  document.getElementById(`item-${index}-total`).innerText = item.totalCost.toFixed(2);
237
- updateCartTotalCost();
238
  }
239
  function updateCartTotalCost() {
240
  const totalCostElement = document.getElementById('cart-total-cost');
@@ -242,11 +243,24 @@ def modal_js():
242
  totalCostElement.innerText = `Total Cart Cost: $${totalCartCost.toFixed(2)}`;
243
  }
244
  function proceedToCheckout() {
245
- window.location.href = '/final-order-page'; // Redirect to final order page
246
- }
247
- function resetAddOns() {
248
- const checkboxes = document.querySelectorAll('input[name="biryani-extra"]');
249
- checkboxes.forEach(checkbox => checkbox.checked = false);
 
 
 
 
 
 
 
 
 
 
 
 
 
250
  }
251
  </script>
252
  """
@@ -282,25 +296,24 @@ with gr.Blocks() as app:
282
  with gr.Column():
283
  preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
284
  menu_output = gr.HTML()
 
 
285
  gr.HTML(create_modal_window())
286
  gr.HTML(modal_js())
287
 
288
- # Final Order Page
289
- with gr.Row(visible=False) as final_order_page:
290
  with gr.Column():
291
- final_order_output = gr.HTML()
 
 
292
 
293
- # Login button action
294
  login_button.click(
295
  lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!")
296
  if login(email, password)[0] == "Login successful!" else (gr.update(), gr.update(), gr.update(), "Invalid email or password."),
297
  [login_email, login_password], [login_page, menu_page, menu_output, login_output]
298
  )
299
 
300
- # Signup button action
301
- signup_button.click(lambda: gr.update(visible=False), [], login_page)
302
-
303
- # Menu preference change
304
  preference.change(lambda pref: filter_menu(pref), [preference], menu_output)
305
 
306
- app.launch()
 
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() {
 
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);
223
  updateCartButton();
224
+ updateCartTotalCost(); // Update total cost displayed
225
  openCartModal();
226
  }
227
  function updateCartItem(index, type, value) {
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');
 
243
  totalCostElement.innerText = `Total Cart Cost: $${totalCartCost.toFixed(2)}`;
244
  }
245
  function proceedToCheckout() {
246
+ const checkoutPage = document.getElementById('checkout-page');
247
+ const cartSummaryContainer = document.getElementById('cart-summary');
248
+ cartSummaryContainer.innerHTML = "";
249
+ cart.forEach(item => {
250
+ const extrasList = item.extras.map(extra => `${extra.name} (+$${(extra.price * extra.quantity).toFixed(2)})`).join(', ');
251
+ cartSummaryContainer.innerHTML += `
252
+ <div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 10px; border-radius: 8px;">
253
+ <h3>${item.name}</h3>
254
+ <p>Quantity: ${item.quantity}</p>
255
+ <p>Extras: ${extrasList || 'None'}</p>
256
+ <p>Special Instructions: ${item.instructions || 'None'}</p>
257
+ <p>Total Cost: $${item.totalCost.toFixed(2)}</p>
258
+ </div>
259
+ `;
260
+ });
261
+ cartSummaryContainer.innerHTML += `<h3>Total Price: $${totalCartCost.toFixed(2)}</h3>`;
262
+ checkoutPage.style.display = 'block';
263
+ closeCartModal();
264
  }
265
  </script>
266
  """
 
296
  with gr.Column():
297
  preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
298
  menu_output = gr.HTML()
299
+ 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>")
300
+ 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;'>&times;</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>")
301
  gr.HTML(create_modal_window())
302
  gr.HTML(modal_js())
303
 
304
+ # Checkout Page
305
+ with gr.Row(visible=False) as checkout_page:
306
  with gr.Column():
307
+ gr.HTML("<h2 style='text-align: center;'>Final Order</h2>")
308
+ cart_summary = gr.HTML()
309
+ gr.Button("Confirm Order")
310
 
 
311
  login_button.click(
312
  lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!")
313
  if login(email, password)[0] == "Login successful!" else (gr.update(), gr.update(), gr.update(), "Invalid email or password."),
314
  [login_email, login_password], [login_page, menu_page, menu_output, login_output]
315
  )
316
 
 
 
 
 
317
  preference.change(lambda pref: filter_menu(pref), [preference], menu_output)
318
 
319
+ app.launch()