From 926acd27c1192fefb7b917e04b2eed198184adbf Mon Sep 17 00:00:00 2001 From: behailu-bekele Date: Mon, 14 Apr 2025 23:47:36 +0300 Subject: [PATCH] created dashboard.ts in actions/admin to get data from API and modified static data into real data in the stat-card.tsx --- actions/admin/dashboard.ts | 66 +++++++++++++++++++ .../shared/admin/dashboard/stat-card.tsx | 61 ++++++++++++++--- 2 files changed, 118 insertions(+), 9 deletions(-) create mode 100644 actions/admin/dashboard.ts diff --git a/actions/admin/dashboard.ts b/actions/admin/dashboard.ts new file mode 100644 index 00000000..49122575 --- /dev/null +++ b/actions/admin/dashboard.ts @@ -0,0 +1,66 @@ +'use server'; +import { GetRequest } from '@/base-api/method'; +const Url = { + user: 'admin/user/all', + mentor: 'admin/mentor', +}; + +export const getUser = async ( + accountId: string, + options?: { + roleId?: string; + page?: number; + limit?: number; + isActive?: boolean; + } +): Promise => { + let url = `${Url.user}?accountId=${accountId}`; + const { roleId, page, limit, isActive } = options || {}; + if (roleId !== undefined) { + url += `&roleId=${roleId}`; + } + if (page !== undefined) { + url += `&page=${page}`; + } + if (limit !== undefined) { + url += `&limit=${limit}`; + } + if (isActive !== undefined) { + url += `&isActive=${isActive}`; + } + + const getRequest = new GetRequest(url, 'get-users-stats'); + const data = await getRequest.getData(); + return data; +}; + +export const getConversation = async ( + accountId: string, + page?: number, + limit?: number +): Promise => { + let url = `conversation?accountId=${accountId}`; + if (page !== undefined) { + url += `&page=${page}`; + } + if (limit !== undefined) { + url += `&limit=${limit}`; + } + const getRequest = new GetRequest(url, 'conversation'); + const data = await getRequest.getData(); + return data; +}; + +export const getMentor = async (accountId: string): Promise => { + const url = `${Url.mentor}?accountId=${accountId}`; + const getRequest = new GetRequest(url, 'mentors'); + const data = await getRequest.getData(); + const activeCount = data.data.reduce((count: any, mentor: any) => { + return mentor.isActive ? count + 1 : count; + }, 0); + + return { + activeCount, + data + }; +}; diff --git a/components/shared/admin/dashboard/stat-card.tsx b/components/shared/admin/dashboard/stat-card.tsx index b9762464..c7559f14 100644 --- a/components/shared/admin/dashboard/stat-card.tsx +++ b/components/shared/admin/dashboard/stat-card.tsx @@ -1,5 +1,11 @@ +'use client'; + +import { useEffect, useState } from 'react'; +import { getUser, getMentor, getConversation } from '@/actions/admin/dashboard'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { FiUsers, FiActivity } from 'react-icons/fi'; +import { Account } from '@/types/users'; +import { userProfile } from '@/actions/auth/login'; interface StatsCardProps { title: string; @@ -30,30 +36,67 @@ function StatsCard({ title, value, change, icon }: StatsCardProps) { } export function StatsCards() { + const [userStats, setUserStats] = useState(null); + const [mentorStats, setMentorStats] = useState(null); + const [conversationStat, setConversationStats] = useState(null); + const [user, setUser] = useState(null); + + useEffect(() => { + const fetchUserProfile = async () => { + const userAccountId: Account = await userProfile(); + console.log('id:',userAccountId) + setUser(userAccountId); + }; + fetchUserProfile(); + }, []); + + useEffect(() => { + if (!user) return; + if (!user.id) return; + + const accountId = user.id; + + const fetchStats = async () => { + const statsConversation = await getConversation(accountId); + const statsMentor = await getMentor(accountId); + const stats = await getUser(accountId); + setMentorStats(statsMentor); + setConversationStats(statsConversation); + setUserStats(stats); + }; + fetchStats(); + }, [user]); + + if (!userStats) { + return
; + } + + console.log('mentorStats:', mentorStats); + return (