File size: 4,780 Bytes
c053032
33eba6b
 
 
444fe60
c053032
 
444fe60
c053032
 
 
 
 
 
 
 
8f55c48
c053032
 
 
 
 
 
444fe60
c053032
 
 
3ad292c
c053032
3ad292c
c053032
 
 
 
3ad292c
c053032
bfa1ca7
 
 
 
 
 
 
 
 
 
 
 
 
444fe60
3ad292c
 
c053032
 
 
 
444fe60
c053032
 
33eba6b
c053032
444fe60
c053032
 
 
 
 
33eba6b
444fe60
c053032
444fe60
 
33eba6b
 
c053032
444fe60
c053032
 
 
 
2821616
 
 
 
 
 
 
 
 
 
 
 
 
c053032
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
444fe60
c053032
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import gradio as gr
from models.salesforce import fetch_menu_items, place_order_in_salesforce
from models.cart import add_to_cart, view_cart, clear_cart
from models.user import login_user, signup_user

# Global user state
current_user = {"email": None, "name": None}

# Login Interface
def login(email, password):
    success, message = login_user(email, password)
    if success:
        current_user["email"] = email
        current_user["name"] = message
        return "Login successful! Redirecting to menu..."
    return f"Login failed: {message}"

# Signup Interface
def signup(name, email, phone, password):
    success, message = signup_user(name, email, phone, password)
    if success:
        return "Signup successful! Please login now."
    return f"Signup failed: {message}"

# Load Menu Items
def load_menu(preference):
    items = fetch_menu_items()
    print(f"Fetched menu items: {items}")  # Debugging output
    filtered_items = [
        item for item in items
        if preference == "All"
        or (preference == "Veg" and item["Veg_NonVeg__c"] == "Veg")
        or (preference == "Non-Veg" and item["Veg_NonVeg__c"] == "Non-Veg")
    ]
    print(f"Filtered menu items: {filtered_items}")  # Debugging output
    return filtered_items
    
def display_menu(preference):
    menu_items = fetch_menu_items()
    # Filter based on preference
    if preference == "Veg":
        filtered_items = [item for item in menu_items if item["Veg_NonVeg"] == "Veg"]
    elif preference == "Non-Veg":
        filtered_items = [item for item in menu_items if item["Veg_NonVeg"] == "Non veg"]
    else:
        filtered_items = menu_items

    # Prepare for display in Gradio
    return [[item["Name"], item["Price"], item["Description"]] for item in filtered_items]

    

# Add to Cart
def add_to_cart_interface(item_name, price):
    add_to_cart(item_name, price)
    return f"Added {item_name} to cart!"

# View Cart
def view_cart_interface():
    cart, total = view_cart()
    return cart, total

# Place Order
def place_order_interface():
    email = current_user["email"]
    if not email:
        return "Error: Please login to place an order."
    cart, total = view_cart()
    if not cart:
        return "Error: Cart is empty!"
    order_details = "\n".join([f"{item['Name']} - ${item['Price']} x {item['Quantity']}" for item in cart])
    try:
        place_order_in_salesforce(email, order_details, total)
        clear_cart()
        return f"Order placed successfully! Total: ${total}"
    except Exception as e:
        return f"Error placing order: {str(e)}"

# Gradio Interfaces
with gr.Blocks() as app:
    preference_radio = gr.Radio(["All", "Veg", "Non-Veg"], label="Filter Menu", value="All")
    menu_data_table = gr.Dataframe(headers=["Name", "Price", "Description"], datatype=["str", "number", "str"])

    # Add a button to trigger menu display
    menu_button = gr.Button("Show Menu")

    # Connect display logic
    menu_button.click(
        display_menu,  # Function to call
        inputs=preference_radio,  # Input component
        outputs=menu_data_table  # Output component
    )


    with gr.Tab("Signup"):
        name = gr.Textbox(label="Name", placeholder="Enter your name")
        signup_email = gr.Textbox(label="Email", placeholder="Enter your email")
        phone = gr.Textbox(label="Phone", placeholder="Enter your phone number")
        signup_password = gr.Textbox(label="Password", type="password", placeholder="Create a password")
        signup_button = gr.Button("Signup")
        signup_output = gr.Textbox(label="Signup Status")
        signup_button.click(signup, inputs=[name, signup_email, phone, signup_password], outputs=signup_output)

    with gr.Tab("Menu"):
        preference = gr.Radio(["All", "Veg", "Non-Veg"], label="Filter Menu", value="All")
        menu_output = gr.Dataframe(headers=["Name", "Description", "Price"])
        preference.change(lambda p: load_menu(p), inputs=[preference], outputs=menu_output)
        item_name = gr.Textbox(label="Item Name")
        item_price = gr.Textbox(label="Price")
        add_button = gr.Button("Add to Cart")
        cart_status = gr.Textbox(label="Cart Status")
        add_button.click(add_to_cart_interface, inputs=[item_name, item_price], outputs=cart_status)

    with gr.Tab("Cart"):
        view_button = gr.Button("View Cart")
        cart_output = gr.Dataframe(headers=["Name", "Price", "Quantity"])
        total_output = gr.Textbox(label="Total Amount")
        view_button.click(view_cart_interface, outputs=[cart_output, total_output])

        place_order_button = gr.Button("Place Order")
        order_status = gr.Textbox(label="Order Status")
        place_order_button.click(place_order_interface, outputs=order_status)

app.launch()