Skip to content

Commit 76676f1

Browse files
committed
Further changes made.
1 parent e78bf2b commit 76676f1

File tree

3 files changed

+61
-23
lines changed

3 files changed

+61
-23
lines changed

src/components/design_system/dataTable/DataTableHeader.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useEffect, useRef, useState, ChangeEvent, ReactNode } from "react";
2-
import { Grid2 as Grid, Stack, Typography } from "@mui/material";
2+
import { Grid as Grid, Stack, Typography } from "@mui/material";
33

44
import Search from "@/components/design_system/search/Search";
55

src/components/student/EditStudentModal.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ interface EditStudentModalProps {
3737
error?: boolean;
3838

3939
//helperText
40-
helperText?: string;
40+
helperText?: {
41+
message: string;
42+
field?: string | null;
43+
};
4144
}
4245

4346
export const EditStudentModal = ({
@@ -80,7 +83,9 @@ export const EditStudentModal = ({
8083
defaultValue={student?.first_name || ""}
8184
required
8285
error={error}
83-
helperText={helperText}
86+
helperText={
87+
helperText?.field === "name" ? helperText.message : ""
88+
}
8489
/>
8590
</Container>
8691
<Container className={$CompassModal.editModalContainer}>
@@ -92,7 +97,9 @@ export const EditStudentModal = ({
9297
defaultValue={student?.last_name || ""}
9398
required
9499
error={error}
95-
helperText={helperText}
100+
helperText={
101+
helperText?.field === "name" ? helperText.message : ""
102+
}
96103
/>
97104
</Container>
98105
<Container className={$CompassModal.editModalContainer}>
@@ -111,8 +118,10 @@ export const EditStudentModal = ({
111118
type="number"
112119
name="grade"
113120
defaultValue={(student?.grade || 0).toString()}
114-
error={error}
115-
helperText={helperText}
121+
error={helperText?.field === "grade" ? true : false}
122+
helperText={
123+
helperText?.field === "grade" ? helperText.message : ""
124+
}
116125
required
117126
/>
118127
</Container>

src/pages/students/[student_id].tsx

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,38 @@ const ViewStudentPage: NextPageWithBreadcrumbs = () => {
3838
const [startDate, setStartDate] = useState("");
3939
const [endDate, setEndDate] = useState("");
4040

41-
const [formError, setFormError] = useState(false);
42-
const [helperText, setHelperText] = useState("");
41+
interface errorMessage {
42+
message: string;
43+
field: string | null;
44+
}
45+
interface FormError {
46+
error: boolean;
47+
errorMessage: errorMessage;
48+
}
4349

50+
const [formError, setFormError] = useState<FormError>({
51+
error: false,
52+
errorMessage: { message: "", field: null },
53+
});
4454
const utils = trpc.useContext();
55+
56+
// Handle error timeout
57+
useEffect(() => {
58+
let timeoutId: NodeJS.Timeout;
59+
if (formError.error) {
60+
timeoutId = setTimeout(() => {
61+
setFormError({
62+
error: false,
63+
errorMessage: { message: "", field: null },
64+
});
65+
}, 3000);
66+
}
67+
return () => {
68+
if (timeoutId) {
69+
clearTimeout(timeoutId);
70+
}
71+
};
72+
}, [formError]);
4573
const router = useRouter();
4674
const { student_id } = router.query;
4775

@@ -106,21 +134,22 @@ const ViewStudentPage: NextPageWithBreadcrumbs = () => {
106134
!alphabeticalRegex.test(data.get("firstName") as string) ||
107135
!alphabeticalRegex.test(data.get("lastName") as string)
108136
) {
109-
setFormError(true);
110-
setHelperText("Only letters, spaces, and hyphens allowed");
111-
setTimeout(() => {
112-
setFormError(false);
113-
setHelperText("");
114-
}, 3000);
115-
137+
setFormError({
138+
error: true,
139+
errorMessage: {
140+
message: "Only letters, spaces, and hyphens allowed",
141+
field: "name",
142+
},
143+
});
116144
return;
117145
} else if (Number(data.get("grade")) === 0) {
118-
setFormError(true);
119-
setHelperText("Grade must be between 1 and 12");
120-
setTimeout(() => {
121-
setHelperText("");
122-
setFormError(false);
123-
}, 3000);
146+
setFormError({
147+
error: true,
148+
errorMessage: {
149+
message: "Grade must be between 1 and 12",
150+
field: "grade",
151+
},
152+
});
124153
return;
125154
} else {
126155
editMutation.mutate({
@@ -284,8 +313,8 @@ const ViewStudentPage: NextPageWithBreadcrumbs = () => {
284313
endDate={endDate}
285314
setStartDate={setStartDate}
286315
onSubmit={handleEditStudent}
287-
error={formError}
288-
helperText={helperText}
316+
error={formError.error}
317+
helperText={formError.errorMessage}
289318
/>
290319

291320
{/* Archiving Student Modal appears when "Archive" button is pressed*/}

0 commit comments

Comments
 (0)