-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminDashboard.tsx
More file actions
187 lines (178 loc) · 6.58 KB
/
Copy pathAdminDashboard.tsx
File metadata and controls
187 lines (178 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { useState, useEffect } from 'react';
import { Link } from 'react-router-dom';
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from '../../components/ui/Card/Card';
import StatusBadge from '../../components/ui/StatusBadge/StatusBadge';
import { Users, BookOpen, GraduationCap, DollarSign } from 'lucide-react';
import { appealService } from '../../api/services/appeal.service';
import { useAdminAnalytics } from '../../hooks/useAdmin';
import { formatCurrency } from '../../lib/currency';
import type { Appeal } from '../../types/entities';
export default function AdminDashboard() {
const [recentRequests, setRecentRequests] = useState<Appeal[]>([]);
const [isLoading, setIsLoading] = useState(true);
// Obtener analíticas del backend
const { data: analytics, isLoading: isLoadingAnalytics } =
useAdminAnalytics(6);
// Construir stats dinámicamente desde las analíticas
const stats = analytics
? [
{
title: 'Total Estudiantes',
value: analytics.totalStudents.toLocaleString('es-AR'),
change: `${
analytics.growth.studentsGrowthPercentage >= 0 ? '+' : ''
}${analytics.growth.studentsGrowthPercentage.toFixed(1)}%`,
icon: Users,
isPositive: analytics.growth.studentsGrowthPercentage >= 0,
},
{
title: 'Cursos Publicados',
value:
analytics.courseStats.byStatus.publicado.toLocaleString('es-AR'),
change: `${
analytics.growth.coursesGrowthPercentage >= 0 ? '+' : ''
}${analytics.growth.coursesGrowthPercentage.toFixed(1)}%`,
icon: BookOpen,
isPositive: analytics.growth.coursesGrowthPercentage >= 0,
},
{
title: 'Profesores Activos',
value: analytics.totalProfessors.toLocaleString('es-AR'),
change: `${
analytics.growth.professorsGrowthPercentage >= 0 ? '+' : ''
}${analytics.growth.professorsGrowthPercentage.toFixed(1)}%`,
icon: GraduationCap,
isPositive: analytics.growth.professorsGrowthPercentage >= 0,
},
{
title: 'Ingresos de la Plataforma',
value: formatCurrency(analytics.platformEarningsInCents),
change: `${
analytics.growth.revenueGrowthPercentage >= 0 ? '+' : ''
}${analytics.growth.revenueGrowthPercentage.toFixed(1)}%`,
icon: DollarSign,
isPositive: analytics.growth.revenueGrowthPercentage >= 0,
},
]
: [];
useEffect(() => {
const fetchRecentRequests = async () => {
try {
const response = await appealService.findAllAppeals();
const allRequests = response.appeals || [];
const sorted = allRequests.sort(
(a: Appeal, b: Appeal) =>
new Date(b.date).getTime() - new Date(a.date).getTime()
);
setRecentRequests(sorted.slice(0, 4));
} catch (error) {
console.error('Failed to fetch requests', error);
} finally {
setIsLoading(false);
}
};
fetchRecentRequests();
}, []);
return (
<div className="min-h-screen bg-gradient-to-br from-slate-50 via-blue-50/30 to-slate-50/100 px-4">
<div className="container mx-auto max-w-7xl space-y-6 pt-24 pb-8">
<div>
<h1 className="text-3xl font-bold text-slate-800 mb-2">
Dashboard de Administrador
</h1>
<p className="text-slate-600">Resumen general de la plataforma.</p>
</div>
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6">
{stats.map((stat) => (
<Card key={stat.title}>
<CardHeader className="flex flex-row items-center justify-between space-y-2 pb-2">
<CardTitle className="text-sm font-medium text-slate-600">
{stat.title}
</CardTitle>
<stat.icon className="w-5 h-5 text-slate-400" />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold text-slate-800">
{stat.value}
</div>
<p
className={`text-xs mt-1 ${
stat.isPositive ? 'text-green-600' : 'text-red-600'
}`}
>
{stat.change} desde el mes pasado
</p>
</CardContent>
</Card>
))}
</div>
{isLoadingAnalytics ? (
<div className="text-center p-8">
<p className="text-slate-600">Cargando estadísticas...</p>
</div>
) : null}
<Card>
<CardHeader>
<CardTitle className="flex items-center">
<GraduationCap className="w-5 h-5 mr-2" />
Solicitudes Recientes
</CardTitle>
<CardDescription>
Últimas solicitudes recibidas.{' '}
<Link
to="/admin/teacher-requests"
className="text-blue-600 hover:underline"
>
Ver todas
</Link>
.
</CardDescription>
</CardHeader>
<CardContent>
<div className="space-y-4">
{isLoading ? (
<p>Cargando...</p>
) : (
recentRequests.map((req) => {
const date = new Date(req.date);
const formattedDate = !isNaN(date.getTime())
? date.toLocaleDateString('es-ES', {
day: '2-digit',
month: 'short',
year: 'numeric',
})
: 'Fecha no disponible';
return (
<div
key={req.id}
className="flex items-center justify-between p-3 rounded-lg hover:bg-blue-50/50 transition-colors"
>
<div>
<p className="font-medium text-slate-800">
{req.user.name} {req.user.surname}
</p>
<p className="text-sm text-slate-600">
{req.expertise}
</p>
<p className="text-xs text-slate-500">
{formattedDate}
</p>
</div>
<StatusBadge status={req.state} />
</div>
);
})
)}
</div>
</CardContent>
</Card>
</div>
</div>
);
}