From b7931cb421e727f3e1a54c30b765f58ea52aeae4 Mon Sep 17 00:00:00 2001
From: nn2-minh <Nguyen12.Minh@live.uwe.ac.uk>
Date: Fri, 25 Apr 2025 00:39:04 +0700
Subject: [PATCH] modify the interface of owner

---
 app/frontend/components/dashboard.py          | 51 ++++++++++++++++++-
 .../components/product/view_product.py        |  8 ++-
 app/frontend/components/shop/view_shop.py     | 33 +++++++++++-
 app/frontend/components/user_details.py       | 22 +++++---
 app/frontend/components/user_orders.py        | 22 +++++---
 app/frontend/components/user_payments.py      | 17 +++++--
 6 files changed, 132 insertions(+), 21 deletions(-)

diff --git a/app/frontend/components/dashboard.py b/app/frontend/components/dashboard.py
index 535024b..f115d19 100644
--- a/app/frontend/components/dashboard.py
+++ b/app/frontend/components/dashboard.py
@@ -35,7 +35,29 @@ def dashboard_frame(parent, switch_func, API_URL, token):
     sidebar.pack(side="left", fill="y")
     sidebar.pack_propagate(False)
 
-    # Dashboard Icon
+    # Check if user is a shop owner
+    is_owner = False
+
+    # Function to check user role
+    def check_user_role():
+        nonlocal is_owner
+        if not token:
+            return
+
+        headers = {"Authorization": f"Bearer {token}"}
+        try:
+            resp = requests.get(f"{API_URL}/user/profile", headers=headers)
+            if resp.status_code == 200:
+                data = resp.json()
+                user_role = data.get("role", "")
+                is_owner = user_role == "owner" or user_role == "shop_owner"
+        except Exception as e:
+            print(f"Error checking user role: {e}")
+
+    # Check user role on initialization
+    check_user_role()
+
+    # Dashboard Icon with modified command for shop owners
     dashboard_icon = ctk.CTkButton(
         sidebar,
         text="🏠",
@@ -45,7 +67,9 @@ def dashboard_frame(parent, switch_func, API_URL, token):
         hover_color="#3b3b3b",
         width=40,
         height=40,
-        command=lambda: switch_func("dashboard"),
+        command=lambda: switch_func("dashboard_bypass", token)
+        if is_owner
+        else switch_func("dashboard"),
     )
     dashboard_icon.pack(pady=(20, 10))
 
@@ -790,4 +814,27 @@ def dashboard_frame(parent, switch_func, API_URL, token):
     fetch_featured_shops()
     fetch_recommendations()
 
+    # Add a refresh method to handle token updates and role changes
+    def refresh_data(*args):
+        nonlocal token
+
+        # If args[0] is a string, it's likely a new token
+        if args and isinstance(args[0], str):
+            token = args[0]
+            # Re-check the user role
+            check_user_role()
+
+            # Update the dashboard icon command based on new role status
+            dashboard_icon.configure(
+                command=lambda: switch_func("dashboard_bypass", token)
+                if is_owner
+                else switch_func("dashboard")
+            )
+
+        # Refresh the dashboard content
+        fetch_featured_shops()
+        fetch_recommendations()
+
+    frame.refresh_data = refresh_data
+
     return frame
diff --git a/app/frontend/components/product/view_product.py b/app/frontend/components/product/view_product.py
index 507baf4..7891b78 100644
--- a/app/frontend/components/product/view_product.py
+++ b/app/frontend/components/product/view_product.py
@@ -615,9 +615,15 @@ def view_product_frame(
         stock_frame = ctk.CTkFrame(details_inner, fg_color="transparent", height=30)
         stock_frame.pack(fill="x", pady=(0, 15))
 
+        # Modified stock label to show remaining quantity when in stock
+        if product_stock > 0:
+            stock_text = f"Status: {stock_status} ({product_stock} remaining)"
+        else:
+            stock_text = f"Status: {stock_status}"
+
         stock_label = ctk.CTkLabel(
             stock_frame,
-            text=f"Status: {stock_status}",
+            text=stock_text,
             font=("Helvetica", 14),
             text_color=stock_color,
         )
diff --git a/app/frontend/components/shop/view_shop.py b/app/frontend/components/shop/view_shop.py
index 747bf3e..5a9237d 100644
--- a/app/frontend/components/shop/view_shop.py
+++ b/app/frontend/components/shop/view_shop.py
@@ -30,6 +30,25 @@ def view_shop_frame(parent, switch_func, API_URL, token, shop_id):
     main_frame = ctk.CTkFrame(parent, fg_color="transparent")
     main_frame.pack(fill="both", expand=True)
 
+    # Track if user is a shop owner
+    is_owner = False
+
+    # Check if user is an owner
+    def check_user_role():
+        nonlocal is_owner
+        if not token:
+            return
+
+        headers = {"Authorization": f"Bearer {token}"}
+        try:
+            resp = requests.get(f"{API_URL}/user/profile", headers=headers)
+            if resp.status_code == 200:
+                data = resp.json()
+                user_role = data.get("role", "")
+                is_owner = user_role == "owner" or user_role == "shop_owner"
+        except Exception as e:
+            print(f"Error checking user role: {e}")
+
     # Create a container frame for content that can be refreshed
     container_frame = ctk.CTkFrame(main_frame, fg_color="transparent")
     container_frame.pack(fill="both", expand=True)
@@ -42,6 +61,9 @@ def view_shop_frame(parent, switch_func, API_URL, token, shop_id):
         if new_shop_id:
             shop_id = new_shop_id
 
+        # Check user role with the updated token
+        check_user_role()
+
         # Clear the container
         for widget in container_frame.winfo_children():
             widget.destroy()
@@ -54,11 +76,18 @@ def view_shop_frame(parent, switch_func, API_URL, token, shop_id):
             no_shop_label.pack(pady=20)
             return
 
-        # Back button at the top
+        # Back button at the top with dashboard_bypass for owners
+        def go_to_dashboard():
+            # For shop owners, use dashboard_bypass to ensure they go to the user dashboard
+            if is_owner:
+                switch_func("dashboard_bypass", token)
+            else:
+                switch_func("dashboard")
+
         back_button = ctk.CTkButton(
             container_frame,
             text="← Back to Dashboard",
-            command=lambda: switch_func("dashboard"),
+            command=go_to_dashboard,
             fg_color=SHOPPING,
             text_color="white",
             width=150,
diff --git a/app/frontend/components/user_details.py b/app/frontend/components/user_details.py
index 9339a64..a689a93 100644
--- a/app/frontend/components/user_details.py
+++ b/app/frontend/components/user_details.py
@@ -110,7 +110,8 @@ def user_details_frame(parent, switch_func, API_URL, token):
 
     # Create a variable for the owner button that can be modified later
     owner_button_frame = ctk.CTkFrame(sidebar_frame, fg_color="transparent")
-    owner_button_frame.pack(fill="x", padx=15, pady=5)
+    # Pack with minimal padding to be closer to nav buttons
+    owner_button_frame.pack(fill="x", padx=15, pady=0)
 
     def open_create_shop():
         switch_func("create_shop")
@@ -123,7 +124,7 @@ def user_details_frame(parent, switch_func, API_URL, token):
 
     # Add a separator before logout
     separator = ctk.CTkFrame(sidebar_frame, fg_color="#3b3b3b", height=1)
-    separator.pack(fill="x", padx=15, pady=10)
+    separator.pack(fill="x", padx=15, pady=10, side="bottom")
 
     # Add logout button with red accent
     def logout():
@@ -356,22 +357,31 @@ def user_details_frame(parent, switch_func, API_URL, token):
 
                 # Create the appropriate button based on user role
                 if is_owner:
-                    # Create a more visually prominent button for shop owners
+                    # Create a button with the same styling as other nav buttons
                     owner_button = ctk.CTkButton(
                         owner_button_frame,
                         text="Go to Shop Dashboard",
-                        command=go_to_owner_dashboard,
                         fg_color=SHOPPING,  # Use the shopping color for emphasis
                         text_color="white",
                         hover_color="#0096ff",
                         height=40,
+                        command=go_to_owner_dashboard,
                         font=("Helvetica", 14, "bold"),
                     )
                 else:
-                    owner_button = create_nav_button(
-                        "Become Shop Owner", open_create_shop
+                    # Create a styled button that stands out better than the nav buttons
+                    owner_button = ctk.CTkButton(
+                        owner_button_frame,
+                        text="Become Shop Owner",
+                        command=open_create_shop,
+                        fg_color="#3b3b3b",  # Darker than transparent but not too prominent
+                        text_color="white",
+                        hover_color="#4a4a4a",
+                        height=40,
+                        font=("Helvetica", 14),
                     )
 
+                # Pack without any padding to match other buttons
                 owner_button.pack(fill="x")
 
             else:
diff --git a/app/frontend/components/user_orders.py b/app/frontend/components/user_orders.py
index bfe4b81..5b5cd18 100644
--- a/app/frontend/components/user_orders.py
+++ b/app/frontend/components/user_orders.py
@@ -121,7 +121,8 @@ def user_orders_frame(parent, switch_func, API_URL, token):
 
     # Create a variable for the owner button that can be modified later
     owner_button_frame = ctk.CTkFrame(sidebar_frame, fg_color="transparent")
-    owner_button_frame.pack(fill="x", padx=15, pady=5)
+    # Pack with minimal padding to be closer to nav buttons
+    owner_button_frame.pack(fill="x", padx=15, pady=0)
 
     def open_create_shop():
         switch_func("create_shop")
@@ -134,7 +135,7 @@ def user_orders_frame(parent, switch_func, API_URL, token):
 
     # Add a separator before logout
     separator = ctk.CTkFrame(sidebar_frame, fg_color="#3b3b3b", height=1)
-    separator.pack(fill="x", padx=15, pady=10)
+    separator.pack(fill="x", padx=15, pady=10, side="bottom")
 
     # Add logout button with red accent
     def logout():
@@ -1240,22 +1241,31 @@ def user_orders_frame(parent, switch_func, API_URL, token):
 
                 # Create the appropriate button based on user role
                 if is_owner:
-                    # Create a more visually prominent button for shop owners
+                    # Create a button with the same styling as other nav buttons
                     owner_button = ctk.CTkButton(
                         owner_button_frame,
                         text="Go to Shop Dashboard",
-                        command=go_to_owner_dashboard,
                         fg_color=SHOPPING,  # Use the shopping color for emphasis
                         text_color="white",
                         hover_color="#0096ff",
                         height=40,
+                        command=go_to_owner_dashboard,
                         font=("Helvetica", 14, "bold"),
                     )
                 else:
-                    owner_button = create_nav_button(
-                        "Become Shop Owner", open_create_shop
+                    # Create a styled button that stands out better than the nav buttons
+                    owner_button = ctk.CTkButton(
+                        owner_button_frame,
+                        text="Become Shop Owner",
+                        command=open_create_shop,
+                        fg_color="#3b3b3b",  # Darker than transparent but not too prominent
+                        text_color="white",
+                        hover_color="#4a4a4a",
+                        height=40,
+                        font=("Helvetica", 14),
                     )
 
+                # Pack without any padding to match other buttons
                 owner_button.pack(fill="x")
         except Exception as e:
             print(f"Error checking user role: {e}")
diff --git a/app/frontend/components/user_payments.py b/app/frontend/components/user_payments.py
index 30672ad..fbe48d4 100644
--- a/app/frontend/components/user_payments.py
+++ b/app/frontend/components/user_payments.py
@@ -122,7 +122,8 @@ def user_payments_frame(parent, switch_func, API_URL, token):
 
     # Create a variable for the owner button that can be modified later
     owner_button_frame = ctk.CTkFrame(sidebar_frame, fg_color="transparent")
-    owner_button_frame.pack(fill="x", padx=15, pady=5)
+    # Pack with minimal padding to be closer to nav buttons
+    owner_button_frame.pack(fill="x", padx=15, pady=0)
 
     def open_create_shop():
         switch_func("create_shop")
@@ -135,7 +136,7 @@ def user_payments_frame(parent, switch_func, API_URL, token):
 
     # Add a separator before logout
     separator = ctk.CTkFrame(sidebar_frame, fg_color="#3b3b3b", height=1)
-    separator.pack(fill="x", padx=15, pady=10)
+    separator.pack(fill="x", padx=15, pady=10, side="bottom")
 
     # Add logout button with red accent
     def logout():
@@ -898,8 +899,16 @@ def user_payments_frame(parent, switch_func, API_URL, token):
                         font=("Helvetica", 14, "bold"),
                     )
                 else:
-                    owner_button = create_nav_button(
-                        "Become Shop Owner", open_create_shop
+                    # Create a styled button that stands out better than the nav buttons
+                    owner_button = ctk.CTkButton(
+                        owner_button_frame,
+                        text="Become Shop Owner",
+                        command=open_create_shop,
+                        fg_color="#3b3b3b",  # Darker than transparent but not too prominent
+                        text_color="white",
+                        hover_color="#4a4a4a",
+                        height=40,
+                        font=("Helvetica", 14),
                     )
 
                 owner_button.pack(fill="x")
-- 
GitLab