1
1
"use client" ;
2
2
3
3
import { useState } from "react" ;
4
- import Skeleton from "react-loading-skeleton" ;
5
4
6
5
import type { Schema } from "@/amplify/data/resource" ;
6
+ import KevinLoadingRing from "@/components/KevinLoadingRing" ;
7
7
import { useUser } from "@/components/contexts/UserContext" ;
8
8
import ModalPopup from "@/components/judging/ModalPopup" ;
9
9
import ScoresTable from "@/components/judging/ScoresTable" ;
@@ -21,6 +21,8 @@ export default function JudgingTable({
21
21
> ;
22
22
} ) {
23
23
const [ selectedTeam , setSelectedTeamId ] = useState ( "" ) ;
24
+ const [ teamName , setTeamName ] = useState ( "" ) ;
25
+ const [ teamsLeft , setTeamsLeft ] = useState ( 0 ) ;
24
26
25
27
const { currentUser } = useUser ( ) ;
26
28
const { data : roomData , isFetching : roomIsFetching } = useQuery ( {
@@ -34,7 +36,9 @@ export default function JudgingTable({
34
36
35
37
return data ;
36
38
} ,
39
+ enabled : ! ! currentUser . JUDGE_roomId ,
37
40
} ) ;
41
+
38
42
const { data : teamsForRoomData , isFetching : teamsForRoomIsFetching } =
39
43
useQuery ( {
40
44
queryKey : [ "TeamsForRoom" , roomData ?. id ] ,
@@ -49,43 +53,61 @@ export default function JudgingTable({
49
53
return teams ;
50
54
} ,
51
55
} ) ;
52
- const isFetching = roomIsFetching || teamsForRoomIsFetching ;
56
+ const isFetching = roomIsFetching && teamsForRoomIsFetching ;
53
57
if ( isFetching || ! roomData || ! teamsForRoomData ) {
54
- return (
55
- < div className = "flex flex-1" >
56
- < Skeleton className = "h-full" containerClassName = "flex-1" />
57
- </ div >
58
+ return < KevinLoadingRing /> ;
59
+ }
60
+ async function getFilteredTeamsCount ( ) {
61
+ // https://medium.com/@debbs 119/array-filter-and-array-map-with-async-functions-9636e1ae8d6e --> why it needs to map to a boolean array first
62
+ if ( ! teamsForRoomData ) {
63
+ return ;
64
+ }
65
+ const boolArray = await Promise . all (
66
+ teamsForRoomData ?. map ( async ( team ) => {
67
+ const scores = await team ?. scores ( ) ;
68
+ return (
69
+ scores ?. data . filter ( ( score ) => score . judgeId === currentUser . username )
70
+ . length === 0
71
+ ) ;
72
+ } ) ,
73
+ ) ;
74
+ setTeamsLeft (
75
+ teamsForRoomData ?. filter ( ( _ , index ) => boolArray [ index ] ) . length ,
58
76
) ;
59
77
}
60
78
79
+ getFilteredTeamsCount ( ) ;
80
+
61
81
const panelData = [
62
82
{
63
83
icon : "/svgs/judging/team_icon.svg" ,
64
84
alt : "Teams assigned icon" ,
65
85
stat : teamsForRoomData . length ,
66
- text : `Teams Assigned to ${ roomData . name } ` ,
86
+ text :
87
+ teamsForRoomData . length === 1
88
+ ? `Team Assigned to ${ roomData . name } `
89
+ : `Teams Assigned to ${ roomData . name } ` ,
67
90
} ,
68
91
{
69
92
icon : "/svgs/judging/teams_left.svg" ,
70
93
alt : "Teams left icon" ,
71
- stat : teamsForRoomData . filter (
72
- async ( team ) =>
73
- ( await team ?. scores ( ) ) ?. data . filter (
74
- ( score ) => score . judgeId === currentUser . username ,
75
- ) . length === 0 ,
76
- ) . length ,
77
- text : "Teams Left To Score" ,
94
+ stat : teamsLeft ,
95
+ text : teamsLeft === 1 ? "Team Left to Score" : "Teams Left to Score" ,
78
96
} ,
79
97
] ;
98
+
80
99
const handleCreateScoreClick = ( teamId : string ) => {
81
- setSelectedTeamId ( teamId ) ;
82
- } ;
83
- const handleEditScoreClick = ( teamId : string ) => {
84
- setSelectedTeamId ( teamId ) ;
100
+ const selectedTeam = teamsForRoomData . find ( ( team ) => team ?. id === teamId ) ;
101
+
102
+ if ( selectedTeam ) {
103
+ setSelectedTeamId ( teamId ) ;
104
+ setTeamName ( selectedTeam . name ) ;
105
+ }
85
106
} ;
86
107
87
108
const closeModal = ( ) => {
88
109
setSelectedTeamId ( "" ) ;
110
+ setTeamName ( "" ) ;
89
111
} ;
90
112
91
113
return (
@@ -105,17 +127,18 @@ export default function JudgingTable({
105
127
< ScoresTable
106
128
tableData = { teamsForRoomData as Schema [ "Team" ] [ "type" ] [ ] }
107
129
onCreateScoreClick = { handleCreateScoreClick }
108
- onEditScoreClick = { handleEditScoreClick }
109
130
colorScheme = "pink"
110
131
entriesPerPage = { 150 }
111
132
hackathonData = { hackathonData }
112
133
/>
113
134
</ div >
135
+
114
136
{ selectedTeam !== "" && (
115
137
< ModalPopup
116
138
hackathon = { hackathonData }
117
139
onClose = { closeModal }
118
140
teamId = { selectedTeam }
141
+ teamName = { teamName }
119
142
/>
120
143
) }
121
144
</ >
0 commit comments