| [875f7d6] | 1 | "use client"
|
|---|
| [b67f274] | 2 | import { useEffect, useMemo, useState } from 'react';
|
|---|
| [50f2fb4] | 3 | import { useTranslation } from 'react-i18next';
|
|---|
| [875f7d6] | 4 | import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|---|
| 5 | import { faSort, faSortUp, faSortDown } from '@fortawesome/free-solid-svg-icons';
|
|---|
| [b67f274] | 6 | import { getAccessToken } from '@/lib/auth';
|
|---|
| [8496f3c] | 7 | import { apiUrl } from '@/lib/api';
|
|---|
| [b67f274] | 8 |
|
|---|
| 9 | type PassedSubject = {
|
|---|
| 10 | id: number;
|
|---|
| 11 | subjectId: number;
|
|---|
| 12 | code: string;
|
|---|
| 13 | subject: string;
|
|---|
| 14 | credits: number;
|
|---|
| 15 | grade: number;
|
|---|
| 16 | gradeText: string;
|
|---|
| 17 | date: string; // DD.MM.YYYY
|
|---|
| 18 | semester: string;
|
|---|
| 19 | professor: string;
|
|---|
| 20 | };
|
|---|
| 21 |
|
|---|
| 22 | type PassedSubjectsResponse = {
|
|---|
| 23 | passedSubjects: PassedSubject[];
|
|---|
| 24 | };
|
|---|
| 25 |
|
|---|
| 26 | type ExamRow = {
|
|---|
| 27 | id: number;
|
|---|
| 28 | code: string;
|
|---|
| 29 | subject: string;
|
|---|
| 30 | date: string;
|
|---|
| 31 | semester: string;
|
|---|
| 32 | credits: number;
|
|---|
| 33 | grade: number;
|
|---|
| 34 | };
|
|---|
| [875f7d6] | 35 |
|
|---|
| 36 | const Exams = () => {
|
|---|
| [50f2fb4] | 37 | const { t } = useTranslation();
|
|---|
| [875f7d6] | 38 | const [sortField, setSortField] = useState<'grade' | 'date' | null>(null);
|
|---|
| 39 | const [sortOrder, setSortOrder] = useState<'asc' | 'desc' | null>(null);
|
|---|
| [b67f274] | 40 | const [exams, setExams] = useState<ExamRow[]>([]);
|
|---|
| 41 | const [isLoading, setIsLoading] = useState(true);
|
|---|
| 42 | const [errorMessage, setErrorMessage] = useState<string | null>(null);
|
|---|
| [875f7d6] | 43 |
|
|---|
| [b67f274] | 44 | useEffect(() => {
|
|---|
| 45 | let cancelled = false;
|
|---|
| 46 |
|
|---|
| 47 | async function load() {
|
|---|
| 48 | setIsLoading(true);
|
|---|
| 49 | setErrorMessage(null);
|
|---|
| 50 |
|
|---|
| 51 | const token = getAccessToken();
|
|---|
| 52 | if (!token) {
|
|---|
| 53 | setErrorMessage('Not authenticated. Please login again.');
|
|---|
| 54 | setIsLoading(false);
|
|---|
| 55 | return;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | try {
|
|---|
| [8496f3c] | 59 | const response = await fetch(apiUrl('/api/user/getPassedSubjects'), {
|
|---|
| [b67f274] | 60 | method: 'GET',
|
|---|
| 61 | headers: {
|
|---|
| 62 | Authorization: `Bearer ${token}`,
|
|---|
| 63 | },
|
|---|
| 64 | });
|
|---|
| 65 |
|
|---|
| 66 | if (!response.ok) {
|
|---|
| 67 | const text = await response.text().catch(() => '');
|
|---|
| 68 | throw new Error(text || `Failed to load passed subjects (${response.status})`);
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | const data = (await response.json()) as PassedSubjectsResponse;
|
|---|
| 72 | const rows: ExamRow[] = (data?.passedSubjects ?? []).map((s) => ({
|
|---|
| 73 | id: s.id,
|
|---|
| 74 | code: s.code,
|
|---|
| 75 | subject: s.subject,
|
|---|
| 76 | date: s.date,
|
|---|
| 77 | semester: s.semester,
|
|---|
| 78 | credits: s.credits,
|
|---|
| 79 | grade: s.grade,
|
|---|
| 80 | }));
|
|---|
| [875f7d6] | 81 |
|
|---|
| [b67f274] | 82 | if (!cancelled) setExams(rows);
|
|---|
| 83 | } catch (err) {
|
|---|
| 84 | if (!cancelled) {
|
|---|
| 85 | setErrorMessage(err instanceof Error ? err.message : 'Failed to load passed subjects.');
|
|---|
| 86 | }
|
|---|
| 87 | } finally {
|
|---|
| 88 | if (!cancelled) setIsLoading(false);
|
|---|
| 89 | }
|
|---|
| [875f7d6] | 90 | }
|
|---|
| [b67f274] | 91 |
|
|---|
| 92 | void load();
|
|---|
| 93 |
|
|---|
| 94 | return () => {
|
|---|
| 95 | cancelled = true;
|
|---|
| 96 | };
|
|---|
| 97 | }, []);
|
|---|
| 98 |
|
|---|
| 99 | const stats = useMemo(() => {
|
|---|
| 100 | const passed = exams.length;
|
|---|
| 101 | const creditsCurrent = exams.reduce((sum, s) => sum + (typeof s.credits === 'number' ? s.credits : 0), 0);
|
|---|
| 102 | const numericGrades = exams.map((e) => (typeof e.grade === 'number' ? e.grade : null)).filter((g): g is number => g !== null);
|
|---|
| 103 | const average =
|
|---|
| 104 | numericGrades.length > 0
|
|---|
| 105 | ? numericGrades.reduce((sum, g) => sum + g, 0) / numericGrades.length
|
|---|
| 106 | : 0;
|
|---|
| 107 |
|
|---|
| 108 | const totalCredits = 240;
|
|---|
| 109 | const totalExams = 40;
|
|---|
| 110 | return {
|
|---|
| 111 | average: Number.isFinite(average) ? Number(average.toFixed(2)) : 0,
|
|---|
| 112 | credits: { current: creditsCurrent, total: totalCredits },
|
|---|
| 113 | passed,
|
|---|
| 114 | remaining: Math.max(0, totalExams - passed),
|
|---|
| 115 | };
|
|---|
| 116 | }, [exams]);
|
|---|
| 117 |
|
|---|
| 118 | const getGradeColor = (grade: number) => {
|
|---|
| [ba52069] | 119 | if (grade >= 9) return "bg-green-500/10 text-green-600 dark:text-green-400 border-green-500/20";
|
|---|
| 120 | if (grade >= 8) return "bg-yellow-500/10 text-yellow-600 dark:text-yellow-400 border-yellow-500/20";
|
|---|
| 121 | if (grade >= 6) return "bg-orange-500/10 text-orange-600 dark:text-orange-400 border-orange-500/20";
|
|---|
| 122 | return "bg-red-500/10 text-red-600 dark:text-red-400 border-red-500/20";
|
|---|
| [875f7d6] | 123 | };
|
|---|
| 124 |
|
|---|
| 125 | const handleSortByGrade = () => {
|
|---|
| 126 | if (sortField === 'grade') {
|
|---|
| 127 | if (sortOrder === 'desc') {
|
|---|
| 128 | setSortOrder('asc');
|
|---|
| 129 | } else if (sortOrder === 'asc') {
|
|---|
| 130 | setSortField(null);
|
|---|
| 131 | setSortOrder(null);
|
|---|
| 132 | } else {
|
|---|
| 133 | setSortOrder('desc');
|
|---|
| 134 | }
|
|---|
| 135 | } else {
|
|---|
| 136 | setSortField('grade');
|
|---|
| 137 | setSortOrder('desc');
|
|---|
| 138 | }
|
|---|
| 139 | };
|
|---|
| 140 |
|
|---|
| 141 | const handleSortByDate = () => {
|
|---|
| 142 | if (sortField === 'date') {
|
|---|
| 143 | if (sortOrder === 'desc') {
|
|---|
| 144 | setSortOrder('asc');
|
|---|
| 145 | } else if (sortOrder === 'asc') {
|
|---|
| 146 | setSortField(null);
|
|---|
| 147 | setSortOrder(null);
|
|---|
| 148 | } else {
|
|---|
| 149 | setSortOrder('desc');
|
|---|
| 150 | }
|
|---|
| 151 | } else {
|
|---|
| 152 | setSortField('date');
|
|---|
| 153 | setSortOrder('desc');
|
|---|
| 154 | }
|
|---|
| 155 | };
|
|---|
| 156 |
|
|---|
| 157 | const getSortedExams = () => {
|
|---|
| 158 | if (!sortField || !sortOrder) return exams;
|
|---|
| 159 |
|
|---|
| 160 | return [...exams].sort((a, b) => {
|
|---|
| 161 | if (sortField === 'grade') {
|
|---|
| [b67f274] | 162 | return sortOrder === 'asc' ? a.grade - b.grade : b.grade - a.grade;
|
|---|
| [875f7d6] | 163 | }
|
|---|
| 164 |
|
|---|
| 165 | if (sortField === 'date') {
|
|---|
| 166 | // Convert date strings to Date objects for proper sorting
|
|---|
| 167 | const dateA = new Date(a.date.split('.').reverse().join('-')); // Convert DD.MM.YYYY to YYYY-MM-DD
|
|---|
| 168 | const dateB = new Date(b.date.split('.').reverse().join('-'));
|
|---|
| 169 |
|
|---|
| 170 | return sortOrder === 'asc' ? dateA.getTime() - dateB.getTime() : dateB.getTime() - dateA.getTime();
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | return 0;
|
|---|
| 174 | });
|
|---|
| 175 | };
|
|---|
| 176 |
|
|---|
| 177 | const getSortIcon = (field: 'grade' | 'date') => {
|
|---|
| 178 | if (sortField === field) {
|
|---|
| 179 | if (sortOrder === 'asc') return faSortUp;
|
|---|
| 180 | if (sortOrder === 'desc') return faSortDown;
|
|---|
| 181 | }
|
|---|
| 182 | return faSort;
|
|---|
| 183 | };
|
|---|
| 184 |
|
|---|
| [b67f274] | 185 | const creditsPercentage = stats.credits.total > 0 ? (stats.credits.current / stats.credits.total) * 100 : 0;
|
|---|
| 186 |
|
|---|
| 187 | if (isLoading) {
|
|---|
| 188 | return (
|
|---|
| [ba52069] | 189 | <div className="bg-card rounded-lg shadow-sm p-6">
|
|---|
| [50f2fb4] | 190 | {t('loading', 'Loading...')}
|
|---|
| [b67f274] | 191 | </div>
|
|---|
| 192 | );
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | if (errorMessage) {
|
|---|
| 196 | return (
|
|---|
| [ba52069] | 197 | <div className="bg-card rounded-lg shadow-sm p-6">
|
|---|
| 198 | <div className="rounded-lg border border-red-500/20 bg-red-500/10 px-4 py-3 text-sm text-red-600 dark:text-red-400">
|
|---|
| [50f2fb4] | 199 | {t('failed_to_load_exams', errorMessage)}
|
|---|
| [b67f274] | 200 | </div>
|
|---|
| 201 | </div>
|
|---|
| 202 | );
|
|---|
| 203 | }
|
|---|
| [875f7d6] | 204 |
|
|---|
| 205 | return (
|
|---|
| [ba52069] | 206 | <div className="bg-card rounded-lg shadow-sm p-6 space-y-6">
|
|---|
| [875f7d6] | 207 | {/* Statistics Section */}
|
|---|
| 208 | <div className="space-y-4">
|
|---|
| 209 | <div className="grid grid-cols-2 md:grid-cols-4 gap-6">
|
|---|
| 210 | {/* Average Grade */}
|
|---|
| 211 | <div className="text-center">
|
|---|
| [ba52069] | 212 | <div className="text-3xl font-bold text-card-foreground">{stats.average}</div>
|
|---|
| 213 | <div className="text-sm text-muted-foreground mt-1">{t('average', 'Просек')}</div>
|
|---|
| [875f7d6] | 214 | </div>
|
|---|
| 215 |
|
|---|
| 216 | {/* Credits */}
|
|---|
| 217 | <div className="text-center">
|
|---|
| [ba52069] | 218 | <div className="text-3xl font-bold text-card-foreground">
|
|---|
| [875f7d6] | 219 | {stats.credits.current}
|
|---|
| [ba52069] | 220 | <span className="text-lg text-muted-foreground">/{stats.credits.total}</span>
|
|---|
| [875f7d6] | 221 | </div>
|
|---|
| [ba52069] | 222 | <div className="text-sm text-muted-foreground mt-1">{t('credits', 'Кредити')}</div>
|
|---|
| [875f7d6] | 223 | </div>
|
|---|
| 224 |
|
|---|
| 225 | {/* Passed Exams */}
|
|---|
| 226 | <div className="text-center">
|
|---|
| [ba52069] | 227 | <div className="text-3xl font-bold text-card-foreground">{stats.passed}</div>
|
|---|
| 228 | <div className="text-sm text-muted-foreground mt-1">{t('passed', 'Положени')}</div>
|
|---|
| [875f7d6] | 229 | </div>
|
|---|
| 230 |
|
|---|
| 231 | {/* Remaining Exams */}
|
|---|
| 232 | <div className="text-center">
|
|---|
| [ba52069] | 233 | <div className="text-3xl font-bold text-card-foreground">{stats.remaining}</div>
|
|---|
| 234 | <div className="text-sm text-muted-foreground mt-1">{t('remaining', 'Останато')}</div>
|
|---|
| [875f7d6] | 235 | </div>
|
|---|
| 236 | </div>
|
|---|
| [50f2fb4] | 237 |
|
|---|
| [875f7d6] | 238 | {/* Progress Bar spanning entire statistics section */}
|
|---|
| [ba52069] | 239 | <div className="w-full bg-secondary rounded-full h-2">
|
|---|
| [50f2fb4] | 240 | <div
|
|---|
| [ba52069] | 241 | className="bg-primary h-2 rounded-full transition-all duration-300"
|
|---|
| [875f7d6] | 242 | style={{ width: `${creditsPercentage}%` }}
|
|---|
| 243 | ></div>
|
|---|
| 244 | </div>
|
|---|
| 245 | </div>
|
|---|
| 246 |
|
|---|
| 247 | {/* Exams Table */}
|
|---|
| 248 | <div>
|
|---|
| [ba52069] | 249 | <h3 className="text-xl font-semibold text-card-foreground mb-4">{t('exams', 'Испити')}</h3>
|
|---|
| [50f2fb4] | 250 |
|
|---|
| [875f7d6] | 251 | <div className="overflow-x-auto">
|
|---|
| 252 | <table className="min-w-full">
|
|---|
| 253 | <thead>
|
|---|
| [ba52069] | 254 | <tr className="border-b border-border">
|
|---|
| 255 | <th className="text-left py-3 px-4 text-sm font-medium text-muted-foreground">#</th>
|
|---|
| 256 | <th className="text-left py-3 px-4 text-sm font-medium text-muted-foreground">{t('subject', 'Предмет')}</th>
|
|---|
| 257 | <th className="text-left py-3 px-4 text-sm font-medium text-muted-foreground">{t('semester', 'Семестар')}</th>
|
|---|
| 258 | <th className="text-left py-3 px-4 text-sm font-medium text-muted-foreground">{t('credits', 'Кредити')}</th>
|
|---|
| 259 | <th className="text-left py-3 px-4 text-sm font-medium text-muted-foreground">
|
|---|
| [50f2fb4] | 260 | <button
|
|---|
| [875f7d6] | 261 | onClick={handleSortByDate}
|
|---|
| [ba52069] | 262 | className="flex items-center gap-2 px-2 py-1 rounded-md hover:bg-accent hover:text-accent-foreground transition-all duration-200 transform hover:scale-105"
|
|---|
| [875f7d6] | 263 | >
|
|---|
| [50f2fb4] | 264 | {t('date', 'Датум')}
|
|---|
| 265 | <FontAwesomeIcon
|
|---|
| 266 | icon={getSortIcon('date')}
|
|---|
| [875f7d6] | 267 | className={`text-xs transition-colors duration-200 ${
|
|---|
| [ba52069] | 268 | sortField === 'date' ? 'text-primary' : 'text-muted-foreground group-hover:text-foreground'
|
|---|
| [875f7d6] | 269 | }`}
|
|---|
| 270 | />
|
|---|
| 271 | </button>
|
|---|
| 272 | </th>
|
|---|
| [ba52069] | 273 | <th className="text-left py-3 px-4 text-sm font-medium text-muted-foreground">
|
|---|
| [50f2fb4] | 274 | <button
|
|---|
| [875f7d6] | 275 | onClick={handleSortByGrade}
|
|---|
| [ba52069] | 276 | className="flex items-center gap-2 px-2 py-1 rounded-md hover:bg-accent hover:text-accent-foreground transition-all duration-200 transform hover:scale-105"
|
|---|
| [875f7d6] | 277 | >
|
|---|
| [50f2fb4] | 278 | {t('grade', 'Оценка')}
|
|---|
| 279 | <FontAwesomeIcon
|
|---|
| 280 | icon={getSortIcon('grade')}
|
|---|
| [875f7d6] | 281 | className={`text-xs transition-colors duration-200 ${
|
|---|
| [ba52069] | 282 | sortField === 'grade' ? 'text-primary' : 'text-muted-foreground group-hover:text-foreground'
|
|---|
| [875f7d6] | 283 | }`}
|
|---|
| 284 | />
|
|---|
| 285 | </button>
|
|---|
| 286 | </th>
|
|---|
| 287 | </tr>
|
|---|
| 288 | </thead>
|
|---|
| [ba52069] | 289 | <tbody className="divide-y divide-border">
|
|---|
| [875f7d6] | 290 | {getSortedExams().map((exam) => (
|
|---|
| [ba52069] | 291 | <tr key={exam.id} className="hover:bg-accent/50 transition-colors duration-150">
|
|---|
| 292 | <td className="py-4 px-4 text-sm text-card-foreground">{exam.id}</td>
|
|---|
| [4fe4582] | 293 | <td className="py-4 px-4 text-sm text-card-foreground font-medium">{t(exam.subject, exam.subject)}</td>
|
|---|
| 294 | <td className="py-4 px-4 text-sm text-muted-foreground">{t(exam.semester, exam.semester)}</td>
|
|---|
| [ba52069] | 295 | <td className="py-4 px-4 text-sm text-muted-foreground">{exam.credits}</td>
|
|---|
| 296 | <td className="py-4 px-4 text-sm text-muted-foreground">{exam.date}</td>
|
|---|
| [875f7d6] | 297 | <td className="py-4 px-4">
|
|---|
| 298 | <span className={`inline-flex items-center px-2.5 py-1 rounded-md text-sm font-medium border ${getGradeColor(exam.grade)}`}>
|
|---|
| 299 | {exam.grade}
|
|---|
| 300 | </span>
|
|---|
| 301 | </td>
|
|---|
| 302 | </tr>
|
|---|
| 303 | ))}
|
|---|
| 304 | </tbody>
|
|---|
| 305 | </table>
|
|---|
| 306 | </div>
|
|---|
| 307 | </div>
|
|---|
| 308 | </div>
|
|---|
| 309 | );
|
|---|
| 310 | };
|
|---|
| 311 |
|
|---|
| 312 | export default Exams;
|
|---|