diff --git a/app/(pages)/admin/page.tsx b/app/(pages)/admin/page.tsx index d06f91c..e788dd2 100644 --- a/app/(pages)/admin/page.tsx +++ b/app/(pages)/admin/page.tsx @@ -11,7 +11,9 @@ import { RiHospitalLine, } from "react-icons/ri"; import { getTilesData } from "@lib/admin"; +import SpinnerLoader from "@components/SpinnerLoader"; // Import your SpinnerLoader component +// Define types type TilesDataProp = { totalHospitals: string; totalPatients: string; @@ -57,6 +59,8 @@ export default function Admin() { const [recentUsers, setRecentUsers] = useState([]); const [page, setPage] = useState(1); const [hasMore, setHasMore] = useState(true); + const [isLoadingTiles, setIsLoadingTiles] = useState(true); + const [isLoadingUsers, setIsLoadingUsers] = useState(true); const observer = useRef(null); const lastUserElementRef = useCallback( @@ -74,8 +78,12 @@ export default function Admin() { useEffect(() => { const fetchData = async () => { - const tiles = await getTilesData(); - setTilesData(tiles); + try { + const tiles = await getTilesData(); + setTilesData(tiles); + } finally { + setIsLoadingTiles(false); + } }; fetchData(); }, []); @@ -87,6 +95,8 @@ export default function Admin() { const fetchRecentUsers = async () => { if (!hasMore) return; + setIsLoadingUsers(true); + try { const response = await fetch( `/api/admin/dashboard/recent-users?page=${page}&limit=10` @@ -96,6 +106,8 @@ export default function Admin() { setHasMore(data.page < data.totalPages); } catch (error) { console.error("Error fetching recent users:", error); + } finally { + setIsLoadingUsers(false); } }; @@ -124,94 +136,112 @@ export default function Admin() { ] : []; + // if both loading states are true + const isLoading = isLoadingTiles || isLoadingUsers; + return (
- {/* Statistics Cards */} -
- {statisticsCards.map((stat, index) => ( - - -
-

- {stat.title} -

- {stat.icon} -
-

- {stat.value} -

-
-
- ))} -
- - {/* Recent Activity */} - - -

- Recent Activity -

-
- -
- {recentUsers.map((user, index) => ( -
+ +
+ ) : ( + <> + {/* Statistics Cards */} +
+ {statisticsCards.map((stat, index) => ( + -
- {activityIcons[user.title]} -
-
-

{user.title}

-

- {user.description} + +

+

+ {stat.title} +

+ {stat.icon} +
+

+ {stat.value}

-
-

{user.timeSince}

-
+ + ))}
-
-
- - {/* Action Buttons */} -
- - - -
+ + {/* Recent Activity */} + + +

+ Recent Activity +

+
+ +
+ {recentUsers.map((user, index) => ( +
+
+ {activityIcons[user.title]} +
+
+

+ {user.title} +

+

+ {user.description} +

+
+

+ {user.timeSince} +

+
+ ))} +
+
+
+ + {/* Action Buttons */} +
+ + + +
+ + )}