|
1 | 1 | 'use client'; |
2 | 2 |
|
| 3 | +import { useEffect, useState, useCallback } from 'react'; |
3 | 4 | import MetricsCard from '@/components/organization/cards/MetricsCard'; |
4 | 5 | import JudgingParticipant from '@/components/organization/cards/JudgingParticipant'; |
5 | 6 | import { useParams } from 'next/navigation'; |
| 7 | +import { |
| 8 | + getJudgingSubmissions, |
| 9 | + type JudgingSubmission, |
| 10 | +} from '@/lib/api/hackathons'; |
| 11 | +import { Loader2 } from 'lucide-react'; |
| 12 | +import { toast } from 'sonner'; |
| 13 | +import { Button } from '@/components/ui/button'; |
6 | 14 |
|
7 | 15 | export default function JudgingPage() { |
8 | 16 | const params = useParams(); |
9 | | - void params.id; |
10 | | - void params.hackathonId; |
| 17 | + const organizationId = params.id as string; |
| 18 | + const hackathonId = params.hackathonId as string; |
| 19 | + |
| 20 | + const [submissions, setSubmissions] = useState<JudgingSubmission[]>([]); |
| 21 | + const [isLoading, setIsLoading] = useState(true); |
| 22 | + const [page, setPage] = useState(1); |
| 23 | + const [pagination, setPagination] = useState({ |
| 24 | + currentPage: 1, |
| 25 | + totalPages: 1, |
| 26 | + totalItems: 0, |
| 27 | + itemsPerPage: 10, |
| 28 | + hasNext: false, |
| 29 | + hasPrev: false, |
| 30 | + }); |
| 31 | + |
| 32 | + const fetchSubmissions = useCallback( |
| 33 | + async (pageNum = 1) => { |
| 34 | + if (!organizationId || !hackathonId) return; |
| 35 | + |
| 36 | + setIsLoading(true); |
| 37 | + try { |
| 38 | + const response = await getJudgingSubmissions( |
| 39 | + organizationId, |
| 40 | + hackathonId, |
| 41 | + pageNum, |
| 42 | + 10 |
| 43 | + ); |
| 44 | + |
| 45 | + if (response.success) { |
| 46 | + setSubmissions(response.data); |
| 47 | + setPagination(response.pagination); |
| 48 | + setPage(pageNum); |
| 49 | + } |
| 50 | + } catch { |
| 51 | + toast.error('Failed to load submissions'); |
| 52 | + } finally { |
| 53 | + setIsLoading(false); |
| 54 | + } |
| 55 | + }, |
| 56 | + [organizationId, hackathonId] |
| 57 | + ); |
| 58 | + |
| 59 | + useEffect(() => { |
| 60 | + fetchSubmissions(); |
| 61 | + }, [fetchSubmissions]); |
| 62 | + |
| 63 | + const handleSuccess = () => { |
| 64 | + fetchSubmissions(page); |
| 65 | + }; |
| 66 | + |
| 67 | + // Calculate statistics |
| 68 | + const totalSubmissions = pagination.totalItems; |
| 69 | + const averageScore = |
| 70 | + submissions.length > 0 |
| 71 | + ? submissions.reduce((sum, sub) => { |
| 72 | + return sum + (sub.averageScore || 0); |
| 73 | + }, 0) / submissions.length |
| 74 | + : 0; |
| 75 | + const totalJudges = new Set( |
| 76 | + submissions.flatMap(sub => sub.scores.map(score => score.judge._id)) |
| 77 | + ).size; |
11 | 78 |
|
12 | 79 | return ( |
13 | 80 | <div className='bg-background min-h-screen space-y-4 p-8 text-white'> |
14 | 81 | <div className='flex gap-4'> |
15 | | - <MetricsCard /> |
16 | | - <MetricsCard /> |
17 | | - </div> |
18 | | - <div className='flex flex-col gap-4'> |
19 | | - <JudgingParticipant isSubmitted={true} /> |
20 | | - <JudgingParticipant isSubmitted={true} /> |
21 | | - <JudgingParticipant isSubmitted={true} /> |
22 | | - <JudgingParticipant isSubmitted={true} /> |
| 82 | + <MetricsCard |
| 83 | + title='Shortlisted Submissions' |
| 84 | + value={totalSubmissions} |
| 85 | + subtitle={`${submissions.length} displayed`} |
| 86 | + /> |
| 87 | + <MetricsCard |
| 88 | + title='Average Score' |
| 89 | + value={averageScore > 0 ? averageScore.toFixed(2) : 'N/A'} |
| 90 | + subtitle={ |
| 91 | + submissions.length > 0 ? 'Across all submissions' : undefined |
| 92 | + } |
| 93 | + /> |
| 94 | + <MetricsCard |
| 95 | + title='Active Judges' |
| 96 | + value={totalJudges} |
| 97 | + subtitle='Judges who have graded' |
| 98 | + /> |
23 | 99 | </div> |
| 100 | + |
| 101 | + {isLoading ? ( |
| 102 | + <div className='flex items-center justify-center py-12'> |
| 103 | + <Loader2 className='h-8 w-8 animate-spin text-gray-400' /> |
| 104 | + </div> |
| 105 | + ) : submissions.length === 0 ? ( |
| 106 | + <div className='bg-background/8 rounded-lg border border-gray-900 p-8 text-center text-gray-400'> |
| 107 | + No shortlisted submissions found |
| 108 | + </div> |
| 109 | + ) : ( |
| 110 | + <> |
| 111 | + <div className='flex flex-col gap-4'> |
| 112 | + {submissions.map(submission => ( |
| 113 | + <JudgingParticipant |
| 114 | + key={submission.participant._id} |
| 115 | + submission={submission} |
| 116 | + organizationId={organizationId} |
| 117 | + hackathonId={hackathonId} |
| 118 | + onSuccess={handleSuccess} |
| 119 | + /> |
| 120 | + ))} |
| 121 | + </div> |
| 122 | + |
| 123 | + {/* Pagination */} |
| 124 | + {pagination.totalPages > 1 && ( |
| 125 | + <div className='flex items-center justify-center gap-2 pt-4'> |
| 126 | + <Button |
| 127 | + variant='outline' |
| 128 | + onClick={() => fetchSubmissions(page - 1)} |
| 129 | + disabled={!pagination.hasPrev || isLoading} |
| 130 | + className='border-gray-700 text-gray-400 hover:bg-gray-800' |
| 131 | + > |
| 132 | + Previous |
| 133 | + </Button> |
| 134 | + <span className='text-sm text-gray-400'> |
| 135 | + Page {pagination.currentPage} of {pagination.totalPages} |
| 136 | + </span> |
| 137 | + <Button |
| 138 | + variant='outline' |
| 139 | + onClick={() => fetchSubmissions(page + 1)} |
| 140 | + disabled={!pagination.hasNext || isLoading} |
| 141 | + className='border-gray-700 text-gray-400 hover:bg-gray-800' |
| 142 | + > |
| 143 | + Next |
| 144 | + </Button> |
| 145 | + </div> |
| 146 | + )} |
| 147 | + </> |
| 148 | + )} |
24 | 149 | </div> |
25 | 150 | ); |
26 | 151 | } |
0 commit comments