nagasurendra commited on
Commit
e7e8f69
·
verified ·
1 Parent(s): 581cda8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -32
app.py CHANGED
@@ -112,8 +112,8 @@ def filter_menu(preference):
112
 
113
  return html_content
114
 
115
- # Function to handle Proceed to Checkout
116
- def proceed_to_checkout(cart):
117
  total_cost = sum(item['totalCost'] for item in cart)
118
  order_summary = "<h2>Your Final Order</h2>"
119
  order_summary += "<ul>"
@@ -124,7 +124,7 @@ def proceed_to_checkout(cart):
124
  order_summary += f"</ul><h3>Total Bill: ${total_cost}</h3>"
125
  return order_summary
126
 
127
- # Function to create Modal Window
128
  def create_modal_window():
129
  add_ons = load_add_ons_from_salesforce()
130
  add_ons_html = ""
@@ -158,12 +158,14 @@ def create_modal_window():
158
  """
159
  return modal_html
160
 
161
- # JavaScript for Modal and Cart
162
  def modal_js():
163
  modal_script = """
164
  <script>
165
  let cart = [];
166
  let totalCartCost = 0;
 
 
167
  function openModal(name, image2, description, price) {
168
  const modal = document.getElementById('modal');
169
  modal.style.display = 'block';
@@ -178,11 +180,15 @@ def modal_js():
178
  document.getElementById('modal-price').innerText = price;
179
  document.getElementById('quantity').value = 1;
180
  document.getElementById('special-instructions').value = '';
181
- resetAddOns(); // Reset add-ons when opening the modal
182
  }
 
 
183
  function closeModal() {
184
  document.getElementById('modal').style.display = 'none';
185
  }
 
 
186
  function addToCart() {
187
  const name = document.getElementById('modal-name').innerText;
188
  const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
@@ -192,21 +198,25 @@ def modal_js():
192
  const extras = selectedAddOns.map(extra => ({
193
  name: extra.value,
194
  price: parseFloat(extra.getAttribute('data-price')),
195
- quantity: 1 // Default quantity for add-ons is 1
196
  }));
197
  const extrasCost = extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
198
  const totalCost = (price * quantity) + extrasCost;
199
- // Add the item to the cart with its specific add-ons
200
  cart.push({ name, price, quantity, extras, instructions, totalCost });
201
- totalCartCost += totalCost; // Update the total cost of the cart
202
  updateCartButton();
203
- updateCartTotalCost(); // Update total cost displayed
204
  closeModal();
205
  }
 
 
206
  function updateCartButton() {
207
  const cartButton = document.getElementById('cart-button');
208
  cartButton.innerText = `View Cart (${cart.length} items)`;
209
  }
 
 
210
  function openCartModal() {
211
  const cartModal = document.getElementById('cart-modal');
212
  const cartItemsContainer = document.getElementById('cart-items');
@@ -226,60 +236,63 @@ def modal_js():
226
  });
227
  cartModal.style.display = 'block';
228
  }
 
 
229
  function closeCartModal() {
230
  document.getElementById('cart-modal').style.display = 'none';
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);
235
  updateCartButton();
236
- updateCartTotalCost(); // Update total cost displayed
237
  openCartModal();
238
  }
 
 
239
  function updateCartItem(index, type, value) {
240
  if (type === 'item') {
241
  cart[index].quantity = parseInt(value);
242
  } else if (type === 'extra') {
243
- cart[index].extras[0].quantity = parseInt(value); // Assuming one add-on for simplicity
244
  }
245
  const item = cart[index];
246
  const price = item.price;
247
  const extrasCost = item.extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
248
  item.totalCost = (price * item.quantity) + extrasCost;
249
  document.getElementById(`item-${index}-total`).innerText = item.totalCost.toFixed(2);
250
- updateCartTotalCost(); // Update total cost displayed
251
  }
 
 
252
  function updateCartTotalCost() {
253
  const totalCostElement = document.getElementById('cart-total-cost');
254
  totalCartCost = cart.reduce((total, item) => total + item.totalCost, 0);
255
  totalCostElement.innerText = `Total Cart Cost: $${totalCartCost.toFixed(2)}`;
256
  }
 
 
257
  function proceedToCheckout() {
258
- const finalOrder = window.open('', '_blank');
259
-
260
  const orderSummary = `
261
- <html>
262
- <head><title>Order Summary</title></head>
263
- <body>
264
- <h2>Your Final Order</h2>
265
- <ul>
266
- ${cart.map(item => `
267
- <li>${item.name} (x${item.quantity}) - $${item.totalCost}</li>
268
- `).join('')}
269
- </ul>
270
- <h3>Total Bill: $${totalCartCost.toFixed(2)}</h3>
271
- </body>
272
- </html>
273
  `;
274
-
275
- finalOrder.document.write(orderSummary);
276
- finalOrder.document.close();
277
  }
278
-
279
  // Reset all selected add-ons when opening a new item modal
280
  function resetAddOns() {
281
  const checkboxes = document.querySelectorAll('input[name="biryani-extra"]');
282
- checkboxes.forEach(checkbox => checkbox.checked = false); // Uncheck all add-ons
283
  }
284
  </script>
285
  """
@@ -316,6 +329,7 @@ with gr.Blocks() as app:
316
  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>")
317
  gr.HTML(create_modal_window())
318
  gr.HTML(modal_js())
 
319
 
320
  login_button.click(
321
  lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!")
 
112
 
113
  return html_content
114
 
115
+ # Function to handle Proceed to Checkout and show the order summary on the same page
116
+ def show_order_summary(cart):
117
  total_cost = sum(item['totalCost'] for item in cart)
118
  order_summary = "<h2>Your Final Order</h2>"
119
  order_summary += "<ul>"
 
124
  order_summary += f"</ul><h3>Total Bill: ${total_cost}</h3>"
125
  return order_summary
126
 
127
+ # Function to create Modal Window for Menu Item Details
128
  def create_modal_window():
129
  add_ons = load_add_ons_from_salesforce()
130
  add_ons_html = ""
 
158
  """
159
  return modal_html
160
 
161
+ # JavaScript for Modal and Cart Management
162
  def modal_js():
163
  modal_script = """
164
  <script>
165
  let cart = [];
166
  let totalCartCost = 0;
167
+
168
+ // Open Item Modal
169
  function openModal(name, image2, description, price) {
170
  const modal = document.getElementById('modal');
171
  modal.style.display = 'block';
 
180
  document.getElementById('modal-price').innerText = price;
181
  document.getElementById('quantity').value = 1;
182
  document.getElementById('special-instructions').value = '';
183
+ resetAddOns();
184
  }
185
+
186
+ // Close the Modal
187
  function closeModal() {
188
  document.getElementById('modal').style.display = 'none';
189
  }
190
+
191
+ // Add Item to Cart
192
  function addToCart() {
193
  const name = document.getElementById('modal-name').innerText;
194
  const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
 
198
  const extras = selectedAddOns.map(extra => ({
199
  name: extra.value,
200
  price: parseFloat(extra.getAttribute('data-price')),
201
+ quantity: 1
202
  }));
203
  const extrasCost = extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
204
  const totalCost = (price * quantity) + extrasCost;
205
+
206
  cart.push({ name, price, quantity, extras, instructions, totalCost });
207
+ totalCartCost += totalCost;
208
  updateCartButton();
209
+ updateCartTotalCost();
210
  closeModal();
211
  }
212
+
213
+ // Update Cart Button
214
  function updateCartButton() {
215
  const cartButton = document.getElementById('cart-button');
216
  cartButton.innerText = `View Cart (${cart.length} items)`;
217
  }
218
+
219
+ // View Cart Modal
220
  function openCartModal() {
221
  const cartModal = document.getElementById('cart-modal');
222
  const cartItemsContainer = document.getElementById('cart-items');
 
236
  });
237
  cartModal.style.display = 'block';
238
  }
239
+
240
+ // Close Cart Modal
241
  function closeCartModal() {
242
  document.getElementById('cart-modal').style.display = 'none';
243
  }
244
+
245
+ // Remove Item from Cart
246
  function removeFromCart(index) {
247
+ totalCartCost -= cart[index].totalCost;
248
  cart.splice(index, 1);
249
  updateCartButton();
250
+ updateCartTotalCost();
251
  openCartModal();
252
  }
253
+
254
+ // Update Cart Item
255
  function updateCartItem(index, type, value) {
256
  if (type === 'item') {
257
  cart[index].quantity = parseInt(value);
258
  } else if (type === 'extra') {
259
+ cart[index].extras[0].quantity = parseInt(value);
260
  }
261
  const item = cart[index];
262
  const price = item.price;
263
  const extrasCost = item.extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
264
  item.totalCost = (price * item.quantity) + extrasCost;
265
  document.getElementById(`item-${index}-total`).innerText = item.totalCost.toFixed(2);
266
+ updateCartTotalCost();
267
  }
268
+
269
+ // Update Total Cart Cost
270
  function updateCartTotalCost() {
271
  const totalCostElement = document.getElementById('cart-total-cost');
272
  totalCartCost = cart.reduce((total, item) => total + item.totalCost, 0);
273
  totalCostElement.innerText = `Total Cart Cost: $${totalCartCost.toFixed(2)}`;
274
  }
275
+
276
+ // Proceed to Checkout and Show Summary in the Same Page
277
  function proceedToCheckout() {
 
 
278
  const orderSummary = `
279
+ <div>
280
+ <h2>Your Final Order</h2>
281
+ <ul>
282
+ ${cart.map(item => `
283
+ <li>${item.name} (x${item.quantity}) - $${item.totalCost}</li>
284
+ `).join('')}
285
+ </ul>
286
+ <h3>Total Bill: $${totalCartCost.toFixed(2)}</h3>
287
+ </div>
 
 
 
288
  `;
289
+ document.getElementById('order-summary').innerHTML = orderSummary;
 
 
290
  }
291
+
292
  // Reset all selected add-ons when opening a new item modal
293
  function resetAddOns() {
294
  const checkboxes = document.querySelectorAll('input[name="biryani-extra"]');
295
+ checkboxes.forEach(checkbox => checkbox.checked = false);
296
  }
297
  </script>
298
  """
 
329
  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>")
330
  gr.HTML(create_modal_window())
331
  gr.HTML(modal_js())
332
+ gr.HTML("<div id='order-summary'></div>")
333
 
334
  login_button.click(
335
  lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!")