import TabPanel from "@mui/lab/TabPanel"; import { useState } from "react"; import { useTheme } from "@emotion/react"; import { Box, Stack, Typography } from "@mui/material"; import LoadingButton from "@mui/lab/LoadingButton"; import Field from "../../Inputs/Field"; import { credentials } from "../../../Validation/validation"; import Alert from "../../Alert"; import { update } from "../../../Features/Auth/authSlice"; import { useDispatch, useSelector } from "react-redux"; import { createToast } from "../../../Utils/toastUtils"; /** * PasswordPanel component manages the form for editing password. * * @returns {JSX.Element} */ const PasswordPanel = () => { const theme = useTheme(); const dispatch = useDispatch(); //redux state const { authToken, isLoading } = useSelector((state) => state.auth); const idToName = { "edit-current-password": "password", "edit-new-password": "newPassword", "edit-confirm-password": "confirm", }; const [localData, setLocalData] = useState({ password: "", newPassword: "", confirm: "", }); const [errors, setErrors] = useState({}); const handleChange = (event) => { const { value, id } = event.target; const name = idToName[id]; setLocalData((prev) => ({ ...prev, [name]: value, })); const validation = credentials.validate( { [name]: value }, { abortEarly: false, context: { password: localData.newPassword } } ); setErrors((prev) => { const updatedErrors = { ...prev }; if (validation.error) { updatedErrors[name] = validation.error.details[0].message; } else { delete updatedErrors[name]; } return updatedErrors; }); }; const handleSubmit = async (event) => { event.preventDefault(); const { error } = credentials.validate(localData, { abortEarly: false, context: { password: localData.newPassword }, }); if (error) { const newErrors = {}; error.details.forEach((err) => { newErrors[err.path[0]] = err.message; }); setErrors(newErrors); } else { const action = await dispatch(update({ authToken, localData })); if (action.payload.success) { createToast({ body: "Your password was changed successfully.", }); setLocalData({ password: "", newPassword: "", confirm: "", }); } else { // TODO: Check for other errors? createToast({ body: "Your password input was incorrect.", }); setErrors({ password: "*" + action.payload.msg + "." }); } } }; return ( Current password New password Confirm new password Save ); }; PasswordPanel.propTypes = { // No props are being passed to this component, hence no specific PropTypes are defined. }; export default PasswordPanel;