Skip to content

Commit ac750e3

Browse files
committed
Add Assignment to Database
1 parent 0362660 commit ac750e3

File tree

3 files changed

+87
-13
lines changed

3 files changed

+87
-13
lines changed

apps/nextjs/src/app/dashboard/Assignments.tsx

+72-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { assignTasks } from "@/lib/utils/autoassign";
2323
import { trpc } from "@/lib/utils/trpc";
2424
import { DndContext, DragOverlay, useDroppable } from "@dnd-kit/core";
2525

26-
import { addMatches, getEmails } from "./actions";
26+
import { addAssignment, addMatches, getEmails } from "./actions";
2727
import { AssignmentCard, MemberCard } from "./components/cards";
2828
import { CalendarDateRangePicker } from "./components/date-range-picker";
2929
import { MainNav } from "./components/main-nav";
@@ -54,9 +54,7 @@ function Assignments({ selectedEvent }) {
5454
// const [members, setMembers] = useState<{ [key: string]: string[] }>(
5555
// Object.fromEntries(tags.map((x) => [`${x}M`, []])),
5656
// );
57-
const [members, setMembers] = useState<{ [key: string]: string[] }>({
58-
"": [],
59-
});
57+
const [members, setMembers] = useState<{ [key: string]: string[] }>({});
6058
// Object.fromEntries(tags.map((x) => [`${x}M`, []])),
6159

6260
//use useEffect to make a supabase request from the auth tabke to fill it with emails
@@ -92,6 +90,7 @@ function Assignments({ selectedEvent }) {
9290
// Extract relevant information from the data and generate assignments
9391
const matches = data.map((match: any) => ({
9492
match_num: match.match_num,
93+
// match_key: match.match_key,
9594
alliances: match.alliances,
9695
}));
9796
const matchKeys = data.map((match: any) => ({
@@ -112,7 +111,7 @@ function Assignments({ selectedEvent }) {
112111
// biome-ignore lint/complexity/noForEach: <explanation>
113112
matches.forEach((match: any) => {
114113
// Extract match number and alliances
115-
const { match_num, alliances } = match;
114+
const { match_num, match_key, alliances } = match;
116115

117116
// Iterate over each alliance (blue and red)
118117
// biome-ignore lint/complexity/noForEach: <explanation>
@@ -125,6 +124,7 @@ function Assignments({ selectedEvent }) {
125124
teamKeys.forEach((teamKey: string) => {
126125
// Construct the assignment string with match number and team number
127126
const assignment = `Match ${c2} - Team ${teamKey}`;
127+
// const assignment = `Match ${match_key} - Team ${teamKey}`;
128128
count++;
129129
if (count % 6 == 0) {
130130
count = 0;
@@ -155,7 +155,73 @@ function Assignments({ selectedEvent }) {
155155
}}
156156
onDragEnd={function handleDragEnd(event) {
157157
const overId = event.over?.id;
158-
console.log("end", overId, activeId, members, assignments);
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+
159225
if (overId === undefined) {
160226
// Drag action was cancelled
161227
return;

apps/nextjs/src/app/dashboard/actions.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
import { createClient } from "@/lib/utils/supabase/client";
22

3-
export async function addToDatabase() {
3+
export async function addAssignment({ match, team, alliance, assignee }) {
44
const supabase = createClient();
55

6+
const { data: profileData, error: profileError } = await supabase
7+
.from("profiles")
8+
.select("id")
9+
.eq("email", assignee)
10+
.single();
11+
612
const { data, error } = await supabase
713
.from("assignments")
814
.insert([
915
{
10-
match: "2023cacg_qm13",
11-
team: "325666",
12-
alliance: "blue1",
13-
assignee: null,
16+
match: match,
17+
team: team,
18+
alliance: alliance,
19+
assignee: profileData?.id,
1420
event_log: {
1521
auto: { log: [], checkboxes: null },
1622
teleop: { log: [], checkboxes: null },

apps/nextjs/src/app/dashboard/components/cards.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ export function MemberCard({
3535
const { isOver, setNodeRef } = useDroppable({
3636
id: user,
3737
});
38+
const str =
39+
typeof user === "string" && user.length >= 2 ? user[0] + user[1] : "";
3840
return (
3941
// <DragOverlay>
4042
<Card
@@ -45,10 +47,10 @@ export function MemberCard({
4547
<CardTitle>
4648
<Avatar className="h-8 w-8">
4749
<AvatarImage src="/avatars/01.png" alt="@shadcn" />
48-
<AvatarFallback>BH</AvatarFallback>
50+
<AvatarFallback>{str.toUpperCase()}</AvatarFallback>
4951
</Avatar>
5052
</CardTitle>
51-
<CardDescription>Bryan Hu ({user})</CardDescription>
53+
<CardDescription>{user}</CardDescription>
5254
</CardHeader>
5355
<CardContent className="h-full">
5456
{assignments.length === 0 ? (

0 commit comments

Comments
 (0)