Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions actions/admin/dashboard.ts
Original file line number Diff line number Diff line change
@@ -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<any> => {
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<any> => {
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<any> => {
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
};
};
61 changes: 52 additions & 9 deletions components/shared/admin/dashboard/stat-card.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -30,30 +36,67 @@ function StatsCard({ title, value, change, icon }: StatsCardProps) {
}

export function StatsCards() {
const [userStats, setUserStats] = useState<any>(null);
const [mentorStats, setMentorStats] = useState<any>(null);
const [conversationStat, setConversationStats] = useState<any>(null);
const [user, setUser] = useState<Account | null>(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 <div></div>;
}

console.log('mentorStats:', mentorStats);

return (
<div className="grid gap-4 sm:grid-cols-1 md:grid-cols-1 lg:grid-cols-2">
<StatsCard
title="Total Users"
value="2,350"
change="+180 from last month"
value={userStats.meta.total || '10'}
change={`+${userStats.usersChange || '0'} from last month`}
icon="users"
/>
<StatsCard
title="Total Mentors"
value="2,350"
change="+180 from last month"
value={mentorStats.data.meta.total || '0'}
change={`+${userStats.mentorsChange || '0'} from last month`}
icon="users"
/>
<StatsCard
title="Active Users"
value="120"
change="+12 since last hour"
title="Messages"
value={userStats.activeUsers || '0'}
change={`+${userStats.activeUsersChange || '0'} since last hour`}
icon="activity"
/>
<StatsCard
title="Active Mentors"
value="30"
change="+3 since last hour"
value={mentorStats.activeCount || '0'}
change={`+${userStats.activeMentorsChange || '0'} since last hour`}
icon="activity"
/>
</div>
Expand Down