nagasurendra commited on
Commit
5b55110
·
verified ·
1 Parent(s): a2874a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -57
app.py CHANGED
@@ -112,23 +112,19 @@ def filter_menu(preference):
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,7 +156,7 @@ def create_modal_window():
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,65 +183,85 @@ 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():
@@ -285,4 +301,4 @@ with gr.Blocks() as app:
285
  )
286
  preference.change(lambda pref: filter_menu(pref), [preference], menu_output)
287
 
288
- app.launch()
 
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>"
120
  for item in cart:
121
+ order_summary += f"""
122
+ <li>{item['name']} (x{item['quantity']}) - ${item['totalCost']}</li>
 
 
 
 
 
123
  """
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 = ""
 
156
  <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>
157
  </div>
158
  """
159
+ return modal_html
160
 
161
  # JavaScript for Modal and Cart
162
  def modal_js():
 
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('$', ''));
189
  const quantity = parseInt(document.getElementById('quantity').value) || 1;
190
+ const instructions = document.getElementById('special-instructions').value;
191
+ const selectedAddOns = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked'));
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');
213
+ cartItemsContainer.innerHTML = "";
214
+ cart.forEach((item, index) => {
215
+ 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(', ');
216
+ cartItemsContainer.innerHTML += `
217
+ <div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 10px; border-radius: 8px;">
218
+ <h3>${item.name}</h3>
219
+ <p>Quantity: <input type="number" value="${item.quantity}" min="1" style="width: 50px;" onchange="updateCartItem(${index}, 'item', this.value)" /></p>
220
+ <p>Extras: ${extrasList || 'None'}</p>
221
+ <p>Special Instructions: ${item.instructions || 'None'}</p>
222
+ <p>Total Cost: $<span id="item-${index}-total">${item.totalCost.toFixed(2)}</span></p>
223
+ <button onclick="removeFromCart(${index})" style="color: red;">Remove</button>
224
+ </div>
225
+ `;
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
+ finalOrder.document.write('${proceed_to_checkout(cart)}');
260
  }
261
  </script>
262
  """
263
  return modal_script
264
+
265
  # Gradio App
266
  with gr.Blocks() as app:
267
  with gr.Row():
 
301
  )
302
  preference.change(lambda pref: filter_menu(pref), [preference], menu_output)
303
 
304
+ app.launch()