Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -112,22 +112,23 @@ def filter_menu(preference):
|
|
112 |
|
113 |
return html_content
|
114 |
|
115 |
-
# Function to
|
116 |
-
def
|
117 |
-
|
118 |
-
|
119 |
for item in cart:
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
<
|
126 |
-
|
127 |
-
</li>
|
128 |
"""
|
129 |
-
|
130 |
-
return
|
|
|
|
|
131 |
def create_modal_window():
|
132 |
add_ons = load_add_ons_from_salesforce()
|
133 |
add_ons_html = ""
|
@@ -159,8 +160,7 @@ def create_modal_window():
|
|
159 |
<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>
|
160 |
</div>
|
161 |
"""
|
162 |
-
return modal_html
|
163 |
-
|
164 |
|
165 |
# JavaScript for Modal and Cart
|
166 |
def modal_js():
|
@@ -187,81 +187,65 @@ def modal_js():
|
|
187 |
function closeModal() {
|
188 |
document.getElementById('modal').style.display = 'none';
|
189 |
}
|
|
|
|
|
|
|
|
|
190 |
function addToCart() {
|
191 |
const name = document.getElementById('modal-name').innerText;
|
192 |
const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
|
193 |
const quantity = parseInt(document.getElementById('quantity').value) || 1;
|
194 |
-
const
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
|
|
|
|
|
|
208 |
closeModal();
|
209 |
}
|
210 |
-
|
211 |
-
|
212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
213 |
}
|
|
|
214 |
function openCartModal() {
|
215 |
-
|
216 |
-
const cartItemsContainer = document.getElementById('cart-items');
|
217 |
-
cartItemsContainer.innerHTML = "";
|
218 |
-
cart.forEach((item, index) => {
|
219 |
-
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(', ');
|
220 |
-
cartItemsContainer.innerHTML += `
|
221 |
-
<div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 10px; border-radius: 8px;">
|
222 |
-
<h3>${item.name}</h3>
|
223 |
-
<p>Quantity: <input type="number" value="${item.quantity}" min="1" style="width: 50px;" onchange="updateCartItem(${index}, 'item', this.value)" /></p>
|
224 |
-
<p>Extras: ${extrasList || 'None'}</p>
|
225 |
-
<p>Special Instructions: ${item.instructions || 'None'}</p>
|
226 |
-
<p>Total Cost: $<span id="item-${index}-total">${item.totalCost.toFixed(2)}</span></p>
|
227 |
-
<button onclick="removeFromCart(${index})" style="color: red;">Remove</button>
|
228 |
-
</div>
|
229 |
-
`;
|
230 |
-
});
|
231 |
-
cartModal.style.display = 'block';
|
232 |
}
|
|
|
233 |
function closeCartModal() {
|
234 |
document.getElementById('cart-modal').style.display = 'none';
|
235 |
}
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
updateCartButton();
|
240 |
-
updateCartTotalCost(); // Update total cost displayed
|
241 |
-
openCartModal();
|
242 |
-
}
|
243 |
-
function updateCartItem(index, type, value) {
|
244 |
-
if (type === 'item') {
|
245 |
-
cart[index].quantity = parseInt(value);
|
246 |
-
} else if (type === 'extra') {
|
247 |
-
cart[index].extras[0].quantity = parseInt(value); // Assuming one add-on for simplicity
|
248 |
-
}
|
249 |
-
const item = cart[index];
|
250 |
-
const price = item.price;
|
251 |
-
const extrasCost = item.extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
|
252 |
-
item.totalCost = (price * item.quantity) + extrasCost;
|
253 |
-
document.getElementById(`item-${index}-total`).innerText = item.totalCost.toFixed(2);
|
254 |
-
updateCartTotalCost(); // Update total cost displayed
|
255 |
}
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
}
|
261 |
</script>
|
262 |
"""
|
263 |
return modal_script
|
264 |
-
|
265 |
# Gradio App
|
266 |
with gr.Blocks() as app:
|
267 |
with gr.Row():
|
@@ -289,11 +273,9 @@ with gr.Blocks() as app:
|
|
289 |
with gr.Column():
|
290 |
preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
|
291 |
menu_output = gr.HTML()
|
292 |
-
gr.HTML("<div id='cart-button' onclick='openCartModal()'>View Cart</div>")
|
293 |
-
gr.HTML("<div id='cart-modal' style='display: none;'>")
|
294 |
-
gr.HTML(
|
295 |
-
gr.HTML("<button onclick='submitCart()'>Proceed to Checkout</button>")
|
296 |
-
gr.HTML("</div>")
|
297 |
gr.HTML(modal_js())
|
298 |
|
299 |
login_button.click(
|
@@ -303,4 +285,4 @@ with gr.Blocks() as app:
|
|
303 |
)
|
304 |
preference.change(lambda pref: filter_menu(pref), [preference], menu_output)
|
305 |
|
306 |
-
app.launch()
|
|
|
112 |
|
113 |
return html_content
|
114 |
|
115 |
+
# Function to finalize order
|
116 |
+
def finalize_order(cart):
|
117 |
+
total_cost = sum(item['totalCost'] for item in cart)
|
118 |
+
order_details = ''
|
119 |
for item in cart:
|
120 |
+
order_details += f"""
|
121 |
+
<div>
|
122 |
+
<h3>{item['name']} (x{item['quantity']})</h3>
|
123 |
+
<p>Add-ons: {', '.join([extra['name'] for extra in item['extras']]) if item['extras'] else 'None'}</p>
|
124 |
+
<p>Special Instructions: {item['instructions']}</p>
|
125 |
+
<p>Cost: ${item['totalCost']}</p>
|
126 |
+
</div>
|
|
|
127 |
"""
|
128 |
+
order_details += f"<h2>Total Cart Cost: ${total_cost}</h2>"
|
129 |
+
return order_details
|
130 |
+
|
131 |
+
# Create Modal Window HTML
|
132 |
def create_modal_window():
|
133 |
add_ons = load_add_ons_from_salesforce()
|
134 |
add_ons_html = ""
|
|
|
160 |
<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>
|
161 |
</div>
|
162 |
"""
|
163 |
+
return modal_html
|
|
|
164 |
|
165 |
# JavaScript for Modal and Cart
|
166 |
def modal_js():
|
|
|
187 |
function closeModal() {
|
188 |
document.getElementById('modal').style.display = 'none';
|
189 |
}
|
190 |
+
function resetAddOns() {
|
191 |
+
const checkboxes = document.querySelectorAll('input[name="biryani-extra"]');
|
192 |
+
checkboxes.forEach(checkbox => checkbox.checked = false); // Uncheck all add-ons
|
193 |
+
}
|
194 |
function addToCart() {
|
195 |
const name = document.getElementById('modal-name').innerText;
|
196 |
const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
|
197 |
const quantity = parseInt(document.getElementById('quantity').value) || 1;
|
198 |
+
const extras = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked'))
|
199 |
+
.map(checkbox => ({ name: checkbox.value, price: parseFloat(checkbox.getAttribute('data-price')) }));
|
200 |
+
|
201 |
+
const specialInstructions = document.getElementById('special-instructions').value;
|
202 |
+
|
203 |
+
const item = {
|
204 |
+
name,
|
205 |
+
quantity,
|
206 |
+
extras,
|
207 |
+
instructions: specialInstructions,
|
208 |
+
totalCost: (price + extras.reduce((acc, extra) => acc + extra.price, 0)) * quantity
|
209 |
+
};
|
210 |
+
|
211 |
+
cart.push(item);
|
212 |
+
totalCartCost += item.totalCost;
|
213 |
+
|
214 |
+
updateCart();
|
215 |
closeModal();
|
216 |
}
|
217 |
+
|
218 |
+
function updateCart() {
|
219 |
+
let cartItemsHtml = '';
|
220 |
+
cart.forEach(item => {
|
221 |
+
cartItemsHtml += `<div><h3>${item.name} (x${item.quantity})</h3>
|
222 |
+
<p>Special Instructions: ${item.instructions}</p>
|
223 |
+
<p>Add-ons: ${item.extras.map(extra => extra.name).join(', ')}</p>
|
224 |
+
<p>Cost: $${item.totalCost}</p></div>`;
|
225 |
+
});
|
226 |
+
document.getElementById('cart-items').innerHTML = cartItemsHtml;
|
227 |
+
document.getElementById('cart-total-cost').innerText = 'Total Cart Cost: $' + totalCartCost.toFixed(2);
|
228 |
}
|
229 |
+
|
230 |
function openCartModal() {
|
231 |
+
document.getElementById('cart-modal').style.display = 'block';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
232 |
}
|
233 |
+
|
234 |
function closeCartModal() {
|
235 |
document.getElementById('cart-modal').style.display = 'none';
|
236 |
}
|
237 |
+
|
238 |
+
function proceedToCheckout() {
|
239 |
+
alert('Proceeding to Checkout!');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
240 |
}
|
241 |
+
// Reset all selected add-ons when opening a new item modal
|
242 |
+
function resetAddOns() {
|
243 |
+
const checkboxes = document.querySelectorAll('input[name="biryani-extra"]');
|
244 |
+
checkboxes.forEach(checkbox => checkbox.checked = false); // Uncheck all add-ons
|
245 |
}
|
246 |
</script>
|
247 |
"""
|
248 |
return modal_script
|
|
|
249 |
# Gradio App
|
250 |
with gr.Blocks() as app:
|
251 |
with gr.Row():
|
|
|
273 |
with gr.Column():
|
274 |
preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
|
275 |
menu_output = gr.HTML()
|
276 |
+
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>")
|
277 |
+
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;'>×</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>")
|
278 |
+
gr.HTML(create_modal_window())
|
|
|
|
|
279 |
gr.HTML(modal_js())
|
280 |
|
281 |
login_button.click(
|
|
|
285 |
)
|
286 |
preference.change(lambda pref: filter_menu(pref), [preference], menu_output)
|
287 |
|
288 |
+
app.launch()
|