From 3f0ada5270bb28632da84deaeb0b082c88f0b069 Mon Sep 17 00:00:00 2001 From: Anand Suthar Date: Sun, 25 Aug 2024 23:33:14 +0530 Subject: [PATCH] feat(admin/dashboard): Add SpinnerLoader in dashboard page --- app/(pages)/admin/page.tsx | 192 +++++++++++++++++++++---------------- 1 file changed, 111 insertions(+), 81 deletions(-) diff --git a/app/(pages)/admin/page.tsx b/app/(pages)/admin/page.tsx index d06f91c5..e788dd27 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 */} +
+ + + +
+ + )}