Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -122,8 +122,6 @@ def store_cart_in_salesforce(cart_summary, total_cost):
|
|
122 |
return "Order saved successfully in Salesforce."
|
123 |
except Exception as e:
|
124 |
return f"Error saving order in Salesforce: {str(e)}"
|
125 |
-
|
126 |
-
# Function to create modal window HTML
|
127 |
def create_modal_window():
|
128 |
add_ons = load_add_ons_from_salesforce()
|
129 |
add_ons_html = ""
|
@@ -172,12 +170,14 @@ def create_modal_window():
|
|
172 |
</div>
|
173 |
"""
|
174 |
return modal_html
|
|
|
175 |
# JavaScript for Modal and Cart
|
176 |
def modal_js():
|
177 |
modal_script = """
|
178 |
<script>
|
179 |
let cart = [];
|
180 |
let totalCartCost = 0;
|
|
|
181 |
function openModal(name, image2, description, price) {
|
182 |
const modal = document.getElementById('modal');
|
183 |
modal.style.display = 'block';
|
@@ -192,11 +192,13 @@ def modal_js():
|
|
192 |
document.getElementById('modal-price').innerText = price;
|
193 |
document.getElementById('quantity').value = 1;
|
194 |
document.getElementById('special-instructions').value = '';
|
195 |
-
resetAddOns();
|
196 |
}
|
|
|
197 |
function closeModal() {
|
198 |
document.getElementById('modal').style.display = 'none';
|
199 |
}
|
|
|
200 |
function addToCart() {
|
201 |
const name = document.getElementById('modal-name').innerText;
|
202 |
const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
|
@@ -206,21 +208,22 @@ def modal_js():
|
|
206 |
const extras = selectedAddOns.map(extra => ({
|
207 |
name: extra.value,
|
208 |
price: parseFloat(extra.getAttribute('data-price')),
|
209 |
-
quantity: 1
|
210 |
}));
|
211 |
const extrasCost = extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
|
212 |
const totalCost = (price * quantity) + extrasCost;
|
213 |
-
// Add the item to the cart with its specific add-ons
|
214 |
cart.push({ name, price, quantity, extras, instructions, totalCost });
|
215 |
-
totalCartCost += totalCost;
|
216 |
updateCartButton();
|
217 |
-
updateCartTotalCost();
|
218 |
closeModal();
|
219 |
}
|
|
|
220 |
function updateCartButton() {
|
221 |
const cartButton = document.getElementById('cart-button');
|
222 |
cartButton.innerText = `View Cart (${cart.length} items)`;
|
223 |
}
|
|
|
224 |
function openCartModal() {
|
225 |
const cartModal = document.getElementById('cart-modal');
|
226 |
const cartItemsContainer = document.getElementById('cart-items');
|
@@ -240,69 +243,60 @@ def modal_js():
|
|
240 |
});
|
241 |
cartModal.style.display = 'block';
|
242 |
}
|
|
|
243 |
function closeCartModal() {
|
244 |
document.getElementById('cart-modal').style.display = 'none';
|
245 |
}
|
|
|
246 |
function removeFromCart(index) {
|
247 |
-
totalCartCost -= cart[index].totalCost;
|
248 |
cart.splice(index, 1);
|
249 |
updateCartButton();
|
250 |
-
updateCartTotalCost();
|
251 |
openCartModal();
|
252 |
}
|
|
|
253 |
function updateCartItem(index, type, value) {
|
254 |
if (type === 'item') {
|
255 |
cart[index].quantity = parseInt(value);
|
256 |
} else if (type === 'extra') {
|
257 |
-
cart[index].extras[0].quantity = parseInt(value);
|
258 |
}
|
259 |
const item = cart[index];
|
260 |
const price = item.price;
|
261 |
const extrasCost = item.extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
|
262 |
item.totalCost = (price * item.quantity) + extrasCost;
|
263 |
document.getElementById(`item-${index}-total`).innerText = item.totalCost.toFixed(2);
|
264 |
-
updateCartTotalCost();
|
265 |
}
|
|
|
266 |
function updateCartTotalCost() {
|
267 |
const totalCostElement = document.getElementById('cart-total-cost');
|
268 |
totalCartCost = cart.reduce((total, item) => total + item.totalCost, 0);
|
269 |
totalCostElement.innerText = `Total Cart Cost: $${totalCartCost.toFixed(2)}`;
|
270 |
}
|
|
|
271 |
function proceedToCheckout() {
|
272 |
const cartSummary = cart.map(item => `
|
273 |
${item.name} (x${item.quantity}) - $${item.totalCost.toFixed(2)}
|
274 |
Extras: ${item.extras.map(extra => extra.name).join(', ') || 'None'}
|
275 |
Instructions: ${item.instructions || 'None'}
|
276 |
`).join('\n');
|
277 |
-
|
278 |
const totalBill = totalCartCost.toFixed(2);
|
279 |
|
280 |
-
// Call Python function to store data in Salesforce
|
281 |
fetch('/run_proceed_checkout', {
|
282 |
method: 'POST',
|
283 |
-
headers: {
|
284 |
-
'Content-Type': 'application/json'
|
285 |
-
},
|
286 |
body: JSON.stringify({ cartSummary, totalBill })
|
287 |
-
})
|
288 |
-
.then(response => response.json())
|
289 |
-
.then(data => {
|
290 |
alert(data.message);
|
291 |
-
})
|
292 |
-
.catch(error => {
|
293 |
alert("Error during checkout: " + error);
|
294 |
});
|
295 |
}
|
296 |
-
// Reset all selected add-ons when opening a new item modal
|
297 |
-
function resetAddOns() {
|
298 |
-
const checkboxes = document.querySelectorAll('input[name="biryani-extra"]');
|
299 |
-
checkboxes.forEach(checkbox => checkbox.checked = false); // Uncheck all add-ons
|
300 |
-
}
|
301 |
</script>
|
302 |
"""
|
303 |
return modal_script
|
304 |
-
# JavaScript for Modal and Cart
|
305 |
-
|
306 |
|
307 |
# Gradio App
|
308 |
with gr.Blocks() as app:
|
@@ -333,19 +327,19 @@ with gr.Blocks() as app:
|
|
333 |
menu_output = gr.HTML()
|
334 |
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>")
|
335 |
gr.HTML(create_modal_window())
|
336 |
-
gr.HTML(modal_js)
|
337 |
|
338 |
login_button.click(
|
339 |
lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!")
|
340 |
if login(email, password)[0] == "Login successful!" else (gr.update(), gr.update(), gr.update(), "Invalid email or password."),
|
341 |
[login_email, login_password], [login_page, menu_page, menu_output, login_output]
|
342 |
)
|
|
|
343 |
submit_signup.click(
|
344 |
lambda name, email, phone, password: signup(name, email, phone, password),
|
345 |
inputs=[signup_name, signup_email, signup_phone, signup_password],
|
346 |
outputs=signup_output
|
347 |
)
|
348 |
-
|
349 |
signup_button.click(
|
350 |
lambda: (gr.update(visible=False), gr.update(visible=True)),
|
351 |
inputs=[],
|
|
|
122 |
return "Order saved successfully in Salesforce."
|
123 |
except Exception as e:
|
124 |
return f"Error saving order in Salesforce: {str(e)}"
|
|
|
|
|
125 |
def create_modal_window():
|
126 |
add_ons = load_add_ons_from_salesforce()
|
127 |
add_ons_html = ""
|
|
|
170 |
</div>
|
171 |
"""
|
172 |
return modal_html
|
173 |
+
|
174 |
# JavaScript for Modal and Cart
|
175 |
def modal_js():
|
176 |
modal_script = """
|
177 |
<script>
|
178 |
let cart = [];
|
179 |
let totalCartCost = 0;
|
180 |
+
|
181 |
function openModal(name, image2, description, price) {
|
182 |
const modal = document.getElementById('modal');
|
183 |
modal.style.display = 'block';
|
|
|
192 |
document.getElementById('modal-price').innerText = price;
|
193 |
document.getElementById('quantity').value = 1;
|
194 |
document.getElementById('special-instructions').value = '';
|
195 |
+
resetAddOns();
|
196 |
}
|
197 |
+
|
198 |
function closeModal() {
|
199 |
document.getElementById('modal').style.display = 'none';
|
200 |
}
|
201 |
+
|
202 |
function addToCart() {
|
203 |
const name = document.getElementById('modal-name').innerText;
|
204 |
const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
|
|
|
208 |
const extras = selectedAddOns.map(extra => ({
|
209 |
name: extra.value,
|
210 |
price: parseFloat(extra.getAttribute('data-price')),
|
211 |
+
quantity: 1
|
212 |
}));
|
213 |
const extrasCost = extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
|
214 |
const totalCost = (price * quantity) + extrasCost;
|
|
|
215 |
cart.push({ name, price, quantity, extras, instructions, totalCost });
|
216 |
+
totalCartCost += totalCost;
|
217 |
updateCartButton();
|
218 |
+
updateCartTotalCost();
|
219 |
closeModal();
|
220 |
}
|
221 |
+
|
222 |
function updateCartButton() {
|
223 |
const cartButton = document.getElementById('cart-button');
|
224 |
cartButton.innerText = `View Cart (${cart.length} items)`;
|
225 |
}
|
226 |
+
|
227 |
function openCartModal() {
|
228 |
const cartModal = document.getElementById('cart-modal');
|
229 |
const cartItemsContainer = document.getElementById('cart-items');
|
|
|
243 |
});
|
244 |
cartModal.style.display = 'block';
|
245 |
}
|
246 |
+
|
247 |
function closeCartModal() {
|
248 |
document.getElementById('cart-modal').style.display = 'none';
|
249 |
}
|
250 |
+
|
251 |
function removeFromCart(index) {
|
252 |
+
totalCartCost -= cart[index].totalCost;
|
253 |
cart.splice(index, 1);
|
254 |
updateCartButton();
|
255 |
+
updateCartTotalCost();
|
256 |
openCartModal();
|
257 |
}
|
258 |
+
|
259 |
function updateCartItem(index, type, value) {
|
260 |
if (type === 'item') {
|
261 |
cart[index].quantity = parseInt(value);
|
262 |
} else if (type === 'extra') {
|
263 |
+
cart[index].extras[0].quantity = parseInt(value);
|
264 |
}
|
265 |
const item = cart[index];
|
266 |
const price = item.price;
|
267 |
const extrasCost = item.extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
|
268 |
item.totalCost = (price * item.quantity) + extrasCost;
|
269 |
document.getElementById(`item-${index}-total`).innerText = item.totalCost.toFixed(2);
|
270 |
+
updateCartTotalCost();
|
271 |
}
|
272 |
+
|
273 |
function updateCartTotalCost() {
|
274 |
const totalCostElement = document.getElementById('cart-total-cost');
|
275 |
totalCartCost = cart.reduce((total, item) => total + item.totalCost, 0);
|
276 |
totalCostElement.innerText = `Total Cart Cost: $${totalCartCost.toFixed(2)}`;
|
277 |
}
|
278 |
+
|
279 |
function proceedToCheckout() {
|
280 |
const cartSummary = cart.map(item => `
|
281 |
${item.name} (x${item.quantity}) - $${item.totalCost.toFixed(2)}
|
282 |
Extras: ${item.extras.map(extra => extra.name).join(', ') || 'None'}
|
283 |
Instructions: ${item.instructions || 'None'}
|
284 |
`).join('\n');
|
|
|
285 |
const totalBill = totalCartCost.toFixed(2);
|
286 |
|
|
|
287 |
fetch('/run_proceed_checkout', {
|
288 |
method: 'POST',
|
289 |
+
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
290 |
body: JSON.stringify({ cartSummary, totalBill })
|
291 |
+
}).then(response => response.json()).then(data => {
|
|
|
|
|
292 |
alert(data.message);
|
293 |
+
}).catch(error => {
|
|
|
294 |
alert("Error during checkout: " + error);
|
295 |
});
|
296 |
}
|
|
|
|
|
|
|
|
|
|
|
297 |
</script>
|
298 |
"""
|
299 |
return modal_script
|
|
|
|
|
300 |
|
301 |
# Gradio App
|
302 |
with gr.Blocks() as app:
|
|
|
327 |
menu_output = gr.HTML()
|
328 |
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>")
|
329 |
gr.HTML(create_modal_window())
|
330 |
+
gr.HTML(modal_js())
|
331 |
|
332 |
login_button.click(
|
333 |
lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!")
|
334 |
if login(email, password)[0] == "Login successful!" else (gr.update(), gr.update(), gr.update(), "Invalid email or password."),
|
335 |
[login_email, login_password], [login_page, menu_page, menu_output, login_output]
|
336 |
)
|
337 |
+
|
338 |
submit_signup.click(
|
339 |
lambda name, email, phone, password: signup(name, email, phone, password),
|
340 |
inputs=[signup_name, signup_email, signup_phone, signup_password],
|
341 |
outputs=signup_output
|
342 |
)
|
|
|
343 |
signup_button.click(
|
344 |
lambda: (gr.update(visible=False), gr.update(visible=True)),
|
345 |
inputs=[],
|