nagasurendra commited on
Commit
59cd355
·
verified ·
1 Parent(s): f012276

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -141
app.py CHANGED
@@ -64,19 +64,9 @@ def load_menu_from_salesforce():
64
  except Exception as e:
65
  return []
66
 
67
- # Function to load add-ons data
68
- def load_add_ons_from_salesforce():
69
- try:
70
- query = "SELECT Name, Price__c FROM Add_Ons__c"
71
- result = sf.query(query)
72
- return result['records']
73
- except Exception as e:
74
- return []
75
-
76
  # Function to filter menu items
77
  def filter_menu(preference):
78
  menu_data = load_menu_from_salesforce()
79
-
80
  filtered_data = {}
81
  for item in menu_data:
82
  if "Section__c" not in item or "Veg_NonVeg__c" not in item:
@@ -112,46 +102,13 @@ def filter_menu(preference):
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 = ""
119
- for add_on in add_ons:
120
- add_ons_html += f"""
121
- <label>
122
- <input type="checkbox" name="biryani-extra" value="{add_on['Name']}" data-price="{add_on['Price__c']}" />
123
- {add_on['Name']} + ${add_on['Price__c']}
124
- </label>
125
- <br>
126
- """
127
-
128
- modal_html = f"""
129
- <div id="modal" style="display: none; position: fixed; background: white; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); padding: 20px; z-index: 1000;">
130
- <div style="text-align: right;">
131
- <button onclick="closeModal()" style="background: none; border: none; font-size: 18px; cursor: pointer;">&times;</button>
132
- </div>
133
- <img id="modal-image" style="width: 100%; height: 300px; border-radius: 8px; margin-bottom: 20px;" />
134
- <h2 id="modal-name"></h2>
135
- <p id="modal-description"></p>
136
- <p id="modal-price"></p>
137
- <label for="biryani-extras"><strong>Add-ons :</strong></label>
138
- <div id="biryani-extras-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
139
- {add_ons_html}
140
- </div>
141
- <label for="quantity">Quantity:</label>
142
- <input type="number" id="quantity" value="1" min="1" style="width: 50px;" />
143
- <textarea id="special-instructions" placeholder="Add your special instructions here..." style="width: 100%; height: 60px;"></textarea>
144
- <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>
145
- </div>
146
- """
147
- return modal_html
148
-
149
- # JavaScript for Modal and Cart
150
  def modal_js():
151
  modal_script = """
152
  <script>
153
  let cart = [];
154
  let totalCartCost = 0;
 
155
  function openModal(name, image2, description, price) {
156
  const modal = document.getElementById('modal');
157
  modal.style.display = 'block';
@@ -166,104 +123,44 @@ def modal_js():
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() {
172
  document.getElementById('modal').style.display = 'none';
173
  }
 
174
  function addToCart() {
175
  const name = document.getElementById('modal-name').innerText;
176
  const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
177
  const quantity = parseInt(document.getElementById('quantity').value) || 1;
178
  const instructions = document.getElementById('special-instructions').value;
179
- const selectedAddOns = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked'));
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() {
195
  const cartButton = document.getElementById('cart-button');
196
  cartButton.innerText = `View Cart (${cart.length} items)`;
197
  }
198
- function openCartModal() {
199
- const cartModal = document.getElementById('cart-modal');
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
- // Displaying the final order details
247
- const finalOrderContainer = document.getElementById('final-order');
248
- finalOrderContainer.innerHTML = "";
249
- cart.forEach((item, index) => {
250
- const extrasList = item.extras.map(extra => `${extra.name} x${extra.quantity} (+$${(extra.price * extra.quantity).toFixed(2)})`).join(', ');
251
- finalOrderContainer.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
- finalOrderContainer.innerHTML += `<h3>Total Order Cost: $${totalCartCost.toFixed(2)}</h3>`;
262
- }
263
- // Reset all selected add-ons when opening a new item modal
264
- function resetAddOns() {
265
- const checkboxes = document.querySelectorAll('input[name="biryani-extra"]');
266
- checkboxes.forEach(checkbox => checkbox.checked = false); // Uncheck all add-ons
267
  }
268
  </script>
269
  """
@@ -279,7 +176,7 @@ with gr.Blocks() as app:
279
  login_email = gr.Textbox(label="Email")
280
  login_password = gr.Textbox(label="Password", type="password")
281
  login_button = gr.Button("Login")
282
- signup_button = gr.Button("Go to Signup")
283
  login_output = gr.Textbox(label="Status")
284
 
285
  with gr.Row(visible=False) as signup_page:
@@ -294,24 +191,46 @@ with gr.Blocks() as app:
294
 
295
  with gr.Row(visible=False) as menu_page:
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
- with gr.Row(visible=False) as final_order_page:
305
- with gr.Column():
306
- gr.HTML("<h2 style='text-align: center;'>Final Order</h2>")
307
- final_order = gr.HTML()
308
- gr.HTML("<button onclick='proceedToCheckout()'>Place Your Order</button>")
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
- preference.change(lambda pref: filter_menu(pref), [preference], menu_output)
316
 
317
  app.launch()
 
64
  except Exception as e:
65
  return []
66
 
 
 
 
 
 
 
 
 
 
67
  # Function to filter menu items
68
  def filter_menu(preference):
69
  menu_data = load_menu_from_salesforce()
 
70
  filtered_data = {}
71
  for item in menu_data:
72
  if "Section__c" not in item or "Veg_NonVeg__c" not in item:
 
102
 
103
  return html_content
104
 
105
+ # JavaScript for Modal and Cart Functionality
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  def modal_js():
107
  modal_script = """
108
  <script>
109
  let cart = [];
110
  let totalCartCost = 0;
111
+
112
  function openModal(name, image2, description, price) {
113
  const modal = document.getElementById('modal');
114
  modal.style.display = 'block';
 
123
  document.getElementById('modal-price').innerText = price;
124
  document.getElementById('quantity').value = 1;
125
  document.getElementById('special-instructions').value = '';
 
126
  }
127
+
128
  function closeModal() {
129
  document.getElementById('modal').style.display = 'none';
130
  }
131
+
132
  function addToCart() {
133
  const name = document.getElementById('modal-name').innerText;
134
  const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
135
  const quantity = parseInt(document.getElementById('quantity').value) || 1;
136
  const instructions = document.getElementById('special-instructions').value;
137
+ const extrasCost = 0; // For simplicity, no extras are added here
138
+ const totalCost = price * quantity + extrasCost;
139
+
140
+ cart.push({ name, quantity, instructions, totalCost });
141
+ totalCartCost += totalCost;
142
+
 
 
 
 
 
143
  updateCartButton();
 
144
  closeModal();
145
  }
146
+
147
  function updateCartButton() {
148
  const cartButton = document.getElementById('cart-button');
149
  cartButton.innerText = `View Cart (${cart.length} items)`;
150
  }
151
+
152
+ function submitCart() {
153
+ if (cart.length === 0) {
154
+ alert("Cart is empty. Please add items!");
155
+ return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
  }
157
+
158
+ let summary = "<h3>Order Summary</h3><ul>";
159
+ cart.forEach(item => {
160
+ summary += `<li>${item.name} (x${item.quantity}) - $${item.totalCost.toFixed(2)}</li>`;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
  });
162
+ summary += `</ul><h3>Total: $${totalCartCost.toFixed(2)}</h3>`;
163
+ document.getElementById('cart-summary').innerHTML = summary;
 
 
 
 
164
  }
165
  </script>
166
  """
 
176
  login_email = gr.Textbox(label="Email")
177
  login_password = gr.Textbox(label="Password", type="password")
178
  login_button = gr.Button("Login")
179
+ signup_redirect = gr.Button("Go to Signup")
180
  login_output = gr.Textbox(label="Status")
181
 
182
  with gr.Row(visible=False) as signup_page:
 
191
 
192
  with gr.Row(visible=False) as menu_page:
193
  with gr.Column():
194
+ preference = gr.Radio(["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
195
  menu_output = gr.HTML()
196
+ submit_cart_button = gr.Button("Submit Cart", elem_id="cart-button")
197
+ cart_summary = gr.HTML(elem_id="cart-summary")
 
 
 
 
 
 
 
 
198
 
199
+ # Login Button Click
200
  login_button.click(
201
+ lambda email, password: (gr.update(visible=False), gr.update(visible=True), filter_menu("All"), "Login successful!")
202
+ if login(email, password)[0] == "Login successful!" else (gr.update(), gr.update(), "Invalid email or password."),
203
+ [login_email, login_password],
204
+ [login_page, menu_page, menu_output, login_output]
205
+ )
206
+
207
+ # Signup Button Click
208
+ submit_signup.click(
209
+ lambda name, email, phone, password: (signup(name, email, phone, password), gr.update(visible=False), gr.update(visible=True)),
210
+ [signup_name, signup_email, signup_phone, signup_password],
211
+ [signup_output, signup_page, login_page]
212
+ )
213
+
214
+ # Signup Redirect
215
+ signup_redirect.click(
216
+ lambda: (gr.update(visible=False), gr.update(visible=True)),
217
+ [],
218
+ [login_page, signup_page]
219
+ )
220
+
221
+ # Login Redirect
222
+ login_redirect.click(
223
+ lambda: (gr.update(visible=True), gr.update(visible=False)),
224
+ [],
225
+ [login_page, signup_page]
226
+ )
227
+
228
+ # Submit Cart Button
229
+ submit_cart_button.click(
230
+ None,
231
+ [],
232
+ [cart_summary],
233
+ _js="submitCart()"
234
  )
 
235
 
236
  app.launch()