nagasurendra commited on
Commit
8117a65
·
verified ·
1 Parent(s): 7a54e0d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -31
app.py CHANGED
@@ -152,13 +152,6 @@ def modal_js():
152
  <script>
153
  let cart = [];
154
  let totalCartCost = 0;
155
-
156
- // Reset add-ons to none when a new item modal is opened
157
- function resetAddOns() {
158
- const checkboxes = document.querySelectorAll('input[name="biryani-extra"]');
159
- checkboxes.forEach(checkbox => checkbox.checked = false); // Uncheck all add-ons
160
- }
161
-
162
  function openModal(name, image2, description, price) {
163
  const modal = document.getElementById('modal');
164
  modal.style.display = 'block';
@@ -175,11 +168,9 @@ def modal_js():
175
  document.getElementById('special-instructions').value = '';
176
  resetAddOns(); // Reset add-ons when opening the modal
177
  }
178
-
179
  function closeModal() {
180
  document.getElementById('modal').style.display = 'none';
181
  }
182
-
183
  function addToCart() {
184
  const name = document.getElementById('modal-name').innerText;
185
  const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
@@ -193,7 +184,6 @@ def modal_js():
193
  }));
194
  const extrasCost = extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
195
  const totalCost = (price * quantity) + extrasCost;
196
-
197
  // Add the item to the cart with its specific add-ons
198
  cart.push({ name, price, quantity, extras, instructions, totalCost });
199
  totalCartCost += totalCost; // Update the total cost of the cart
@@ -201,36 +191,32 @@ def modal_js():
201
  updateCartTotalCost(); // Update total cost displayed
202
  closeModal();
203
  }
204
-
205
  function updateCartButton() {
206
  const cartButton = document.getElementById('cart-button');
207
  cartButton.innerText = `View Cart (${cart.length} items)`;
208
  }
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} (+$${extra.price.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: ${item.quantity}</p>
220
  <p>Extras: ${extrasList || 'None'}</p>
221
  <p>Special Instructions: ${item.instructions || 'None'}</p>
222
- <p>Total Cost: $${item.totalCost.toFixed(2)}</p>
223
  <button onclick="removeFromCart(${index})" style="color: red;">Remove</button>
224
  </div>
225
  `;
226
  });
227
  cartModal.style.display = 'block';
228
  }
229
-
230
  function closeCartModal() {
231
  document.getElementById('cart-modal').style.display = 'none';
232
  }
233
-
234
  function removeFromCart(index) {
235
  totalCartCost -= cart[index].totalCost; // Deduct the cost of the removed item from total cost
236
  cart.splice(index, 1);
@@ -238,29 +224,46 @@ def modal_js():
238
  updateCartTotalCost(); // Update total cost displayed
239
  openCartModal();
240
  }
241
-
 
 
 
 
 
 
 
 
 
 
 
 
242
  function updateCartTotalCost() {
243
  const totalCostElement = document.getElementById('cart-total-cost');
244
  totalCartCost = cart.reduce((total, item) => total + item.totalCost, 0);
245
  totalCostElement.innerText = `Total Cart Cost: $${totalCartCost.toFixed(2)}`;
246
  }
247
-
248
  function proceedToCheckout() {
249
- const checkoutModal = document.getElementById('final-order-section');
250
- let finalOrderHTML = "<h1>Final Order</h1>";
251
- cart.forEach((item) => {
252
- finalOrderHTML += `
253
- <div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 10px;">
254
- <h3>${item.name} (x${item.quantity})</h3>
255
- <p>Extras: ${item.extras.map(extra => `${extra.name} (+$${(extra.price * extra.quantity).toFixed(2)})`).join(', ') || 'None'}</p>
256
- <p>Total: $${item.totalCost.toFixed(2)}</p>
 
 
 
 
257
  </div>
258
  `;
259
  });
260
- finalOrderHTML += `<h3>Total Bill: $${totalCartCost.toFixed(2)}</h3>`;
261
- checkoutModal.innerHTML = finalOrderHTML;
262
- checkoutModal.style.display = 'block';
263
- closeCartModal(); // Close cart modal
 
 
264
  }
265
  </script>
266
  """
@@ -298,6 +301,12 @@ with gr.Blocks() as app:
298
  gr.HTML(create_modal_window())
299
  gr.HTML(modal_js())
300
 
 
 
 
 
 
 
301
  login_button.click(
302
  lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!")
303
  if login(email, password)[0] == "Login successful!" else (gr.update(), gr.update(), gr.update(), "Invalid email or password."),
 
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';
 
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('$', ''));
 
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
 
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);
 
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
  """
 
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.Button("Place Your Order", onclick=modal_js().proceedToCheckout)
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."),