nagasurendra commited on
Commit
d45f86f
·
verified ·
1 Parent(s): 0383f12

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -62
app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  import bcrypt
3
  import gradio as gr
4
  from simple_salesforce import Salesforce
@@ -112,7 +111,8 @@ def filter_menu(preference):
112
  return "<p>No items match your filter.</p>"
113
 
114
  return html_content
115
- # Create Modal Window HTML
 
116
  def create_modal_window():
117
  add_ons = load_add_ons_from_salesforce()
118
  add_ons_html = ""
@@ -146,37 +146,7 @@ def create_modal_window():
146
  """
147
  return modal_html
148
 
149
- # Updated Cart Modal HTML
150
- def create_cart_modal():
151
- cart_modal_html = """
152
- <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;">
153
- <div style="padding: 20px;">
154
- <div style="text-align: right;">
155
- <button onclick="closeCartModal()" style="background: none; border: none; font-size: 24px; cursor: pointer;">&times;</button>
156
- </div>
157
- <h1>Your Cart</h1>
158
- <div id="cart-items"></div>
159
- <p id="cart-total-cost" style="font-size: 1.2em; font-weight: bold;">Total Cart Cost: $0.00</p>
160
- <button style="background: #ff5722; color: white; padding: 10px 20px; border-radius: 5px; border: none; cursor: pointer;" onclick="proceedToCheckout()">Proceed to Checkout</button>
161
- <button style="background: #007bff; color: white; padding: 10px 20px; border-radius: 5px; border: none; cursor: pointer;" onclick="submitOrder()">Submit Order</button>
162
- </div>
163
- </div>
164
- """
165
- return cart_modal_html
166
-
167
- # Final Order Page HTML
168
- def create_final_order_page():
169
- final_order_html = """
170
- <div id="final-order-page" style="display: none; padding: 20px; max-width: 1200px; margin: auto;">
171
- <h1>Final Order</h1>
172
- <div id="final-order-items"></div>
173
- <p id="final-order-total-cost" style="font-size: 1.2em; font-weight: bold;">Total Order Cost: $0.00</p>
174
- <button onclick="confirmOrder()" style="background-color: #28a745; color: white; padding: 10px 20px; border-radius: 5px; cursor: pointer;">Confirm Order</button>
175
- </div>
176
- """
177
- return final_order_html
178
-
179
- # Updated JavaScript for Submit Order
180
  def modal_js():
181
  modal_script = """
182
  <script>
@@ -185,10 +155,17 @@ def modal_js():
185
  function openModal(name, image2, description, price) {
186
  const modal = document.getElementById('modal');
187
  modal.style.display = 'block';
 
 
 
 
 
188
  document.getElementById('modal-image').src = image2;
189
  document.getElementById('modal-name').innerText = name;
190
  document.getElementById('modal-description').innerText = description;
191
  document.getElementById('modal-price').innerText = price;
 
 
192
  resetAddOns(); // Reset add-ons when opening the modal
193
  }
194
  function closeModal() {
@@ -203,14 +180,15 @@ def modal_js():
203
  const extras = selectedAddOns.map(extra => ({
204
  name: extra.value,
205
  price: parseFloat(extra.getAttribute('data-price')),
206
- quantity: 1
207
  }));
208
  const extrasCost = extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
209
  const totalCost = (price * quantity) + extrasCost;
 
210
  cart.push({ name, price, quantity, extras, instructions, totalCost });
211
- totalCartCost += totalCost;
212
  updateCartButton();
213
- updateCartTotalCost();
214
  closeModal();
215
  }
216
  function updateCartButton() {
@@ -222,31 +200,58 @@ def modal_js():
222
  const cartItemsContainer = document.getElementById('cart-items');
223
  cartItemsContainer.innerHTML = "";
224
  cart.forEach((item, index) => {
225
- const extrasList = item.extras.map(extra => `${extra.name} (+$${(extra.price * extra.quantity).toFixed(2)})`).join(', ');
226
  cartItemsContainer.innerHTML += `
227
- <div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 10px;">
228
  <h3>${item.name}</h3>
229
- <p>Quantity: ${item.quantity}</p>
230
  <p>Extras: ${extrasList || 'None'}</p>
231
  <p>Special Instructions: ${item.instructions || 'None'}</p>
232
- <p>Total Cost: $${item.totalCost.toFixed(2)}</p>
 
233
  </div>
234
  `;
235
  });
236
- document.getElementById('cart-total-cost').innerText = `Total Cart Cost: $${totalCartCost.toFixed(2)}`;
237
  cartModal.style.display = 'block';
238
  }
239
  function closeCartModal() {
240
  document.getElementById('cart-modal').style.display = 'none';
241
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
242
  function submitOrder() {
243
- const finalOrderPage = document.getElementById('final-order-page');
244
- const finalOrderItemsContainer = document.getElementById('final-order-items');
245
- finalOrderItemsContainer.innerHTML = "";
246
- cart.forEach((item) => {
247
- const extrasList = item.extras.map(extra => `${extra.name} (+$${(extra.price * extra.quantity).toFixed(2)})`).join(', ');
248
- finalOrderItemsContainer.innerHTML += `
249
- <div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 10px;">
250
  <h3>${item.name}</h3>
251
  <p>Quantity: ${item.quantity}</p>
252
  <p>Extras: ${extrasList || 'None'}</p>
@@ -254,18 +259,21 @@ def modal_js():
254
  <p>Total Cost: $${item.totalCost.toFixed(2)}</p>
255
  </div>
256
  `;
257
- });
258
- document.getElementById('final-order-total-cost').innerText = `Total Order Cost: $${totalCartCost.toFixed(2)}`;
 
 
 
 
 
259
  finalOrderPage.style.display = 'block';
260
  document.getElementById('cart-modal').style.display = 'none';
261
  }
262
- function confirmOrder() {
263
- alert("Order confirmed! Thank you for your purchase.");
264
- cart = [];
265
- totalCartCost = 0;
266
- updateCartButton();
267
- updateCartTotalCost();
268
  document.getElementById('final-order-page').style.display = 'none';
 
269
  }
270
  </script>
271
  """
@@ -298,19 +306,23 @@ with gr.Blocks() as app:
298
  with gr.Column():
299
  preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
300
  menu_output = gr.HTML()
301
- 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;' onclick='openCartModal()'>View Cart</div>")
302
- 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><button style='background: #007bff; color: white; padding: 10px 20px; border-radius: 5px; border: none; cursor: pointer;' onclick='submitOrder()'>Submit Order</button></div></div>")
303
-
304
- # Add Final Order Page
305
- gr.HTML(create_final_order_page())
306
-
307
  gr.HTML(create_modal_window())
308
  gr.HTML(modal_js())
309
 
 
 
 
 
 
 
 
310
  login_button.click(
311
  lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!")
312
  if login(email, password)[0] == "Login successful!" else (gr.update(), gr.update(), gr.update(), "Invalid email or password."),
313
  [login_email, login_password], [login_page, menu_page, menu_output, login_output]
314
  )
 
315
 
316
  app.launch()
 
 
1
  import bcrypt
2
  import gradio as gr
3
  from simple_salesforce import Salesforce
 
111
  return "<p>No items match your filter.</p>"
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 = ""
 
146
  """
147
  return modal_html
148
 
149
+ # JavaScript for Modal and Cart
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  def modal_js():
151
  modal_script = """
152
  <script>
 
155
  function openModal(name, image2, description, price) {
156
  const modal = document.getElementById('modal');
157
  modal.style.display = 'block';
158
+ modal.style.position = 'fixed';
159
+ modal.style.width = window.innerWidth <= 768 ? '90%' : '30%';
160
+ modal.style.top = `${event.clientY}px`;
161
+ modal.style.left = '50%';
162
+ modal.style.transform = 'translate(-50%, -50%)';
163
  document.getElementById('modal-image').src = image2;
164
  document.getElementById('modal-name').innerText = name;
165
  document.getElementById('modal-description').innerText = description;
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() {
 
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() {
 
200
  const cartItemsContainer = document.getElementById('cart-items');
201
  cartItemsContainer.innerHTML = "";
202
  cart.forEach((item, index) => {
203
+ 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(', ');
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: <input type="number" value="${item.quantity}" min="1" style="width: 50px;" onchange="updateCartItem(${index}, 'item', this.value)" /></p>
208
  <p>Extras: ${extrasList || 'None'}</p>
209
  <p>Special Instructions: ${item.instructions || 'None'}</p>
210
+ <p>Total Cost: $<span id="item-${index}-total">${item.totalCost.toFixed(2)}</span></p>
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);
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');
242
+ totalCartCost = cart.reduce((total, item) => total + item.totalCost, 0);
243
+ totalCostElement.innerText = `Total Cart Cost: $${totalCartCost.toFixed(2)}`;
244
+ }
245
+ function proceedToCheckout() {
246
+ alert("Proceeding to checkout...");
247
+ }
248
+
249
+ // Submit Order and go to Final Order Page
250
  function submitOrder() {
251
+ const orderDetails = cart.map(item => {
252
+ const extrasList = item.extras.map(extra => `${extra.name} x${extra.quantity} (+$${(extra.price * extra.quantity).toFixed(2)})`).join(', ');
253
+ return `
254
+ <div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 10px; border-radius: 8px;">
 
 
 
255
  <h3>${item.name}</h3>
256
  <p>Quantity: ${item.quantity}</p>
257
  <p>Extras: ${extrasList || 'None'}</p>
 
259
  <p>Total Cost: $${item.totalCost.toFixed(2)}</p>
260
  </div>
261
  `;
262
+ }).join('');
263
+
264
+ const finalOrderPage = document.getElementById('final-order-page');
265
+ const orderDetailsContainer = document.getElementById('order-details');
266
+ orderDetailsContainer.innerHTML = orderDetails;
267
+
268
+ // Show the final order page and hide the cart page
269
  finalOrderPage.style.display = 'block';
270
  document.getElementById('cart-modal').style.display = 'none';
271
  }
272
+
273
+ // Go back to Menu Page from Final Order Page
274
+ function goBackToMenu() {
 
 
 
275
  document.getElementById('final-order-page').style.display = 'none';
276
+ document.getElementById('menu-page').style.display = 'block';
277
  }
278
  </script>
279
  """
 
306
  with gr.Column():
307
  preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
308
  menu_output = gr.HTML()
309
+ 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>")
310
+ 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 onclick='submitOrder()' style='background: #007bff; color: white; padding: 10px 20px; border-radius: 5px;'>Submit Order</button></div></div>")
 
 
 
 
311
  gr.HTML(create_modal_window())
312
  gr.HTML(modal_js())
313
 
314
+ # Final Order Page
315
+ with gr.Row(visible=False) as final_order_page:
316
+ with gr.Column():
317
+ gr.HTML("<h1 style='text-align: center;'>Final Order</h1>")
318
+ order_details_output = gr.HTML()
319
+ gr.HTML("<button onclick='goBackToMenu()' style='background: #28a745; color: white; padding: 10px 20px; border-radius: 5px;'>Back to Menu</button>")
320
+
321
  login_button.click(
322
  lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!")
323
  if login(email, password)[0] == "Login successful!" else (gr.update(), gr.update(), gr.update(), "Invalid email or password."),
324
  [login_email, login_password], [login_page, menu_page, menu_output, login_output]
325
  )
326
+ preference.change(lambda pref: filter_menu(pref), [preference], menu_output)
327
 
328
  app.launch()