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 151 152 153 154 155 156 157 158 159 160 161 162 163 | 39x 39x 39x 39x 4x 12x 4x 4x 39x 1x 39x 39x 4x 4x 3x 3x 3x 1x 1x 1x 39x 5x 39x | //validations
import { useFormik } from "formik";
import * as React from "react";
import Button from "@mui/material/Button";
import Dialog from "@mui/material/Dialog";
import DialogActions from "@mui/material/DialogActions";
import DialogContent from "@mui/material/DialogContent";
import DialogTitle from "@mui/material/DialogTitle";
import { Alert, Box, TextField, Typography } from "@mui/material";
import { useDispatch } from "react-redux";
import { _clearErrorMessage, _updateFullname } from "@/store/slices/teacher/TeacherData";
import { useSelector } from "react-redux";
import { enqueueSnackbar } from "notistack";
import { useEffect } from "react";
import {
teacherNameValidationSchema,
buildTeacherFullnamePayload,
buildTeacherNameInitialValues,
} from "./teacherProfileUpdate";
export default function ChangeName({ showDialog, setShowDialog }) {
const { teacherData, hasErrors, isLoading } = useSelector((state) => state.TeacherData);
const dispatch = useDispatch();
const {
values,
touched,
errors,
handleChange,
handleBlur,
setFieldTouched,
validateForm,
setValues,
} = useFormik({
initialValues: {
firstName: "",
middleName: "",
lastName: "",
},
onSubmit: () => {},
validationSchema: teacherNameValidationSchema,
});
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 = () => {
setValues({
firstName: "",
middleName: "",
lastName: "",
});
};
const clickCancel = () => {
resetFields();
dispatch(_clearErrorMessage());
setShowDialog(false);
};
const clickSave = async () => {
const hasErrors = await checkFormErrors();
if (!hasErrors) {
const payload = buildTeacherFullnamePayload(values);
dispatch(_updateFullname({ teacherUUID: teacherData.id, payload })).then((response) => {
// Modified by Allan Ninal — `response.payload.status` throws when the
// rejection carries no body, and reads undefined when it does, so a
// failed save closed nothing and said nothing. The dialog now stays
// open on failure, which is what a form should do: the store holds a
// readable reason (see the slice) and the Alert above renders it.
if (!_updateFullname.fulfilled.match(response)) return;
resetFields();
setShowDialog(false);
enqueueSnackbar(response.payload.data.message, { variant: "success" });
});
}
};
useEffect(() => {
setValues(buildTeacherNameInitialValues(teacherData));
}, [showDialog]);
return (
<React.Fragment>
<Dialog
open={showDialog}
aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description"
fullWidth
>
<DialogTitle
id="alert-dialog-title"
sx={{
display: "flex",
justifyContent: "space-between",
alignItems: "center",
}}
>
<Typography variant="body1">Edit Name</Typography>
</DialogTitle>
<DialogContent dividers>
<Box sx={{ display: "flex", flexDirection: "column", gap: "1rem" }}>
{hasErrors && <Alert severity="error">{hasErrors}</Alert>}
<TextField
name="firstName"
value={values.firstName}
onChange={handleChange}
onBlur={handleBlur}
error={touched.firstName && Boolean(errors.firstName)}
helperText={touched.firstName && errors.firstName}
label="First name"
variant="outlined"
fullWidth
required
/>
<TextField
name="middleName"
value={values.middleName}
onChange={handleChange}
onBlur={handleBlur}
error={touched.middleName && Boolean(errors.middleName)}
helperText={touched.middleName && errors.middleName}
label="Middle name"
variant="outlined"
fullWidth
/>
<TextField
name="lastName"
value={values.lastName}
onChange={handleChange}
onBlur={handleBlur}
error={touched.lastName && Boolean(errors.lastName)}
helperText={touched.lastName && errors.lastName}
label="Last name"
variant="outlined"
fullWidth
required
/>
</Box>
</DialogContent>
<DialogActions sx={{ display: "flex", justifyContent: "space-between" }}>
<Button sx={{ width: "150px" }} variant="inherit" onClick={clickCancel}>
Cancel
</Button>
<Button sx={{ width: "150px" }} variant="contained" color="primary" onClick={clickSave}>
Save
</Button>
</DialogActions>
</Dialog>
</React.Fragment>
);
}
|