nagasurendra commited on
Commit
8c4fa1b
·
verified ·
1 Parent(s): f086cff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -88
app.py CHANGED
@@ -63,24 +63,24 @@ modal_and_cart_js = """
63
  modal.style.display = 'block';
64
  modal.style.position = 'absolute';
65
 
 
66
  const buttonRect = event.target.getBoundingClientRect();
67
- modal.style.top = `${window.scrollY + buttonRect.top}px`;
68
- modal.style.left = `${window.scrollX + buttonRect.left}px`;
69
-
70
- if (window.innerWidth <= 768) {
71
- modal.style.width = '90%';
72
- modal.style.left = '50%';
73
- modal.style.transform = 'translateX(-50%)';
74
- } else {
75
- modal.style.width = '30%';
76
- modal.style.transform = 'none';
77
- }
78
 
 
79
  document.getElementById('modal-image').src = image;
80
  document.getElementById('modal-name').innerText = name;
81
  document.getElementById('modal-description').innerText = description;
82
  document.getElementById('modal-price').innerText = price;
83
 
 
84
  const extrasInputs = document.querySelectorAll('input[name="biryani-extra"]');
85
  extrasInputs.forEach(input => input.checked = false);
86
  document.getElementById('quantity').value = 1;
@@ -91,94 +91,38 @@ modal_and_cart_js = """
91
  document.getElementById('modal').style.display = 'none';
92
  }
93
 
94
- // Update the cart display dynamically
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  function updateCartDisplay() {
96
  let totalBill = 0;
97
  let cartHTML = "<div class='cart-container'>";
98
  cart.forEach((item, index) => {
99
  totalBill += item.itemTotal;
100
- const extras = item.extras.map((extra, i) => {
101
- const extraQuantity = item.extrasQuantities ? item.extrasQuantities[i] || 1 : 1;
102
- const extraTotal = extrasPrices[extra] * extraQuantity;
103
- totalBill += extraTotal;
104
- return `<div class='cart-item'>
105
- <span>${extra}</span>
106
- <span>Price: $${extrasPrices[extra].toFixed(2)}</span>
107
- <div class='quantity-container'>
108
- <label for='extra-quantity-${index}-${i}'>Quantity:</label>
109
- <input type='number' id='extra-quantity-${index}-${i}' value='${extraQuantity}' min='1' style='width: 50px;' onchange='updateExtraQuantity(${index}, ${i}, this.value)' />
110
- </div>
111
- <span>Total: $${extraTotal.toFixed(2)}</span>
112
- <input type='checkbox' id='extra-remove-${index}-${i}' onclick='removeExtra(${index}, ${i})'> Remove
113
- </div>`;
114
- }).join('');
115
  cartHTML += `<div class='cart-item'>
116
  <span>${item.name}</span>
117
  <span>Item Price: $${item.price.toFixed(2)}</span>
118
- <div class='quantity-container'>
119
- <label for='item-quantity-${index}'>Quantity:</label>
120
- <input type='number' id='item-quantity-${index}' value='${item.quantity}' min='1' style='width: 50px;' onchange='updateItemQuantity(${index}, this.value)' />
121
- </div>
122
- <span>Total: $${(item.price * item.quantity).toFixed(2)}</span>
123
- <input type='checkbox' id='item-remove-${index}' onclick='removeItem(${index})'> Remove
124
- </div>
125
- ${extras}
126
- <div class='cart-item'><strong>Instructions:</strong> ${item.instructions || "None"}</div>`;
127
  });
128
  cartHTML += `</div><p class='cart-total'>Total Bill: $${totalBill.toFixed(2)}</p>`;
129
- cartHTML += `<button style='margin-top: 10px; background-color: #007bff; color: white; border: none; padding: 10px; border-radius: 5px; width: 100%; cursor: pointer;' onclick='submitCart()'>Submit</button>`;
130
  document.getElementById('floating-cart').innerHTML = cartHTML;
131
  }
132
-
133
- // Additional functions for cart management (unchanged from your original code)
134
- function updateItemQuantity(index, newQuantity) {
135
- const quantity = parseInt(newQuantity) || 1;
136
- cart[index].quantity = quantity;
137
- cart[index].itemTotal = cart[index].price * quantity;
138
- updateCartDisplay();
139
- }
140
-
141
- function updateExtraQuantity(cartIndex, extraIndex, newQuantity) {
142
- const quantity = parseInt(newQuantity) || 1;
143
- cart[cartIndex].extrasQuantities = cart[cartIndex].extrasQuantities || [];
144
- cart[cartIndex].extrasQuantities[extraIndex] = quantity;
145
- updateCartDisplay();
146
- }
147
-
148
- function removeExtra(cartIndex, extraIndex) {
149
- cart[cartIndex].extras.splice(extraIndex, 1);
150
- if (cart[cartIndex].extrasQuantities) {
151
- cart[cartIndex].extrasQuantities.splice(extraIndex, 1);
152
- }
153
- updateCartDisplay();
154
- }
155
-
156
- function removeItem(index) {
157
- cart.splice(index, 1);
158
- updateCartDisplay();
159
- }
160
-
161
- function submitCart() {
162
- let finalOrderHTML = "<h3>Final Order:</h3><ul>";
163
- let totalBill = 0;
164
- cart.forEach(item => {
165
- totalBill += item.itemTotal;
166
- const extras = item.extras.map((extra, i) => {
167
- const extraQuantity = item.extrasQuantities ? item.extrasQuantities[i] || 1 : 1;
168
- const extraTotal = extrasPrices[extra] * extraQuantity;
169
- totalBill += extraTotal;
170
- return `${extra} (x${extraQuantity}) - $${extraTotal.toFixed(2)}`;
171
- }).join(', ');
172
- finalOrderHTML += `<li>
173
- ${item.name} (x${item.quantity}) - $${item.itemTotal.toFixed(2)}
174
- <br>Extras: ${extras}
175
- <br>Instructions: ${item.instructions || "None"}
176
- </li>`;
177
- });
178
- finalOrderHTML += `</ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>`;
179
- document.getElementById('final-order').innerHTML = finalOrderHTML;
180
- alert("Your final order has been submitted!");
181
- }
182
  </script>
183
  """
184
 
@@ -197,7 +141,7 @@ def app():
197
  cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")
198
  final_order_output = gr.HTML(value="", elem_id="final-order")
199
 
200
- # Modal window preserved as requested
201
  modal_window = gr.HTML("""
202
  <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;">
203
  <div style="text-align: right;">
 
63
  modal.style.display = 'block';
64
  modal.style.position = 'absolute';
65
 
66
+ // Get button position and adjust modal location
67
  const buttonRect = event.target.getBoundingClientRect();
68
+ const isSmallScreen = window.innerWidth <= 768;
69
+
70
+ modal.style.top = `${window.scrollY + buttonRect.top + (isSmallScreen ? 20 : 0)}px`;
71
+ modal.style.left = isSmallScreen
72
+ ? "50%"
73
+ : `${window.scrollX + buttonRect.left}px`;
74
+ modal.style.transform = isSmallScreen ? "translateX(-50%)" : "none";
75
+ modal.style.width = isSmallScreen ? "90%" : "30%";
 
 
 
76
 
77
+ // Update modal content
78
  document.getElementById('modal-image').src = image;
79
  document.getElementById('modal-name').innerText = name;
80
  document.getElementById('modal-description').innerText = description;
81
  document.getElementById('modal-price').innerText = price;
82
 
83
+ // Reset extras and quantity
84
  const extrasInputs = document.querySelectorAll('input[name="biryani-extra"]');
85
  extrasInputs.forEach(input => input.checked = false);
86
  document.getElementById('quantity').value = 1;
 
91
  document.getElementById('modal').style.display = 'none';
92
  }
93
 
94
+ // Functions for cart management (retained)
95
+ function addToCart() {
96
+ const name = document.getElementById('modal-name').innerText;
97
+ const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
98
+ const quantity = parseInt(document.getElementById('quantity').value) || 1;
99
+ const instructions = document.getElementById('special-instructions').value;
100
+ const extras = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked')).map(extra => extra.value);
101
+ const extrasCost = extras.reduce((sum, extra) => sum + (extrasPrices[extra] || 0), 0);
102
+ const itemTotal = (price + extrasCost) * quantity;
103
+ const cartItem = { name, price, quantity, instructions, extras, itemTotal };
104
+ cart.push(cartItem);
105
+ alert(`${name} added to cart!`);
106
+ updateCartDisplay();
107
+ closeModal();
108
+ }
109
+
110
  function updateCartDisplay() {
111
  let totalBill = 0;
112
  let cartHTML = "<div class='cart-container'>";
113
  cart.forEach((item, index) => {
114
  totalBill += item.itemTotal;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  cartHTML += `<div class='cart-item'>
116
  <span>${item.name}</span>
117
  <span>Item Price: $${item.price.toFixed(2)}</span>
118
+ <span>Quantity: ${item.quantity}</span>
119
+ <span>Total: $${item.itemTotal.toFixed(2)}</span>
120
+ <div><strong>Instructions:</strong> ${item.instructions || "None"}</div>
121
+ </div>`;
 
 
 
 
 
122
  });
123
  cartHTML += `</div><p class='cart-total'>Total Bill: $${totalBill.toFixed(2)}</p>`;
 
124
  document.getElementById('floating-cart').innerHTML = cartHTML;
125
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  </script>
127
  """
128
 
 
141
  cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")
142
  final_order_output = gr.HTML(value="", elem_id="final-order")
143
 
144
+ # Modal window retained as requested
145
  modal_window = gr.HTML("""
146
  <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;">
147
  <div style="text-align: right;">