Skip to content

Commit 25fa92c

Browse files
committed
Scores fixed
1 parent d29db14 commit 25fa92c

2 files changed

Lines changed: 67 additions & 49 deletions

File tree

src/api/fetchScores.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@ const scoresResponseSchema = z.object({
1717
message: z.string(),
1818
data: z.object({
1919
message: z.string(),
20-
scores: z.array(scoreSchema),
20+
scores: z.array(scoreSchema).optional(),
2121
}),
2222
});
23+
const createUpdateResponseSchema = z.object({
24+
status: z.string(),
25+
message: z.string(),
26+
});
2327

24-
const deleteSchema = z.object({
28+
const deleteResponseSchema = z.object({
2529
status: z.string(),
26-
data: z.record(z.unknown()),
30+
message: z.string()
2731
});
2832

2933

@@ -37,24 +41,25 @@ export const fetchScores = async (teamId: string) => {
3741
const response = await axios.get<{
3842
status: string;
3943
message: string;
40-
data: {
41-
message: string;
42-
scores: ScoreResponse[];
44+
data?: {
45+
message?: string;
46+
scores?: ScoreResponse[];
4347
};
4448
}>(`panel/getscore/${teamId}`, {
4549
withCredentials: true,
4650
});
4751
const parsedResponse = scoresResponseSchema.parse(response.data);
48-
return parsedResponse.data.scores;
49-
} catch (err) {
50-
console.error(err)
52+
return parsedResponse.data?.scores ?? [];
53+
} catch (err:any) {
54+
if (err.response.status === 404) {
55+
return [];
56+
}
5157
throw err;
5258
}
5359
};
5460
const createResponseSchema = z.object({
55-
61+
status: z.string(),
5662
message: z.string(),
57-
5863
});
5964

6065
export const createScore = async ({
@@ -98,20 +103,14 @@ export const deleteScore = async (scoreId: string) => {
98103
withCredentials: true,
99104
});
100105

101-
const parsedResponse = deleteSchema.parse(response.data);
106+
const parsedResponse = deleteResponseSchema.parse(response.data);
102107
return parsedResponse;
103108
} catch (err) {
104109
console.error(err);
105110
throw err;
106111
}
107112
};
108113

109-
const updateResponseSchema = z.object({
110-
111-
message: z.string(),
112-
113-
});
114-
115114
export const updateScore = async ({
116115
scoreId,
117116
design,
@@ -120,7 +119,8 @@ export const updateScore = async ({
120119
innovation,
121120
teamwork,
122121
comment,
123-
round
122+
round,
123+
team_id
124124
}: {
125125
scoreId:string,
126126
design:number,
@@ -130,6 +130,7 @@ export const updateScore = async ({
130130
teamwork:number,
131131
comment:string,
132132
round:number,
133+
team_id:string
133134
}) => {
134135
try {
135136
const response = await axios.put(`panel/updatescore/${scoreId}`,
@@ -140,14 +141,15 @@ export const updateScore = async ({
140141
innovation,
141142
teamwork,
142143
comment,
143-
round
144+
round,
145+
team_id
144146
},
145147
{
146148
withCredentials: true,
147149
}
148150
);
149151

150-
const parsedResponse = updateResponseSchema.parse(response.data);
152+
const parsedResponse = createUpdateResponseSchema.parse(response.data);
151153
return parsedResponse;
152154
} catch (err) {
153155
console.error(err);

src/app/team/[id]/page.tsx

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ function ScoreSection({ teamId }: { teamId: string }) {
108108
innovation,
109109
teamwork,
110110
comment,
111-
round
111+
round,
112+
team_id: teamId || ''
112113
}),
113114
onError: (err: ApiError) => {
114115
create(`Error updating score: ${err?.message ?? "unknown error"}`, "error");
@@ -144,31 +145,35 @@ function ScoreSection({ teamId }: { teamId: string }) {
144145
setEditMode(true);
145146
};
146147

147-
const handleSubmit = () => {
148+
const handleSubmit = async() => {
148149
if (!teamId) return;
149150

150-
if (editMode && currentScoreId) {
151-
void updateScoreMutation.mutateAsync({
152-
scoreId: currentScoreId,
153-
design,
154-
implementation,
155-
presentation,
156-
innovation,
157-
teamwork,
158-
comment,
159-
round
160-
});
161-
} else {
162-
void createScoreMutation.mutateAsync({
163-
teamId,
164-
design,
165-
implementation,
166-
presentation,
167-
innovation,
168-
teamwork,
169-
comment,
170-
round
171-
});
151+
try {
152+
if (editMode && currentScoreId) {
153+
await updateScoreMutation.mutateAsync({
154+
scoreId: currentScoreId,
155+
design,
156+
implementation,
157+
presentation,
158+
innovation,
159+
teamwork,
160+
comment,
161+
round
162+
});
163+
} else {
164+
await createScoreMutation.mutateAsync({
165+
teamId,
166+
design,
167+
implementation,
168+
presentation,
169+
innovation,
170+
teamwork,
171+
comment,
172+
round
173+
});
174+
}
175+
} catch (error) {
176+
console.error('Error submitting score:', error);
172177
}
173178
};
174179

@@ -186,11 +191,13 @@ function ScoreSection({ teamId }: { teamId: string }) {
186191
if (scoresLoading) return <div>Loading scores...</div>;
187192
if (scoresError) return <div>Error loading scores</div>;
188193

194+
const showForm = !scores || scores.length === 0 || editMode;
195+
189196
return (
190197
<div className="mt-8">
191198
<h2 className="text-xl font-bold mb-4">Team Scores</h2>
192199

193-
{((!scores || scores.length === 0) || editMode) && (
200+
{showForm && (
194201
<div className="space-y-4 bg-gray-900 p-4 rounded-lg mb-4">
195202
<h3 className="text-lg font-semibold">{editMode ? "Edit Score" : "Add Score"}</h3>
196203
<div className="grid grid-cols-2 gap-4">
@@ -228,19 +235,28 @@ function ScoreSection({ teamId }: { teamId: string }) {
228235
/>
229236
</div>
230237
<div className="space-x-2">
231-
<Button onClick={handleSubmit}>
238+
<Button
239+
onClick={handleSubmit}
240+
disabled={updateScoreMutation.isPending || createScoreMutation.isPending}
241+
>
232242
{editMode ? "Update Score" : "Add Score"}
233243
</Button>
234244
{editMode && (
235-
<Button variant="secondary" onClick={resetForm}>
245+
<Button
246+
variant="secondary"
247+
onClick={() => {
248+
resetForm();
249+
setEditMode(false);
250+
}}
251+
>
236252
Cancel
237253
</Button>
238254
)}
239255
</div>
240256
</div>
241257
)}
242258

243-
{scores.map((score: Score) => (
259+
{scores && scores.length > 0 && !editMode && scores.map((score: Score) => (
244260
<div key={score.id} className="bg-gray-800 rounded-lg p-4 space-y-4 mt-4">
245261
<div className="flex justify-between items-center">
246262
<div className="text-xl font-bold">

0 commit comments

Comments
 (0)