Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -112,40 +112,14 @@ def filter_menu(preference):
|
|
112 |
|
113 |
return html_content
|
114 |
|
115 |
-
#
|
116 |
-
def save_cart_to_salesforce(cart):
|
117 |
-
try:
|
118 |
-
# Loop through each cart item and create a new Order__c record
|
119 |
-
for item in cart:
|
120 |
-
order_data = {
|
121 |
-
'Name2__c': item['name'], # Custom name for the order (can be the item name or order ID)
|
122 |
-
'Item_Name__c': item['name'], # Name of the item in the order
|
123 |
-
'Quantity__c': item['quantity'], # Quantity of the item
|
124 |
-
'Price__c': item['price'], # Price of the item
|
125 |
-
'Total_Cost__c': item['totalCost'], # Total cost for the item
|
126 |
-
'Special_Instructions__c': item['instructions'], # Special instructions (if any)
|
127 |
-
}
|
128 |
-
|
129 |
-
# Handle add-ons if available
|
130 |
-
if item['extras']:
|
131 |
-
add_ons = ", ".join([add_on['name'] for add_on in item['extras']])
|
132 |
-
order_data['Add_Ons__c'] = add_ons # Add-ons associated with the item
|
133 |
-
|
134 |
-
# Create the order in Salesforce
|
135 |
-
sf.Order__c.create(order_data)
|
136 |
-
|
137 |
-
return "Cart saved successfully to Salesforce!"
|
138 |
-
except Exception as e:
|
139 |
-
return f"Error while saving cart to Salesforce: {str(e)}"
|
140 |
-
|
141 |
-
# Function to create the modal window with add-ons
|
142 |
def create_modal_window():
|
143 |
add_ons = load_add_ons_from_salesforce()
|
144 |
add_ons_html = ""
|
145 |
for add_on in add_ons:
|
146 |
add_ons_html += f"""
|
147 |
<label>
|
148 |
-
<input type="checkbox" name="biryani-extra" value="{add_on['Name']}" data-price="{add_on['Price__c']}"
|
149 |
{add_on['Name']} + ${add_on['Price__c']}
|
150 |
</label>
|
151 |
<br>
|
@@ -206,14 +180,14 @@ def modal_js():
|
|
206 |
const extras = selectedAddOns.map(extra => ({
|
207 |
name: extra.value,
|
208 |
price: parseFloat(extra.getAttribute('data-price')),
|
|
|
209 |
}));
|
210 |
-
const extrasCost = extras.reduce((total, extra) => total + (extra.price *
|
211 |
const totalCost = (price * quantity) + extrasCost;
|
212 |
-
// Add the item to the cart with its specific add-ons
|
213 |
cart.push({ name, price, quantity, extras, instructions, totalCost });
|
214 |
-
totalCartCost += totalCost;
|
215 |
updateCartButton();
|
216 |
-
updateCartTotalCost();
|
217 |
closeModal();
|
218 |
}
|
219 |
function updateCartButton() {
|
@@ -225,7 +199,7 @@ def modal_js():
|
|
225 |
const cartItemsContainer = document.getElementById('cart-items');
|
226 |
cartItemsContainer.innerHTML = "";
|
227 |
cart.forEach((item, index) => {
|
228 |
-
const extrasList = item.extras.map(extra => `${extra.name} (+$${extra.price})`).join(', ');
|
229 |
cartItemsContainer.innerHTML += `
|
230 |
<div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 10px; border-radius: 8px;">
|
231 |
<h3>${item.name}</h3>
|
@@ -243,22 +217,24 @@ def modal_js():
|
|
243 |
document.getElementById('cart-modal').style.display = 'none';
|
244 |
}
|
245 |
function removeFromCart(index) {
|
246 |
-
totalCartCost -= cart[index].totalCost;
|
247 |
cart.splice(index, 1);
|
248 |
updateCartButton();
|
249 |
-
updateCartTotalCost();
|
250 |
openCartModal();
|
251 |
}
|
252 |
function updateCartItem(index, type, value) {
|
253 |
if (type === 'item') {
|
254 |
cart[index].quantity = parseInt(value);
|
|
|
|
|
255 |
}
|
256 |
const item = cart[index];
|
257 |
const price = item.price;
|
258 |
-
const extrasCost = item.extras.reduce((total, extra) => total + (extra.price *
|
259 |
item.totalCost = (price * item.quantity) + extrasCost;
|
260 |
document.getElementById(`item-${index}-total`).innerText = item.totalCost.toFixed(2);
|
261 |
-
updateCartTotalCost();
|
262 |
}
|
263 |
function updateCartTotalCost() {
|
264 |
const totalCostElement = document.getElementById('cart-total-cost');
|
@@ -266,14 +242,11 @@ def modal_js():
|
|
266 |
totalCostElement.innerText = `Total Cart Cost: $${totalCartCost.toFixed(2)}`;
|
267 |
}
|
268 |
function proceedToCheckout() {
|
269 |
-
|
270 |
-
save_cart_to_salesforce(cart);
|
271 |
-
alert("Proceeding to checkout...");
|
272 |
}
|
273 |
-
// Reset all selected add-ons when opening a new item modal
|
274 |
function resetAddOns() {
|
275 |
const checkboxes = document.querySelectorAll('input[name="biryani-extra"]');
|
276 |
-
checkboxes.forEach(checkbox => checkbox.checked =
|
277 |
}
|
278 |
</script>
|
279 |
"""
|
@@ -284,6 +257,7 @@ with gr.Blocks() as app:
|
|
284 |
with gr.Row():
|
285 |
gr.HTML("<h1 style='text-align: center;'>Welcome to Biryani Hub</h1>")
|
286 |
|
|
|
287 |
with gr.Row(visible=True) as login_page:
|
288 |
with gr.Column():
|
289 |
login_email = gr.Textbox(label="Email")
|
@@ -292,6 +266,7 @@ with gr.Blocks() as app:
|
|
292 |
signup_button = gr.Button("Go to Signup")
|
293 |
login_output = gr.Textbox(label="Status")
|
294 |
|
|
|
295 |
with gr.Row(visible=False) as signup_page:
|
296 |
with gr.Column():
|
297 |
signup_name = gr.Textbox(label="Name")
|
@@ -302,20 +277,30 @@ with gr.Blocks() as app:
|
|
302 |
login_redirect = gr.Button("Go to Login")
|
303 |
signup_output = gr.Textbox(label="Status")
|
304 |
|
|
|
305 |
with gr.Row(visible=False) as menu_page:
|
306 |
with gr.Column():
|
307 |
preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
|
308 |
menu_output = gr.HTML()
|
309 |
-
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>")
|
310 |
-
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>")
|
311 |
gr.HTML(create_modal_window())
|
312 |
gr.HTML(modal_js())
|
313 |
|
|
|
|
|
|
|
|
|
|
|
|
|
314 |
login_button.click(
|
315 |
lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!")
|
316 |
if login(email, password)[0] == "Login successful!" else (gr.update(), gr.update(), gr.update(), "Invalid email or password."),
|
317 |
[login_email, login_password], [login_page, menu_page, menu_output, login_output]
|
318 |
)
|
|
|
|
|
|
|
|
|
|
|
319 |
preference.change(lambda pref: filter_menu(pref), [preference], menu_output)
|
320 |
|
321 |
-
app.launch()
|
|
|
112 |
|
113 |
return html_content
|
114 |
|
115 |
+
# Create Modal Window HTML
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
def create_modal_window():
|
117 |
add_ons = load_add_ons_from_salesforce()
|
118 |
add_ons_html = ""
|
119 |
for add_on in add_ons:
|
120 |
add_ons_html += f"""
|
121 |
<label>
|
122 |
+
<input type="checkbox" name="biryani-extra" value="{add_on['Name']}" data-price="{add_on['Price__c']}" />
|
123 |
{add_on['Name']} + ${add_on['Price__c']}
|
124 |
</label>
|
125 |
<br>
|
|
|
180 |
const extras = selectedAddOns.map(extra => ({
|
181 |
name: extra.value,
|
182 |
price: parseFloat(extra.getAttribute('data-price')),
|
183 |
+
quantity: 1 // Default quantity for add-ons is 1
|
184 |
}));
|
185 |
+
const extrasCost = extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
|
186 |
const totalCost = (price * quantity) + extrasCost;
|
|
|
187 |
cart.push({ name, price, quantity, extras, instructions, totalCost });
|
188 |
+
totalCartCost += totalCost;
|
189 |
updateCartButton();
|
190 |
+
updateCartTotalCost();
|
191 |
closeModal();
|
192 |
}
|
193 |
function updateCartButton() {
|
|
|
199 |
const cartItemsContainer = document.getElementById('cart-items');
|
200 |
cartItemsContainer.innerHTML = "";
|
201 |
cart.forEach((item, index) => {
|
202 |
+
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(', ');
|
203 |
cartItemsContainer.innerHTML += `
|
204 |
<div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 10px; border-radius: 8px;">
|
205 |
<h3>${item.name}</h3>
|
|
|
217 |
document.getElementById('cart-modal').style.display = 'none';
|
218 |
}
|
219 |
function removeFromCart(index) {
|
220 |
+
totalCartCost -= cart[index].totalCost;
|
221 |
cart.splice(index, 1);
|
222 |
updateCartButton();
|
223 |
+
updateCartTotalCost();
|
224 |
openCartModal();
|
225 |
}
|
226 |
function updateCartItem(index, type, value) {
|
227 |
if (type === 'item') {
|
228 |
cart[index].quantity = parseInt(value);
|
229 |
+
} else if (type === 'extra') {
|
230 |
+
cart[index].extras[0].quantity = parseInt(value);
|
231 |
}
|
232 |
const item = cart[index];
|
233 |
const price = item.price;
|
234 |
+
const extrasCost = item.extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
|
235 |
item.totalCost = (price * item.quantity) + extrasCost;
|
236 |
document.getElementById(`item-${index}-total`).innerText = item.totalCost.toFixed(2);
|
237 |
+
updateCartTotalCost();
|
238 |
}
|
239 |
function updateCartTotalCost() {
|
240 |
const totalCostElement = document.getElementById('cart-total-cost');
|
|
|
242 |
totalCostElement.innerText = `Total Cart Cost: $${totalCartCost.toFixed(2)}`;
|
243 |
}
|
244 |
function proceedToCheckout() {
|
245 |
+
window.location.href = '/final-order-page'; // Redirect to final order page
|
|
|
|
|
246 |
}
|
|
|
247 |
function resetAddOns() {
|
248 |
const checkboxes = document.querySelectorAll('input[name="biryani-extra"]');
|
249 |
+
checkboxes.forEach(checkbox => checkbox.checked = false);
|
250 |
}
|
251 |
</script>
|
252 |
"""
|
|
|
257 |
with gr.Row():
|
258 |
gr.HTML("<h1 style='text-align: center;'>Welcome to Biryani Hub</h1>")
|
259 |
|
260 |
+
# Login Page
|
261 |
with gr.Row(visible=True) as login_page:
|
262 |
with gr.Column():
|
263 |
login_email = gr.Textbox(label="Email")
|
|
|
266 |
signup_button = gr.Button("Go to Signup")
|
267 |
login_output = gr.Textbox(label="Status")
|
268 |
|
269 |
+
# Signup Page
|
270 |
with gr.Row(visible=False) as signup_page:
|
271 |
with gr.Column():
|
272 |
signup_name = gr.Textbox(label="Name")
|
|
|
277 |
login_redirect = gr.Button("Go to Login")
|
278 |
signup_output = gr.Textbox(label="Status")
|
279 |
|
280 |
+
# Menu Page
|
281 |
with gr.Row(visible=False) as menu_page:
|
282 |
with gr.Column():
|
283 |
preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
|
284 |
menu_output = gr.HTML()
|
|
|
|
|
285 |
gr.HTML(create_modal_window())
|
286 |
gr.HTML(modal_js())
|
287 |
|
288 |
+
# Final Order Page
|
289 |
+
with gr.Row(visible=False) as final_order_page:
|
290 |
+
with gr.Column():
|
291 |
+
final_order_output = gr.HTML()
|
292 |
+
|
293 |
+
# Login button action
|
294 |
login_button.click(
|
295 |
lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!")
|
296 |
if login(email, password)[0] == "Login successful!" else (gr.update(), gr.update(), gr.update(), "Invalid email or password."),
|
297 |
[login_email, login_password], [login_page, menu_page, menu_output, login_output]
|
298 |
)
|
299 |
+
|
300 |
+
# Signup button action
|
301 |
+
signup_button.click(lambda: gr.update(visible=False), [], login_page)
|
302 |
+
|
303 |
+
# Menu preference change
|
304 |
preference.change(lambda pref: filter_menu(pref), [preference], menu_output)
|
305 |
|
306 |
+
app.launch()
|