nagasurendra commited on
Commit
a09f414
·
verified ·
1 Parent(s): a8ea5b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -39
app.py CHANGED
@@ -150,24 +150,37 @@ 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;
162
- document.getElementById('modal-name').innerText = name;
163
- document.getElementById('modal-description').innerText = description;
164
- document.getElementById('modal-price').innerText = price;
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('$', ''));
@@ -179,43 +192,33 @@ def modal_js():
179
  }));
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
  const cartButtonTop = document.getElementById('cart-button-top');
 
 
190
  cartButtonTop.innerText = `View Cart (${cart.length} items)`;
191
  }
192
- function openCartModal() {
193
- const cartModal = document.getElementById('cart-modal');
194
- const cartItemsContainer = document.getElementById('cart-items');
195
- cartItemsContainer.innerHTML = "";
196
- cart.forEach((item, index) => {
197
- const extrasList = item.extras.map(extra => `${extra.name} (+$${extra.price.toFixed(2)})`).join(', ');
198
- cartItemsContainer.innerHTML += `
199
- <div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 10px; border-radius: 8px;">
200
- <h3>${item.name}</h3>
201
- <p>Quantity: ${item.quantity}</p>
202
- <p>Extras: ${extrasList || 'None'}</p>
203
- <p>Special Instructions: ${item.instructions || 'None'}</p>
204
- <p>Total Cost: $${item.totalCost.toFixed(2)}</p>
205
- <button onclick="removeFromCart(${index})" style="color: red;">Remove</button>
206
- </div>
207
- `;
208
- });
209
- cartModal.style.display = 'block';
210
- }
211
- function closeCartModal() {
212
- document.getElementById('cart-modal').style.display = 'none';
213
- }
214
  function removeFromCart(index) {
 
215
  cart.splice(index, 1);
216
  updateCartButton();
217
- openCartModal();
218
  }
 
219
  function proceedToCheckout() {
220
  alert("Proceeding to checkout...");
221
  }
 
150
  modal_script = """
151
  <script>
152
  let cart = [];
153
+ function openCartModal() {
154
+ const cartModal = document.getElementById('cart-modal');
155
+ const cartItemsContainer = document.getElementById('cart-items');
156
+
157
+ // Empty the current cart content and update with the latest cart items
158
+ cartItemsContainer.innerHTML = "";
159
+
160
+ // Loop through the cart and add it to the modal
161
+ cart.forEach((item, index) => {
162
+ const extrasList = item.extras.map(extra => `${extra.name} (+$${extra.price.toFixed(2)})`).join(', ');
163
+ cartItemsContainer.innerHTML += `
164
+ <div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 10px; border-radius: 8px;">
165
+ <h3>${item.name}</h3>
166
+ <p>Quantity: ${item.quantity}</p>
167
+ <p>Extras: ${extrasList || 'None'}</p>
168
+ <p>Special Instructions: ${item.instructions || 'None'}</p>
169
+ <p>Total Cost: $${item.totalCost.toFixed(2)}</p>
170
+ <button onclick="removeFromCart(${index})" style="color: red;">Remove</button>
171
+ </div>
172
+ `;
173
+ });
174
+
175
+ // Ensure modal is visible
176
+ cartModal.style.display = 'block';
177
+ cartModal.style.zIndex = 10000; // Ensure it's above other elements
178
  }
179
+
180
  function closeModal() {
181
+ document.getElementById('cart-modal').style.display = 'none';
182
  }
183
+
184
  function addToCart() {
185
  const name = document.getElementById('modal-name').innerText;
186
  const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
 
192
  }));
193
  const extrasCost = extras.reduce((total, extra) => total + extra.price, 0);
194
  const totalCost = (price + extrasCost) * quantity;
195
+
196
+ // Push the item to the cart
197
  cart.push({ name, price, quantity, extras, instructions, totalCost });
198
+
199
+ // Update the cart button text with the number of items
200
  updateCartButton();
201
+
202
+ // Close the modal after adding the item
203
  closeModal();
204
  }
205
+
206
  function updateCartButton() {
207
+ // Update both buttons to show the number of items in the cart
208
  const cartButton = document.getElementById('cart-button');
 
209
  const cartButtonTop = document.getElementById('cart-button-top');
210
+
211
+ cartButton.innerText = `View Cart (${cart.length} items)`;
212
  cartButtonTop.innerText = `View Cart (${cart.length} items)`;
213
  }
214
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215
  function removeFromCart(index) {
216
+ // Remove an item from the cart and update the cart button
217
  cart.splice(index, 1);
218
  updateCartButton();
219
+ openCartModal(); // Refresh the cart modal after removing an item
220
  }
221
+
222
  function proceedToCheckout() {
223
  alert("Proceeding to checkout...");
224
  }