nagasurendra commited on
Commit
282dad1
·
verified ·
1 Parent(s): 32f38e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -23
app.py CHANGED
@@ -145,18 +145,17 @@ def create_modal_window():
145
  """
146
  return modal_html
147
 
148
- # JavaScript for Modal Functionality
149
  def modal_js():
150
  modal_script = """
151
  <script>
152
  let cart = [];
153
-
154
  function openModal(name, image2, description, price) {
155
  const modal = document.getElementById('modal');
156
  modal.style.display = 'block';
157
  modal.style.position = 'fixed';
158
  modal.style.width = window.innerWidth <= 768 ? '90%' : '30%';
159
- modal.style.top = ${event.clientY}px;
160
  modal.style.left = '50%';
161
  modal.style.transform = 'translate(-50%, -50%)';
162
  document.getElementById('modal-image').src = image2;
@@ -166,11 +165,9 @@ def modal_js():
166
  document.getElementById('quantity').value = 1;
167
  document.getElementById('special-instructions').value = '';
168
  }
169
-
170
  function closeModal() {
171
  document.getElementById('modal').style.display = 'none';
172
  }
173
-
174
  function addToCart() {
175
  const name = document.getElementById('modal-name').innerText;
176
  const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
@@ -183,16 +180,20 @@ def modal_js():
183
  const extrasCost = extras.reduce((total, extra) => total + extra.price, 0);
184
  const totalCost = (price + extrasCost) * quantity;
185
  cart.push({ name, price, quantity, extras, instructions, totalCost });
186
- updateCartUI();
187
  closeModal();
188
  }
189
-
190
- function updateCartUI() {
191
- const cartContainer = document.getElementById('cart-container');
192
- cartContainer.innerHTML = "";
 
 
 
 
193
  cart.forEach((item, index) => {
194
- const extrasList = item.extras.map(extra => ${extra.name} (+$${extra.price.toFixed(2)})).join(', ');
195
- cartContainer.innerHTML +=
196
  <div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 10px; border-radius: 8px;">
197
  <h3>${item.name}</h3>
198
  <p>Quantity: ${item.quantity}</p>
@@ -201,19 +202,25 @@ def modal_js():
201
  <p>Total Cost: $${item.totalCost.toFixed(2)}</p>
202
  <button onclick="removeFromCart(${index})" style="color: red;">Remove</button>
203
  </div>
204
- ;
205
  });
 
 
 
 
206
  }
207
-
208
  function removeFromCart(index) {
209
  cart.splice(index, 1);
210
- updateCartUI();
 
 
 
 
211
  }
212
  </script>
213
  """
214
  return modal_script
215
 
216
-
217
  # Gradio App
218
  with gr.Blocks() as app:
219
  with gr.Row():
@@ -240,14 +247,12 @@ with gr.Blocks() as app:
240
  with gr.Row(visible=False) as menu_page:
241
  with gr.Column():
242
  preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
243
-
244
- # Duplicate View Cart at the top
245
- gr.HTML("<div id='cart-container-top' style='margin-top: 10px; padding: 10px; border: 1px solid #ddd; border-radius: 8px; background-color: #f9f9f9;'>Your cart is empty.</div>")
246
-
247
  menu_output = gr.HTML()
248
- gr.HTML("<div id='cart-container' style='margin-top: 20px; padding: 10px; border: 1px solid #ddd; border-radius: 8px; background-color: #f9f9f9;'>Your cart is empty.</div>")
249
- gr.HTML(create_modal_window()) # Add modal window HTML
250
- gr.HTML(modal_js()) # Add modal JavaScript
 
 
251
 
252
  login_button.click(
253
  lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!")
 
145
  """
146
  return modal_html
147
 
148
+ # JavaScript for Modal and Cart
149
  def modal_js():
150
  modal_script = """
151
  <script>
152
  let cart = [];
 
153
  function openModal(name, image2, description, price) {
154
  const modal = document.getElementById('modal');
155
  modal.style.display = 'block';
156
  modal.style.position = 'fixed';
157
  modal.style.width = window.innerWidth <= 768 ? '90%' : '30%';
158
+ modal.style.top = `${event.clientY}px`;
159
  modal.style.left = '50%';
160
  modal.style.transform = 'translate(-50%, -50%)';
161
  document.getElementById('modal-image').src = image2;
 
165
  document.getElementById('quantity').value = 1;
166
  document.getElementById('special-instructions').value = '';
167
  }
 
168
  function closeModal() {
169
  document.getElementById('modal').style.display = 'none';
170
  }
 
171
  function addToCart() {
172
  const name = document.getElementById('modal-name').innerText;
173
  const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
 
180
  const extrasCost = extras.reduce((total, extra) => total + extra.price, 0);
181
  const totalCost = (price + extrasCost) * quantity;
182
  cart.push({ name, price, quantity, extras, instructions, totalCost });
183
+ updateCartButton();
184
  closeModal();
185
  }
186
+ function updateCartButton() {
187
+ const cartButton = document.getElementById('cart-button');
188
+ cartButton.innerText = `View Cart (${cart.length} items)`;
189
+ }
190
+ function openCartModal() {
191
+ const cartModal = document.getElementById('cart-modal');
192
+ const cartItemsContainer = document.getElementById('cart-items');
193
+ cartItemsContainer.innerHTML = "";
194
  cart.forEach((item, index) => {
195
+ const extrasList = item.extras.map(extra => `${extra.name} (+$${extra.price.toFixed(2)})`).join(', ');
196
+ cartItemsContainer.innerHTML += `
197
  <div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 10px; border-radius: 8px;">
198
  <h3>${item.name}</h3>
199
  <p>Quantity: ${item.quantity}</p>
 
202
  <p>Total Cost: $${item.totalCost.toFixed(2)}</p>
203
  <button onclick="removeFromCart(${index})" style="color: red;">Remove</button>
204
  </div>
205
+ `;
206
  });
207
+ cartModal.style.display = 'block';
208
+ }
209
+ function closeCartModal() {
210
+ document.getElementById('cart-modal').style.display = 'none';
211
  }
 
212
  function removeFromCart(index) {
213
  cart.splice(index, 1);
214
+ updateCartButton();
215
+ openCartModal();
216
+ }
217
+ function proceedToCheckout() {
218
+ alert("Proceeding to checkout...");
219
  }
220
  </script>
221
  """
222
  return modal_script
223
 
 
224
  # Gradio App
225
  with gr.Blocks() as app:
226
  with gr.Row():
 
247
  with gr.Row(visible=False) as menu_page:
248
  with gr.Column():
249
  preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
 
 
 
 
250
  menu_output = gr.HTML()
251
+ <div id='cart-button-top' style='position: fixed; top: 20px; left: 20px; background: #28a745; color: white; padding: 10px 20px; border-radius: 30px; cursor: pointer; z-index: 1000;' onclick='openCartModal()'>View Cart</div>
252
+ gr.HTML("<div id='cart-button' style='position: fixed; bottom: 20px; right: 20px; background: #28a745; color: white; padding: 10px 20px; border-radius: 30px; cursor: pointer; z-index: 1000;' onclick='openCartModal()'>View Cart</div>")
253
+ 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><button style='background: #ff5722; color: white; padding: 10px 20px; border-radius: 5px; border: none; cursor: pointer;' onclick='proceedToCheckout()'>Proceed to Checkout</button></div></div>")
254
+ gr.HTML(create_modal_window())
255
+ gr.HTML(modal_js())
256
 
257
  login_button.click(
258
  lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!")