import { useState } from "react"; import { Button, Stack } from "@mui/material"; import { GenericDialog } from "../../../../Components/Dialog/genericDialog"; import TextInput from "../../../../Components/Inputs/TextInput"; import PasswordTooltip from "../../../Auth/components/PasswordTooltip"; import { useTheme } from "@emotion/react"; import { useTranslation } from "react-i18next"; import { createToast } from "../../../../Utils/toastUtils"; import { PasswordEndAdornment } from "../../../../Components/Inputs/TextInput/Adornments"; import usePasswordFeedback from "../../../Auth/hooks/usePasswordFeedback"; const ChangePasswordModal = ({ isSaving, isLoading, userId, changePassword, email }) => { const INITIAL_FORM_STATE = { password: "", confirm: "", }; console.log("email", email); const theme = useTheme(); const { t } = useTranslation(); const { feedback, handlePasswordFeedback } = usePasswordFeedback(); const [form, setForm] = useState(INITIAL_FORM_STATE); const [isChangePasswordOpen, setIsChangePasswordOpen] = useState(false); const [errors, setErrors] = useState({}); const [isLoadingSubmit, setIsLoadingSubmit] = useState(false); const closeChangePasswordModal = () => { setIsChangePasswordOpen(false); setForm(INITIAL_FORM_STATE); }; const onChange = (e) => { let { name, value } = e.target; const updatedForm = { ...form, [name]: value }; //validateFields(name, value, updatedForm); setForm(updatedForm); handlePasswordFeedback(updatedForm, name, value, form, errors, setErrors); }; const isFormValid = !errors.password && !errors.confirm; const onsubmitChangePassword = async (event) => { event.preventDefault(); if (!isFormValid) return; const newPasswordForm = { password: form.password, newPassword: form.confirm, email: email, }; try { setIsLoadingSubmit(true); await changePassword(newPasswordForm); closeChangePasswordModal(); } catch (error) { const errorMsg = error.response?.data?.msg || error.message || "unknownError"; createToast({ type: "error", body: t(errorMsg), }); } finally { setIsLoadingSubmit(false); } }; return ( <> } sx={{ mb: theme.spacing(5) }} /> } sx={{ mb: theme.spacing(5) }} /> ); }; export default ChangePasswordModal;