page.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. 'use client'
  2. import { useEffect, useState } from 'react'
  3. import { MainLayout } from '@/components/layout/main-layout'
  4. import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
  5. import { BookOpen, Calendar, ClipboardList, TrendingUp, Clock, CheckCircle, XCircle, Loader2 } from 'lucide-react'
  6. import { toast } from 'sonner'
  7. interface Student {
  8. id: string;
  9. firstName: string;
  10. lastName: string;
  11. admissionNumber: string;
  12. }
  13. interface Stats {
  14. totalClasses: number;
  15. attendanceRate: number;
  16. todayClasses: number;
  17. monthlyAbsences: number;
  18. }
  19. interface TodayScheduleItem {
  20. id: string;
  21. name: string;
  22. section: string;
  23. time: string;
  24. room: string;
  25. professor: string;
  26. }
  27. interface AttendanceHistoryItem {
  28. id: string;
  29. class: string;
  30. date: string;
  31. status: string;
  32. time: string;
  33. statusColor: string;
  34. }
  35. interface ClassOverviewItem {
  36. id: string;
  37. name: string;
  38. section: string;
  39. professor: string;
  40. attendance: number;
  41. totalClasses: number;
  42. attendedClasses: number;
  43. }
  44. interface DashboardData {
  45. student: Student;
  46. stats: Stats;
  47. todaySchedule: TodayScheduleItem[];
  48. recentAttendances: AttendanceHistoryItem[];
  49. classesOverview: ClassOverviewItem[];
  50. }
  51. export default function StudentDashboard() {
  52. const [data, setData] = useState<DashboardData | null>(null)
  53. const [loading, setLoading] = useState(true)
  54. useEffect(() => {
  55. fetchDashboardData()
  56. }, [])
  57. const fetchDashboardData = async () => {
  58. try {
  59. setLoading(true)
  60. const response = await fetch('/api/student/dashboard')
  61. if (!response.ok) {
  62. throw new Error('Error al cargar los datos del dashboard')
  63. }
  64. const dashboardData: DashboardData = await response.json()
  65. setData(dashboardData)
  66. } catch (error) {
  67. console.error('Error:', error)
  68. toast.error('Error al cargar los datos del dashboard')
  69. } finally {
  70. setLoading(false)
  71. }
  72. }
  73. if (loading) {
  74. return (
  75. <MainLayout
  76. title="Dashboard Estudiante"
  77. subtitle="Mi progreso académico y asistencia"
  78. requiredRole="STUDENT"
  79. >
  80. <div className="flex items-center justify-center h-64">
  81. <Loader2 className="h-8 w-8 animate-spin" />
  82. </div>
  83. </MainLayout>
  84. )
  85. }
  86. if (!data) {
  87. return (
  88. <MainLayout
  89. title="Dashboard Estudiante"
  90. subtitle="Mi progreso académico y asistencia"
  91. requiredRole="STUDENT"
  92. >
  93. <div className="text-center py-8">
  94. <p className="text-muted-foreground">No se pudieron cargar los datos del dashboard.</p>
  95. </div>
  96. </MainLayout>
  97. )
  98. }
  99. const stats = [
  100. {
  101. title: 'Mis Clases',
  102. value: data.stats.totalClasses.toString(),
  103. description: 'Clases matriculadas',
  104. icon: BookOpen,
  105. color: 'text-blue-600',
  106. bgColor: 'bg-blue-100'
  107. },
  108. {
  109. title: 'Asistencia General',
  110. value: `${data.stats.attendanceRate}%`,
  111. description: 'Promedio del semestre',
  112. icon: TrendingUp,
  113. color: 'text-green-600',
  114. bgColor: 'bg-green-100'
  115. },
  116. {
  117. title: 'Clases Hoy',
  118. value: data.stats.todayClasses.toString(),
  119. description: 'Clases programadas',
  120. icon: Calendar,
  121. color: 'text-purple-600',
  122. bgColor: 'bg-purple-100'
  123. },
  124. {
  125. title: 'Faltas Este Mes',
  126. value: data.stats.monthlyAbsences.toString(),
  127. description: 'Inasistencias registradas',
  128. icon: XCircle,
  129. color: 'text-red-600',
  130. bgColor: 'bg-red-100'
  131. }
  132. ]
  133. return (
  134. <MainLayout
  135. title="Dashboard Estudiante"
  136. subtitle={`Bienvenido/a ${data.student.firstName} ${data.student.lastName}`}
  137. requiredRole="STUDENT"
  138. >
  139. <div className="space-y-6">
  140. {/* Estadísticas */}
  141. <div className="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-4">
  142. {stats.map((stat) => {
  143. const Icon = stat.icon
  144. return (
  145. <Card key={stat.title}>
  146. <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
  147. <CardTitle className="text-sm font-medium">
  148. {stat.title}
  149. </CardTitle>
  150. <div className={`p-2 rounded-full ${stat.bgColor}`}>
  151. <Icon className={`h-4 w-4 ${stat.color}`} />
  152. </div>
  153. </CardHeader>
  154. <CardContent>
  155. <div className="text-2xl font-bold">{stat.value}</div>
  156. <p className="text-xs text-muted-foreground">
  157. {stat.description}
  158. </p>
  159. </CardContent>
  160. </Card>
  161. )
  162. })}
  163. </div>
  164. <div className="grid grid-cols-1 gap-6 lg:grid-cols-3">
  165. {/* Horario de Hoy */}
  166. <Card>
  167. <CardHeader>
  168. <CardTitle className="flex items-center gap-2">
  169. <Clock className="h-5 w-5" />
  170. Horario de Hoy
  171. </CardTitle>
  172. <CardDescription>
  173. Clases programadas para hoy
  174. </CardDescription>
  175. </CardHeader>
  176. <CardContent>
  177. <div className="space-y-4">
  178. {data.todaySchedule.length > 0 ? (
  179. data.todaySchedule.map((classItem) => (
  180. <div key={classItem.id} className="p-3 border rounded-lg">
  181. <div className="space-y-2">
  182. <p className="text-sm font-medium">{classItem.name}</p>
  183. <p className="text-xs text-muted-foreground">
  184. {classItem.section} • {classItem.room}
  185. </p>
  186. <p className="text-xs text-muted-foreground">
  187. {classItem.time} • Prof. {classItem.professor}
  188. </p>
  189. </div>
  190. </div>
  191. ))
  192. ) : (
  193. <p className="text-sm text-muted-foreground text-center py-4">
  194. No hay clases programadas para hoy
  195. </p>
  196. )}
  197. </div>
  198. </CardContent>
  199. </Card>
  200. {/* Historial de Asistencia */}
  201. <Card>
  202. <CardHeader>
  203. <CardTitle className="flex items-center gap-2">
  204. <ClipboardList className="h-5 w-5" />
  205. Asistencia Reciente
  206. </CardTitle>
  207. <CardDescription>
  208. Últimos registros de asistencia
  209. </CardDescription>
  210. </CardHeader>
  211. <CardContent>
  212. <div className="space-y-3">
  213. {data.recentAttendances.length > 0 ? (
  214. data.recentAttendances.map((record) => (
  215. <div key={record.id} className="flex items-center justify-between">
  216. <div className="space-y-1">
  217. <p className="text-sm font-medium">{record.class}</p>
  218. <p className="text-xs text-muted-foreground">
  219. {record.date} • {record.time}
  220. </p>
  221. </div>
  222. <div className="flex items-center gap-2">
  223. {record.status === 'Presente' || record.status === 'Justificado' ? (
  224. <CheckCircle className="h-4 w-4 text-green-600" />
  225. ) : (
  226. <XCircle className="h-4 w-4 text-red-600" />
  227. )}
  228. <span className={`text-xs font-medium ${record.statusColor}`}>
  229. {record.status}
  230. </span>
  231. </div>
  232. </div>
  233. ))
  234. ) : (
  235. <p className="text-sm text-muted-foreground text-center py-4">
  236. No hay registros de asistencia
  237. </p>
  238. )}
  239. </div>
  240. </CardContent>
  241. </Card>
  242. {/* Resumen por Clase */}
  243. <Card>
  244. <CardHeader>
  245. <CardTitle className="flex items-center gap-2">
  246. <BookOpen className="h-5 w-5" />
  247. Mis Clases
  248. </CardTitle>
  249. <CardDescription>
  250. Resumen de asistencia por materia
  251. </CardDescription>
  252. </CardHeader>
  253. <CardContent>
  254. <div className="space-y-4">
  255. {data.classesOverview.length > 0 ? (
  256. data.classesOverview.map((classItem) => (
  257. <div key={classItem.id} className="space-y-2">
  258. <div className="flex justify-between items-start">
  259. <div>
  260. <p className="text-sm font-medium">{classItem.name}</p>
  261. <p className="text-xs text-muted-foreground">
  262. {classItem.section} • Prof. {classItem.professor}
  263. </p>
  264. </div>
  265. <span className={`text-xs font-medium ${
  266. classItem.attendance >= 90
  267. ? 'text-green-600'
  268. : classItem.attendance >= 80
  269. ? 'text-yellow-600'
  270. : 'text-red-600'
  271. }`}>
  272. {classItem.attendance}%
  273. </span>
  274. </div>
  275. <div className="w-full bg-gray-200 rounded-full h-2">
  276. <div
  277. className={`h-2 rounded-full ${
  278. classItem.attendance >= 90
  279. ? 'bg-green-600'
  280. : classItem.attendance >= 80
  281. ? 'bg-yellow-600'
  282. : 'bg-red-600'
  283. }`}
  284. style={{ width: `${classItem.attendance}%` }}
  285. ></div>
  286. </div>
  287. <p className="text-xs text-muted-foreground">
  288. {classItem.attendedClasses}/{classItem.totalClasses} clases asistidas
  289. </p>
  290. </div>
  291. ))
  292. ) : (
  293. <p className="text-sm text-muted-foreground text-center py-4">
  294. No hay clases matriculadas
  295. </p>
  296. )}
  297. </div>
  298. </CardContent>
  299. </Card>
  300. </div>
  301. </div>
  302. </MainLayout>
  303. )
  304. }