nagasurendra commited on
Commit
a2874a3
·
verified ·
1 Parent(s): 6cf5b04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -80
app.py CHANGED
@@ -112,22 +112,23 @@ def filter_menu(preference):
112
 
113
  return html_content
114
 
115
- # Function to generate final order
116
- def submit_cart():
117
- final_order_html = "<h3>Final Order:</h3><ul>"
118
- total_bill = 0
119
  for item in cart:
120
- total_bill += item['totalCost']
121
- extras = ", ".join([f"{extra['name']} (+${extra['price'] * extra['quantity']})" for extra in item['extras']])
122
- final_order_html += f"""
123
- <li>
124
- {item['name']} (x{item['quantity']}) - ${item['totalCost']:.2f}
125
- <br>Extras: {extras if extras else "None"}
126
- <br>Special Instructions: {item['instructions'] if item['instructions'] else "None"}
127
- </li>
128
  """
129
- final_order_html += f"</ul><p><strong>Total Bill: ${total_bill:.2f}</strong></p>"
130
- return final_order_html
 
 
131
  def create_modal_window():
132
  add_ons = load_add_ons_from_salesforce()
133
  add_ons_html = ""
@@ -159,8 +160,7 @@ def create_modal_window():
159
  <button style="background-color: #28a745; color: white; border: none; padding: 10px 20px; font-size: 14px; border-radius: 5px; cursor: pointer;" onclick="addToCart()">Add to Cart</button>
160
  </div>
161
  """
162
- return modal_html
163
-
164
 
165
  # JavaScript for Modal and Cart
166
  def modal_js():
@@ -187,81 +187,65 @@ def modal_js():
187
  function closeModal() {
188
  document.getElementById('modal').style.display = 'none';
189
  }
 
 
 
 
190
  function addToCart() {
191
  const name = document.getElementById('modal-name').innerText;
192
  const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
193
  const quantity = parseInt(document.getElementById('quantity').value) || 1;
194
- const instructions = document.getElementById('special-instructions').value;
195
- const selectedAddOns = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked'));
196
- const extras = selectedAddOns.map(extra => ({
197
- name: extra.value,
198
- price: parseFloat(extra.getAttribute('data-price')),
199
- quantity: 1 // Default quantity for add-ons is 1
200
- }));
201
- const extrasCost = extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
202
- const totalCost = (price * quantity) + extrasCost;
203
- // Add the item to the cart with its specific add-ons
204
- cart.push({ name, price, quantity, extras, instructions, totalCost });
205
- totalCartCost += totalCost; // Update the total cost of the cart
206
- updateCartButton();
207
- updateCartTotalCost(); // Update total cost displayed
 
 
 
208
  closeModal();
209
  }
210
- function updateCartButton() {
211
- const cartButton = document.getElementById('cart-button');
212
- cartButton.innerText = `View Cart (${cart.length} items)`;
 
 
 
 
 
 
 
 
213
  }
 
214
  function openCartModal() {
215
- const cartModal = document.getElementById('cart-modal');
216
- const cartItemsContainer = document.getElementById('cart-items');
217
- cartItemsContainer.innerHTML = "";
218
- cart.forEach((item, index) => {
219
- const extrasList = item.extras.map(extra => `${extra.name} x<input type="number" value="${extra.quantity}" min="1" style="width: 50px;" onchange="updateCartItem(${index}, 'extra', this.value)" /> (+$${(extra.price * extra.quantity).toFixed(2)})`).join(', ');
220
- cartItemsContainer.innerHTML += `
221
- <div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 10px; border-radius: 8px;">
222
- <h3>${item.name}</h3>
223
- <p>Quantity: <input type="number" value="${item.quantity}" min="1" style="width: 50px;" onchange="updateCartItem(${index}, 'item', this.value)" /></p>
224
- <p>Extras: ${extrasList || 'None'}</p>
225
- <p>Special Instructions: ${item.instructions || 'None'}</p>
226
- <p>Total Cost: $<span id="item-${index}-total">${item.totalCost.toFixed(2)}</span></p>
227
- <button onclick="removeFromCart(${index})" style="color: red;">Remove</button>
228
- </div>
229
- `;
230
- });
231
- cartModal.style.display = 'block';
232
  }
 
233
  function closeCartModal() {
234
  document.getElementById('cart-modal').style.display = 'none';
235
  }
236
- function removeFromCart(index) {
237
- totalCartCost -= cart[index].totalCost; // Deduct the cost of the removed item from total cost
238
- cart.splice(index, 1);
239
- updateCartButton();
240
- updateCartTotalCost(); // Update total cost displayed
241
- openCartModal();
242
- }
243
- function updateCartItem(index, type, value) {
244
- if (type === 'item') {
245
- cart[index].quantity = parseInt(value);
246
- } else if (type === 'extra') {
247
- cart[index].extras[0].quantity = parseInt(value); // Assuming one add-on for simplicity
248
- }
249
- const item = cart[index];
250
- const price = item.price;
251
- const extrasCost = item.extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
252
- item.totalCost = (price * item.quantity) + extrasCost;
253
- document.getElementById(`item-${index}-total`).innerText = item.totalCost.toFixed(2);
254
- updateCartTotalCost(); // Update total cost displayed
255
  }
256
- function updateCartTotalCost() {
257
- const totalCostElement = document.getElementById('cart-total-cost');
258
- totalCartCost = cart.reduce((total, item) => total + item.totalCost, 0);
259
- totalCostElement.innerText = `Total Cart Cost: $${totalCartCost.toFixed(2)}`;
260
  }
261
  </script>
262
  """
263
  return modal_script
264
-
265
  # Gradio App
266
  with gr.Blocks() as app:
267
  with gr.Row():
@@ -289,11 +273,9 @@ with gr.Blocks() as app:
289
  with gr.Column():
290
  preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
291
  menu_output = gr.HTML()
292
- gr.HTML("<div id='cart-button' onclick='openCartModal()'>View Cart</div>")
293
- gr.HTML("<div id='cart-modal' style='display: none;'>")
294
- gr.HTML("<div id='cart-items'></div>")
295
- gr.HTML("<button onclick='submitCart()'>Proceed to Checkout</button>")
296
- gr.HTML("</div>")
297
  gr.HTML(modal_js())
298
 
299
  login_button.click(
@@ -303,4 +285,4 @@ with gr.Blocks() as app:
303
  )
304
  preference.change(lambda pref: filter_menu(pref), [preference], menu_output)
305
 
306
- app.launch()
 
112
 
113
  return html_content
114
 
115
+ # Function to finalize order
116
+ def finalize_order(cart):
117
+ total_cost = sum(item['totalCost'] for item in cart)
118
+ order_details = ''
119
  for item in cart:
120
+ order_details += f"""
121
+ <div>
122
+ <h3>{item['name']} (x{item['quantity']})</h3>
123
+ <p>Add-ons: {', '.join([extra['name'] for extra in item['extras']]) if item['extras'] else 'None'}</p>
124
+ <p>Special Instructions: {item['instructions']}</p>
125
+ <p>Cost: ${item['totalCost']}</p>
126
+ </div>
 
127
  """
128
+ order_details += f"<h2>Total Cart Cost: ${total_cost}</h2>"
129
+ return order_details
130
+
131
+ # Create Modal Window HTML
132
  def create_modal_window():
133
  add_ons = load_add_ons_from_salesforce()
134
  add_ons_html = ""
 
160
  <button style="background-color: #28a745; color: white; border: none; padding: 10px 20px; font-size: 14px; border-radius: 5px; cursor: pointer;" onclick="addToCart()">Add to Cart</button>
161
  </div>
162
  """
163
+ return modal_html
 
164
 
165
  # JavaScript for Modal and Cart
166
  def modal_js():
 
187
  function closeModal() {
188
  document.getElementById('modal').style.display = 'none';
189
  }
190
+ function resetAddOns() {
191
+ const checkboxes = document.querySelectorAll('input[name="biryani-extra"]');
192
+ checkboxes.forEach(checkbox => checkbox.checked = false); // Uncheck all add-ons
193
+ }
194
  function addToCart() {
195
  const name = document.getElementById('modal-name').innerText;
196
  const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
197
  const quantity = parseInt(document.getElementById('quantity').value) || 1;
198
+ const extras = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked'))
199
+ .map(checkbox => ({ name: checkbox.value, price: parseFloat(checkbox.getAttribute('data-price')) }));
200
+
201
+ const specialInstructions = document.getElementById('special-instructions').value;
202
+
203
+ const item = {
204
+ name,
205
+ quantity,
206
+ extras,
207
+ instructions: specialInstructions,
208
+ totalCost: (price + extras.reduce((acc, extra) => acc + extra.price, 0)) * quantity
209
+ };
210
+
211
+ cart.push(item);
212
+ totalCartCost += item.totalCost;
213
+
214
+ updateCart();
215
  closeModal();
216
  }
217
+
218
+ function updateCart() {
219
+ let cartItemsHtml = '';
220
+ cart.forEach(item => {
221
+ cartItemsHtml += `<div><h3>${item.name} (x${item.quantity})</h3>
222
+ <p>Special Instructions: ${item.instructions}</p>
223
+ <p>Add-ons: ${item.extras.map(extra => extra.name).join(', ')}</p>
224
+ <p>Cost: $${item.totalCost}</p></div>`;
225
+ });
226
+ document.getElementById('cart-items').innerHTML = cartItemsHtml;
227
+ document.getElementById('cart-total-cost').innerText = 'Total Cart Cost: $' + totalCartCost.toFixed(2);
228
  }
229
+
230
  function openCartModal() {
231
+ document.getElementById('cart-modal').style.display = 'block';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
  }
233
+
234
  function closeCartModal() {
235
  document.getElementById('cart-modal').style.display = 'none';
236
  }
237
+
238
+ function proceedToCheckout() {
239
+ alert('Proceeding to Checkout!');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
240
  }
241
+ // Reset all selected add-ons when opening a new item modal
242
+ function resetAddOns() {
243
+ const checkboxes = document.querySelectorAll('input[name="biryani-extra"]');
244
+ checkboxes.forEach(checkbox => checkbox.checked = false); // Uncheck all add-ons
245
  }
246
  </script>
247
  """
248
  return modal_script
 
249
  # Gradio App
250
  with gr.Blocks() as app:
251
  with gr.Row():
 
273
  with gr.Column():
274
  preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
275
  menu_output = gr.HTML()
276
+ 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>")
277
+ 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>")
278
+ gr.HTML(create_modal_window())
 
 
279
  gr.HTML(modal_js())
280
 
281
  login_button.click(
 
285
  )
286
  preference.change(lambda pref: filter_menu(pref), [preference], menu_output)
287
 
288
+ app.launch()