import CenterLayout from "@/layouts/CenterLayout"; import DashboardLayouts from "@/layouts/dashboardLayouts"; import {Box, Button, Container, Paper, Stack, Typography,} from "@mui/material"; import Image from "next/image"; import {Formik} from "formik"; import * as Yup from "yup"; import {useTranslations} from "next-intl"; import PasswordField from "@/core/components/PasswordField"; import StyledForm from "@/core/components/StyledForm"; import {CHANGE_PASSWORD} from "@/core/data/apiRoutes"; import useUser from "@/lib/app/hooks/useUser"; import axios from "axios"; import Notifications from "@/core/components/notifications"; import useDirection from "@/lib/app/hooks/useDirection"; import {toast} from "react-toastify"; const DashboardChangePasswordComponent = () => { const t = useTranslations(); const {token} = useUser(); const {directionApp} = useDirection(); const handleSubmit = (values, {setSubmitting, resetForm}) => { toast.dismiss(); const pendingToast = toast(t("notifications.pending"), { position: directionApp === "ltr" ? "top-left" : "top-right", autoClose: false, closeOnClick: false, draggable: false, }); axios .post( CHANGE_PASSWORD, { current_password: values.current_password, new_password: values.new_password, new_password_confirmation: values.new_password_confirmation, }, { headers: {authorization: `Bearer ${token}`}, } ) .then((response) => { toast.dismiss(pendingToast); // Dismiss the pending toast notification Notifications(directionApp, response, t); resetForm(); }) .catch((error) => { toast.dismiss(pendingToast); // Dismiss the pending toast notification Notifications(directionApp, error.response, t); }) .finally(() => { setSubmitting(false); // Set `setSubmitting` to false after the API request completes }); }; const initialValues = { current_password: "", new_password: "", new_password_confirmation: "", }; const validationSchema = Yup.object().shape({ current_password: Yup.string().required( t("ChangePassword.error_message_required") ), new_password: Yup.string() .min(8, t("ChangePassword.error_message_password_length")) .required(t("ChangePassword.error_message_required")), new_password_confirmation: Yup.string() .required(t("ChangePassword.error_message_required")) .test( t("ChangePassword.error_message_password_match"), t("ChangePassword.error_message_password_not_match"), function (value) { return this.parent.new_password === value; } ), }); return ( {(props) => ( { e.preventDefault(); props.handleSubmit(); }} > {t("ChangePassword.typography_change_password")} )} ); }; export default DashboardChangePasswordComponent;