Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -76,6 +76,7 @@ def load_add_ons_from_salesforce():
|
|
76 |
# Function to filter menu items
|
77 |
def filter_menu(preference):
|
78 |
menu_data = load_menu_from_salesforce()
|
|
|
79 |
filtered_data = {}
|
80 |
for item in menu_data:
|
81 |
if "Section__c" not in item or "Veg_NonVeg__c" not in item:
|
@@ -111,7 +112,22 @@ def filter_menu(preference):
|
|
111 |
|
112 |
return html_content
|
113 |
|
114 |
-
# Function to
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
def create_modal_window():
|
116 |
add_ons = load_add_ons_from_salesforce()
|
117 |
add_ons_html = ""
|
@@ -145,7 +161,8 @@ def create_modal_window():
|
|
145 |
"""
|
146 |
return modal_html
|
147 |
|
148 |
-
|
|
|
149 |
def modal_js():
|
150 |
modal_script = """
|
151 |
<script>
|
@@ -165,6 +182,7 @@ def modal_js():
|
|
165 |
document.getElementById('modal-price').innerText = price;
|
166 |
document.getElementById('quantity').value = 1;
|
167 |
document.getElementById('special-instructions').value = '';
|
|
|
168 |
}
|
169 |
function closeModal() {
|
170 |
document.getElementById('modal').style.display = 'none';
|
@@ -174,27 +192,71 @@ def modal_js():
|
|
174 |
const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
|
175 |
const quantity = parseInt(document.getElementById('quantity').value) || 1;
|
176 |
const instructions = document.getElementById('special-instructions').value;
|
177 |
-
const
|
178 |
-
|
179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
updateCartButton();
|
|
|
181 |
closeModal();
|
182 |
}
|
183 |
function updateCartButton() {
|
184 |
const cartButton = document.getElementById('cart-button');
|
185 |
cartButton.innerText = `View Cart (${cart.length} items)`;
|
186 |
}
|
187 |
-
function
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
});
|
196 |
-
|
197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
}
|
199 |
</script>
|
200 |
"""
|
@@ -205,16 +267,14 @@ with gr.Blocks() as app:
|
|
205 |
with gr.Row():
|
206 |
gr.HTML("<h1 style='text-align: center;'>Welcome to Biryani Hub</h1>")
|
207 |
|
208 |
-
# Login Page
|
209 |
with gr.Row(visible=True) as login_page:
|
210 |
with gr.Column():
|
211 |
login_email = gr.Textbox(label="Email")
|
212 |
login_password = gr.Textbox(label="Password", type="password")
|
213 |
login_button = gr.Button("Login")
|
214 |
-
|
215 |
login_output = gr.Textbox(label="Status")
|
216 |
|
217 |
-
# Signup Page
|
218 |
with gr.Row(visible=False) as signup_page:
|
219 |
with gr.Column():
|
220 |
signup_name = gr.Textbox(label="Name")
|
@@ -225,39 +285,22 @@ with gr.Blocks() as app:
|
|
225 |
login_redirect = gr.Button("Go to Login")
|
226 |
signup_output = gr.Textbox(label="Status")
|
227 |
|
228 |
-
# Menu Page
|
229 |
with gr.Row(visible=False) as menu_page:
|
230 |
with gr.Column():
|
231 |
-
preference = gr.Radio(["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
|
232 |
menu_output = gr.HTML()
|
233 |
-
gr.HTML(
|
|
|
|
|
|
|
|
|
234 |
gr.HTML(modal_js())
|
235 |
-
gr.HTML("<div id='cart-summary'></div>")
|
236 |
|
237 |
-
# Navigation and Functionality
|
238 |
login_button.click(
|
239 |
-
lambda email, password: (gr.update(visible=False), gr.update(visible=True), filter_menu("All"), "Login successful!")
|
240 |
-
if login(email, password)[0] == "Login successful!" else (gr.update(), gr.update(),
|
241 |
-
[login_email, login_password],
|
242 |
-
[login_page, menu_page, menu_output, login_output]
|
243 |
-
)
|
244 |
-
|
245 |
-
submit_signup.click(
|
246 |
-
lambda name, email, phone, password: (signup(name, email, phone, password), gr.update(visible=False), gr.update(visible=True)),
|
247 |
-
[signup_name, signup_email, signup_phone, signup_password],
|
248 |
-
[signup_output, signup_page, login_page]
|
249 |
-
)
|
250 |
-
|
251 |
-
signup_redirect.click(
|
252 |
-
lambda: (gr.update(visible=False), gr.update(visible=True)),
|
253 |
-
[],
|
254 |
-
[login_page, signup_page]
|
255 |
-
)
|
256 |
-
|
257 |
-
login_redirect.click(
|
258 |
-
lambda: (gr.update(visible=True), gr.update(visible=False)),
|
259 |
-
[],
|
260 |
-
[login_page, signup_page]
|
261 |
)
|
|
|
262 |
|
263 |
app.launch()
|
|
|
76 |
# Function to filter menu items
|
77 |
def filter_menu(preference):
|
78 |
menu_data = load_menu_from_salesforce()
|
79 |
+
|
80 |
filtered_data = {}
|
81 |
for item in menu_data:
|
82 |
if "Section__c" not in item or "Veg_NonVeg__c" not in item:
|
|
|
112 |
|
113 |
return html_content
|
114 |
|
115 |
+
# Function to generate final order
|
116 |
+
def submit_cart():
|
117 |
+
final_order_html = "<h3>Final Order:</h3><ul>"
|
118 |
+
total_bill = 0
|
119 |
+
for item in cart:
|
120 |
+
total_bill += item['totalCost']
|
121 |
+
extras = ", ".join([f"{extra['name']} (+${extra['price'] * extra['quantity']})" for extra in item['extras']])
|
122 |
+
final_order_html += f"""
|
123 |
+
<li>
|
124 |
+
{item['name']} (x{item['quantity']}) - ${item['totalCost']:.2f}
|
125 |
+
<br>Extras: {extras if extras else "None"}
|
126 |
+
<br>Special Instructions: {item['instructions'] if item['instructions'] else "None"}
|
127 |
+
</li>
|
128 |
+
"""
|
129 |
+
final_order_html += f"</ul><p><strong>Total Bill: ${total_bill:.2f}</strong></p>"
|
130 |
+
return final_order_html
|
131 |
def create_modal_window():
|
132 |
add_ons = load_add_ons_from_salesforce()
|
133 |
add_ons_html = ""
|
|
|
161 |
"""
|
162 |
return modal_html
|
163 |
|
164 |
+
|
165 |
+
# JavaScript for Modal and Cart
|
166 |
def modal_js():
|
167 |
modal_script = """
|
168 |
<script>
|
|
|
182 |
document.getElementById('modal-price').innerText = price;
|
183 |
document.getElementById('quantity').value = 1;
|
184 |
document.getElementById('special-instructions').value = '';
|
185 |
+
resetAddOns(); // Reset add-ons when opening the modal
|
186 |
}
|
187 |
function closeModal() {
|
188 |
document.getElementById('modal').style.display = 'none';
|
|
|
192 |
const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
|
193 |
const quantity = parseInt(document.getElementById('quantity').value) || 1;
|
194 |
const instructions = document.getElementById('special-instructions').value;
|
195 |
+
const selectedAddOns = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked'));
|
196 |
+
const extras = selectedAddOns.map(extra => ({
|
197 |
+
name: extra.value,
|
198 |
+
price: parseFloat(extra.getAttribute('data-price')),
|
199 |
+
quantity: 1 // Default quantity for add-ons is 1
|
200 |
+
}));
|
201 |
+
const extrasCost = extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
|
202 |
+
const totalCost = (price * quantity) + extrasCost;
|
203 |
+
// Add the item to the cart with its specific add-ons
|
204 |
+
cart.push({ name, price, quantity, extras, instructions, totalCost });
|
205 |
+
totalCartCost += totalCost; // Update the total cost of the cart
|
206 |
updateCartButton();
|
207 |
+
updateCartTotalCost(); // Update total cost displayed
|
208 |
closeModal();
|
209 |
}
|
210 |
function updateCartButton() {
|
211 |
const cartButton = document.getElementById('cart-button');
|
212 |
cartButton.innerText = `View Cart (${cart.length} items)`;
|
213 |
}
|
214 |
+
function openCartModal() {
|
215 |
+
const cartModal = document.getElementById('cart-modal');
|
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 |
+
function removeFromCart(index) {
|
237 |
+
totalCartCost -= cart[index].totalCost; // Deduct the cost of the removed item from total cost
|
238 |
+
cart.splice(index, 1);
|
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 |
+
function updateCartTotalCost() {
|
257 |
+
const totalCostElement = document.getElementById('cart-total-cost');
|
258 |
+
totalCartCost = cart.reduce((total, item) => total + item.totalCost, 0);
|
259 |
+
totalCostElement.innerText = `Total Cart Cost: $${totalCartCost.toFixed(2)}`;
|
260 |
}
|
261 |
</script>
|
262 |
"""
|
|
|
267 |
with gr.Row():
|
268 |
gr.HTML("<h1 style='text-align: center;'>Welcome to Biryani Hub</h1>")
|
269 |
|
|
|
270 |
with gr.Row(visible=True) as login_page:
|
271 |
with gr.Column():
|
272 |
login_email = gr.Textbox(label="Email")
|
273 |
login_password = gr.Textbox(label="Password", type="password")
|
274 |
login_button = gr.Button("Login")
|
275 |
+
signup_button = gr.Button("Go to Signup")
|
276 |
login_output = gr.Textbox(label="Status")
|
277 |
|
|
|
278 |
with gr.Row(visible=False) as signup_page:
|
279 |
with gr.Column():
|
280 |
signup_name = gr.Textbox(label="Name")
|
|
|
285 |
login_redirect = gr.Button("Go to Login")
|
286 |
signup_output = gr.Textbox(label="Status")
|
287 |
|
|
|
288 |
with gr.Row(visible=False) as menu_page:
|
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("<div id='cart-items'></div>")
|
295 |
+
gr.HTML("<button onclick='submitCart()'>Proceed to Checkout</button>")
|
296 |
+
gr.HTML("</div>")
|
297 |
gr.HTML(modal_js())
|
|
|
298 |
|
|
|
299 |
login_button.click(
|
300 |
+
lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!")
|
301 |
+
if login(email, password)[0] == "Login successful!" else (gr.update(), gr.update(), gr.update(), "Invalid email or password."),
|
302 |
+
[login_email, login_password], [login_page, menu_page, menu_output, login_output]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
303 |
)
|
304 |
+
preference.change(lambda pref: filter_menu(pref), [preference], menu_output)
|
305 |
|
306 |
app.launch()
|