Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | 1x 108x 78x 78x 78x 78x 78x 78x 78x 11x 11x 11x 11x 78x 4x 4x 4x 78x 11x 11x 8x 8x 11x 1x 1x 1x 7x 4x 4x 78x 3x 3x 3x 78x 15x | import { Alert, AlertTitle, Button, Dialog, DialogActions, DialogContent, DialogTitle, TextField } from "@mui/material";
import AddIcon from "@mui/icons-material/Add";
import { useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { useLocation, useNavigate } from "react-router-dom";
import { _searchClassByCode, DEFAULT_CLASS_ERROR } from "@/store/slices/student/StudentSearchClass";
import { useFormik } from "formik";
import * as yup from "yup";
const ButtonNewClass = () => {
const { hasErrors, isLoading } = useSelector((state) => state.StudentSearchClass);
const dispatch = useDispatch();
const location = useLocation();
const navigate = useNavigate();
const { values, touched, errors, handleChange, handleBlur, setFieldTouched, validateForm, setFieldError } =
useFormik({
initialValues: {
classCode: "",
},
validationSchema: yup.object({
classCode: yup
.string()
.length(6, "Class code must be exactly 6 characters long")
.required("Class code is required"),
}),
});
const [showDialog, setShowDialog] = useState(false);
const [classNotFoundError, setClassNotFoundError] = useState(false);
const checkFormErrors = async () => {
// Mark all fields as touched
Object.keys(values).forEach((fieldName) => {
setFieldTouched(fieldName, true);
});
// Validate the form to update the errors object
const errors = await validateForm();
// Check if there are any errors
return Object.keys(errors).length > 0;
};
const resetFields = () => {
Object.keys(values).forEach((key) => {
values[key] = "";
});
// remove all field errors
Object.keys(errors).forEach((fieldName) => {
setFieldError(fieldName, "");
});
};
/**
* Modified by Allan Ninal — a search that neither found a class nor returned the
* exact string "Class not found." fell through both branches and did nothing
* visible at all: the student pressed Search and the dialog just sat there.
*
* The optional chaining matters too. On a rejected search `payload` is the error
* body, so reading `payload.data.Class` threw a TypeError inside this `.then()`,
* which is swallowed as an unhandled rejection.
*/
const handleSearch = async () => {
const hasErrors = await checkFormErrors();
if (hasErrors) return;
const response = await dispatch(_searchClassByCode(values.classCode));
const found = response?.payload?.data?.Class;
if (found) {
navigate(`${location.pathname}/search/${found.class_code}`);
resetFields();
return;
}
// A REJECTED search already put a readable message in the store, and the alert
// prefers it — only the fulfilled-but-not-found case needs one from here.
// (`hasErrors` read straight after the await is the previous render's value,
// so the matcher decides this rather than the selector.)
if (_searchClassByCode.fulfilled.match(response)) {
const message = response.payload?.data?.message;
setClassNotFoundError(
message === "Class not found."
? "Incorrect class code. Get correct class code from your teacher."
: message || DEFAULT_CLASS_ERROR
);
}
};
const handleCancel = () => {
setClassNotFoundError("");
resetFields();
setShowDialog(false);
};
return (
<>
<Button
startIcon={<AddIcon />}
variant="contained"
color="primary"
size="standard"
fullWidth
onClick={() => {
setShowDialog(true);
}}
>
Enroll New Class
</Button>
<Dialog
open={showDialog}
aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description"
fullWidth
maxWidth="sm"
>
<DialogTitle id="alert-dialog-title">Search Class</DialogTitle>
<DialogContent dividers>
{(hasErrors || classNotFoundError) && (
<Alert sx={{ borderRadius: 0, mb: 4 }} severity="error">
<AlertTitle>{hasErrors || classNotFoundError}</AlertTitle>
</Alert>
)}
<TextField
id="student-classes-search-textfield-enter-class-clode"
variant="outlined"
fullWidth
name="classCode"
label="Enter class code"
value={values.classCode}
onChange={handleChange}
onBlur={handleBlur}
error={touched.classCode && Boolean(errors.classCode)}
helperText={touched.classCode && errors.classCode}
/>
</DialogContent>
<DialogActions sx={{ display: "flex", justifyContent: "space-between" }}>
<Button sx={{ width: "150px" }} variant="inherit" onClick={handleCancel}>
Cancel
</Button>
<Button sx={{ width: "150px" }} variant="contained" color="primary" onClick={handleSearch}>
Search
</Button>
</DialogActions>
</Dialog>
</>
);
};
export default ButtonNewClass;
|