1
1
"use client" ;
2
2
3
- import { useState } from "react" ;
3
+ import { useEffect , useState } from "react" ;
4
4
import { Metadata } from "next" ;
5
5
import Image from "next/image" ;
6
6
import { Button } from "@/components/ui/button" ;
@@ -19,8 +19,11 @@ import {
19
19
import { ScrollArea } from "@/components/ui/scroll-area" ;
20
20
import { Separator } from "@/components/ui/separator" ;
21
21
import { Tabs , TabsContent , TabsList , TabsTrigger } from "@/components/ui/tabs" ;
22
+ import { assignTasks } from "@/lib/utils/autoassign" ;
23
+ import { trpc } from "@/lib/utils/trpc" ;
22
24
import { DndContext , DragOverlay , useDroppable } from "@dnd-kit/core" ;
23
25
26
+ import { addAssignment , addMatches , getEmails } from "./actions" ;
24
27
import { AssignmentCard , MemberCard } from "./components/cards" ;
25
28
import { CalendarDateRangePicker } from "./components/date-range-picker" ;
26
29
import { MainNav } from "./components/main-nav" ;
@@ -33,96 +36,262 @@ import { UserNav } from "./components/user-nav";
33
36
const tags = Array . from ( { length : 50 } ) . map (
34
37
( _ , i , a ) => `v1.2.0-beta.${ a . length - i } ` ,
35
38
) ;
36
-
37
- export default function Assignments ( ) {
39
+ function autoAssign (
40
+ assignments : string [ ] ,
41
+ members : { [ key : string ] : string [ ] } ,
42
+ setMembers : ( members : { [ key : string ] : string [ ] } ) => void ,
43
+ setAssignments : ( assignments : string [ ] ) => void ,
44
+ ) {
45
+ // useEffect(() => {
46
+ const autoassign = assignTasks ( assignments , Object . keys ( members ) ) ;
47
+ console . log ( autoassign , typeof autoassign ) ;
48
+ setMembers ( autoassign ) ;
49
+ setAssignments ( [ ] ) ;
50
+ // }, []);
51
+ }
52
+ function Assignments ( { selectedEvent } ) {
38
53
// XXX: Use real data via tRPC
39
- const [ members , setMembers ] = useState < { [ key : string ] : string [ ] } > (
40
- Object . fromEntries ( tags . map ( ( x ) => [ `${ x } M` , [ ] ] ) ) ,
41
- ) ;
42
- const [ assignments , setAssignments ] = useState ( tags . map ( ( x ) => `${ x } A` ) ) ;
54
+ // const [members, setMembers] = useState<{ [key: string]: string[] }>(
55
+ // Object.fromEntries(tags.map((x) => [`${x}M`, []])),
56
+ // );
57
+ const [ members , setMembers ] = useState < { [ key : string ] : string [ ] } > ( { } ) ;
58
+ // Object.fromEntries(tags.map((x) => [`${x}M`, []])),
59
+
60
+ //use useEffect to make a supabase request from the auth tabke to fill it with emails
61
+ useEffect ( ( ) => {
62
+ async function fetchData ( ) {
63
+ getEmails ( setMembers ) ;
64
+ console . log ( "HEEEEE: " , members ) ;
65
+ }
66
+ fetchData ( ) ;
67
+ } , [ ] ) ;
68
+ const [ assignments , setAssignments ] = useState < string [ ] > ( [ ] ) ; // Updated t
43
69
const [ activeId , setActiveId ] = useState < string | null > ( null ) ;
44
70
// Inverted members
45
71
const reverseAssignmentToMembers = Object . fromEntries (
46
72
Object . entries ( members ) . flatMap ( ( [ k , v ] ) => v . map ( ( x ) => [ x , k ] ) ) ,
47
73
) ;
48
74
// const [parent, setParent] = useState(null);
75
+ // const { data, isLoading } = trpc.tba.teamEvents.useQuery({
76
+ // teamKey: "frc3256",
77
+ // year: 2023,
78
+ // });
79
+
80
+ const { data, isLoading } = trpc . tba . eventMatches . useQuery ( {
81
+ teamKey : "frc3256" ,
82
+ eventKey : selectedEvent ,
83
+ } ) ;
84
+ if ( ! isLoading ) {
85
+ console . log ( "DATA: " , data ) ;
86
+ console . log ( "SELECTED EVENT: " , selectedEvent ) ;
87
+ }
88
+ useEffect ( ( ) => {
89
+ if ( ! isLoading && data ) {
90
+ // Extract relevant information from the data and generate assignments
91
+ const matches = data . map ( ( match : any ) => ( {
92
+ match_num : match . match_num ,
93
+ // match_key: match.match_key,
94
+ alliances : match . alliances ,
95
+ } ) ) ;
96
+ const matchKeys = data . map ( ( match : any ) => ( {
97
+ match_key : match . match_key ,
98
+ event : selectedEvent ,
99
+ } ) ) ;
100
+ console . log ( "MATCHES: " , matchKeys ) ;
101
+ // for(int i = 0; i < matchKeys.length; i++) {
102
+ // addMatches({ match: matchKeys[i] });
103
+ // }
104
+ for ( let i = 0 ; i < matchKeys . length ; i ++ ) {
105
+ addMatches ( { match : matchKeys [ i ] } ) ;
106
+ }
107
+ let newAssignments : string [ ] = [ ] ;
108
+ // Iterate over each match object
109
+ let count = 0 ;
110
+ let c2 = 1 ;
111
+ // biome-ignore lint/complexity/noForEach: <explanation>
112
+ matches . forEach ( ( match : any ) => {
113
+ // Extract match number and alliances
114
+ const { match_num, match_key, alliances } = match ;
115
+
116
+ // Iterate over each alliance (blue and red)
117
+ // biome-ignore lint/complexity/noForEach: <explanation>
118
+ Object . values ( alliances ) . forEach ( ( alliance : any ) => {
119
+ // Extract team keys from the alliance
120
+ const teamKeys = alliance . team_keys ;
121
+
122
+ // Iterate over each team key
123
+ // biome-ignore lint/complexity/noForEach: <explanation>
124
+ teamKeys . forEach ( ( teamKey : string ) => {
125
+ // Construct the assignment string with match number and team number
126
+ const assignment = `Match ${ c2 } - Team ${ teamKey } ` ;
127
+ // const assignment = `Match ${match_key} - Team ${teamKey}`;
128
+ count ++ ;
129
+ if ( count % 6 == 0 ) {
130
+ count = 0 ;
131
+ c2 ++ ;
132
+ }
133
+ // Check if the assignment already exists in the assignments array
134
+ if ( ! newAssignments . includes ( assignment ) ) {
135
+ // If not, add it to the newAssignments array
136
+ newAssignments . push ( assignment ) ;
137
+ }
138
+ } ) ;
139
+ } ) ;
140
+ } ) ;
141
+
142
+ // Update the state with the newAssignments array
143
+ setAssignments ( newAssignments ) ;
144
+ }
145
+ } , [ isLoading , data ] ) ;
49
146
return (
50
- < DndContext
51
- onDragStart = { function handleDragStart ( event ) {
52
- const active = event . active as { id : string } ;
53
- console . log ( "start" , active ) ;
54
- setActiveId ( active . id ) ;
55
- } }
56
- onDragEnd = { function handleDragEnd ( event ) {
57
- const overId = event . over ?. id ;
58
- console . log ( "end" , overId , activeId , members , assignments ) ;
59
- if ( overId === undefined ) {
60
- // Drag action was cancelled
61
- return ;
62
- }
63
- if ( overId === "ASSIGNMENT_LIST" ) {
64
- if ( assignments . includes ( activeId as string ) ) {
65
- // Assignment already exists in the list
147
+ < >
148
+ { /* <ModalSelectComponent /> */ }
149
+
150
+ < DndContext
151
+ onDragStart = { function handleDragStart ( event ) {
152
+ const active = event . active as { id : string } ;
153
+ console . log ( "start" , active ) ;
154
+ setActiveId ( active . id ) ;
155
+ } }
156
+ onDragEnd = { function handleDragEnd ( event ) {
157
+ const overId = event . over ?. id ;
158
+ console . log (
159
+ "end" ,
160
+ "OVERID: " ,
161
+ overId ,
162
+ "ACTIVE ID: " ,
163
+ activeId ,
164
+ members ,
165
+ assignments ,
166
+ ) ;
167
+
168
+ // Find the match object corresponding to the activeTeamKey
169
+ if ( activeId && data ) {
170
+ const activeTeamKey = activeId . split ( " " ) [ 4 ] ;
171
+ const match = data . find ( ( match ) => {
172
+ // Check if activeTeamKey is part of the blue alliance
173
+ if ( match . alliances . blue . team_keys . includes ( activeTeamKey ) ) {
174
+ return true ; // Return true if found in blue alliance
175
+ }
176
+ // Check if activeTeamKey is part of the red alliance
177
+ if ( match . alliances . red . team_keys . includes ( activeTeamKey ) ) {
178
+ return true ; // Return true if found in red alliance
179
+ }
180
+ return false ; // Return false if not found in either alliance
181
+ } ) ;
182
+
183
+ // Check if the activeTeamKey is part of the blue alliance or the red alliance
184
+ const allianceColor = match . alliances . blue . team_keys . includes (
185
+ activeTeamKey ,
186
+ )
187
+ ? "blue"
188
+ : "red" ;
189
+ console . log ( "Alliance color:" , allianceColor ) ;
190
+
191
+ // Find the index of the match object within the data array
192
+ // const index = data.indexOf(match);
193
+ // Find the index of the team key within the blue or red alliance array
194
+ let index ;
195
+ if ( allianceColor === "blue" ) {
196
+ index = match . alliances . blue . team_keys . indexOf ( activeTeamKey ) ;
197
+ } else {
198
+ index = match . alliances . red . team_keys . indexOf ( activeTeamKey ) ;
199
+ }
200
+
201
+ // If the team key is found, return the corresponding index + 1
202
+ if ( index !== - 1 ) {
203
+ const correspondingIndex = index + 1 ;
204
+ console . log ( "Corresponding index:" , correspondingIndex ) ;
205
+ } else {
206
+ console . log ( "Team key not found in data." ) ;
207
+ }
208
+
209
+ const alliance = ( allianceColor + ( index + 1 ) ) as string ;
210
+ const matchNumber = parseInt ( activeId . split ( " " ) [ 1 ] ) ;
211
+
212
+ // Make sure index is within the valid range of data array
213
+
214
+ const currentMatchKey = data [ matchNumber - 1 ] . match_key ;
215
+ const currentTeam = parseInt ( activeId . split ( " " ) [ 4 ] . slice ( - 4 ) ) ;
216
+
217
+ addAssignment ( {
218
+ match : currentMatchKey ,
219
+ team : currentTeam ,
220
+ alliance : alliance ,
221
+ assignee : overId ,
222
+ } ) ;
223
+ }
224
+
225
+ if ( overId === undefined ) {
226
+ // Drag action was cancelled
66
227
return ;
67
228
}
68
- setAssignments ( ( assignments ) => [ ...assignments , activeId as string ] ) ;
69
- setMembers ( ( members ) => {
70
- const prevMember = reverseAssignmentToMembers [ activeId as string ] ;
71
- return {
72
- ...members ,
73
- [ prevMember ] : members [ prevMember ] . filter ( ( x ) => x !== activeId ) ,
74
- } ;
75
- } ) ;
229
+ if ( overId === "ASSIGNMENT_LIST" ) {
230
+ if ( assignments . includes ( activeId as string ) ) {
231
+ // Assignment already exists in the list
232
+ return ;
233
+ }
234
+ setAssignments ( ( assignments ) => [
235
+ ...assignments ,
236
+ activeId as string ,
237
+ ] ) ;
238
+ setMembers ( ( members ) => {
239
+ const prevMember = reverseAssignmentToMembers [ activeId as string ] ;
240
+ return {
241
+ ...members ,
242
+ [ prevMember ] : members [ prevMember ] . filter ( ( x ) => x !== activeId ) ,
243
+ } ;
244
+ } ) ;
245
+ setActiveId ( null ) ;
246
+ return ;
247
+ }
248
+ if ( members [ overId ] . includes ( activeId as string ) ) {
249
+ // Assignment already exists in the member's list
250
+ return ;
251
+ }
252
+ const prevMember = reverseAssignmentToMembers [ activeId as string ] ;
253
+ if ( prevMember !== undefined ) {
254
+ setMembers ( ( members ) => {
255
+ return {
256
+ ...members ,
257
+ [ overId ] : [ ...members [ overId ] , activeId as string ] ,
258
+ [ prevMember ] : members [ prevMember ] . filter ( ( x ) => x !== activeId ) ,
259
+ } ;
260
+ } ) ;
261
+ } else {
262
+ setMembers ( ( members ) => {
263
+ return {
264
+ ...members ,
265
+ [ overId ] : [ ...members [ overId ] , activeId as string ] ,
266
+ } ;
267
+ } ) ;
268
+ }
269
+ setAssignments ( ( assignments ) =>
270
+ assignments . filter ( ( x ) => x !== activeId ) ,
271
+ ) ;
76
272
setActiveId ( null ) ;
77
- return ;
78
- }
79
- if ( members [ overId ] . includes ( activeId as string ) ) {
80
- // Assignment already exists in the member's list
81
- return ;
82
- }
83
- const prevMember = reverseAssignmentToMembers [ activeId as string ] ;
84
- if ( prevMember !== undefined ) {
85
- setMembers ( ( members ) => {
86
- return {
87
- ...members ,
88
- [ overId ] : [ ...members [ overId ] , activeId as string ] ,
89
- [ prevMember ] : members [ prevMember ] . filter ( ( x ) => x !== activeId ) ,
90
- } ;
91
- } ) ;
92
- } else {
93
- setMembers ( ( members ) => {
94
- return {
95
- ...members ,
96
- [ overId ] : [ ...members [ overId ] , activeId as string ] ,
97
- } ;
98
- } ) ;
99
- }
100
- setAssignments ( ( assignments ) =>
101
- assignments . filter ( ( x ) => x !== activeId ) ,
102
- ) ;
103
- setActiveId ( null ) ;
104
- } }
105
- >
106
- < ResizablePanelGroup
107
- direction = "horizontal"
108
- className = "max-w-screen h-rounded-lg h-full border"
273
+ } }
109
274
>
110
- < ResizablePanel defaultSize = { 50 } >
111
- < ScrollArea className = "h-full w-full rounded-md border" >
112
- < div className = "flex flex-wrap" >
113
- { Object . entries ( members ) . map ( ( [ name , values ] ) => (
114
- < MemberCard key = { name } user = { name } assignments = { values } />
115
- ) ) }
116
- </ div >
117
- </ ScrollArea >
118
- </ ResizablePanel >
119
- < ResizableHandle withHandle = { true } />
120
- < ResizablePanel defaultSize = { 50 } >
121
- < AssignmentList assignments = { assignments } />
122
- < DragOverlay >
123
- { activeId && < AssignmentCard assignment = { activeId } /> }
124
- </ DragOverlay >
125
- { /* <ResizablePanelGroup direction="vertical">
275
+ < ResizablePanelGroup
276
+ direction = "horizontal"
277
+ className = "max-w-screen h-rounded-lg h-full border"
278
+ >
279
+ < ResizablePanel defaultSize = { 50 } >
280
+ < ScrollArea className = "h-full w-full rounded-md border" >
281
+ < div className = "flex flex-wrap" >
282
+ { Object . entries ( members ) . map ( ( [ name , values ] ) => (
283
+ < MemberCard key = { name } user = { name } assignments = { values } />
284
+ ) ) }
285
+ </ div >
286
+ </ ScrollArea >
287
+ </ ResizablePanel >
288
+ < ResizableHandle withHandle = { true } />
289
+ < ResizablePanel defaultSize = { 50 } >
290
+ < AssignmentList assignments = { assignments } />
291
+ < DragOverlay >
292
+ { activeId && < AssignmentCard assignment = { activeId } /> }
293
+ </ DragOverlay >
294
+ { /* <ResizablePanelGroup direction="vertical">
126
295
<ResizablePanel defaultSize={25}>
127
296
<div className="flex h-full items-center justify-center p-6">
128
297
<span className="font-semibold">Two</span>
@@ -135,9 +304,17 @@ export default function Assignments() {
135
304
</div>
136
305
</ResizablePanel>
137
306
</ResizablePanelGroup> */ }
138
- </ ResizablePanel >
139
- </ ResizablePanelGroup >
140
- </ DndContext >
307
+ </ ResizablePanel >
308
+ </ ResizablePanelGroup >
309
+ < Button
310
+ onClick = { ( ) =>
311
+ autoAssign ( assignments , members , setMembers , setAssignments )
312
+ }
313
+ >
314
+ Auto Assign
315
+ </ Button >
316
+ </ DndContext >
317
+ </ >
141
318
) ;
142
319
}
143
320
function AssignmentList ( { assignments } : { assignments : string [ ] } ) {
@@ -157,3 +334,4 @@ function AssignmentList({ assignments }: { assignments: string[] }) {
157
334
</ ScrollArea >
158
335
) ;
159
336
}
337
+ export default trpc . withTRPC ( Assignments ) ;
0 commit comments