import { Button, IconButton, Input } from "@mui/joy"; import { useEffect, useState } from "react"; import { toast } from "react-hot-toast"; import useCurrentUser from "@/hooks/useCurrentUser"; import { useCommonContext } from "@/layouts/CommonContextProvider"; import { useUserStore } from "@/store/v1"; import { useTranslate } from "@/utils/i18n"; import { generateDialog } from "./Dialog"; import Icon from "./Icon"; type Props = DialogProps; const ChangePasswordDialog: React.FC = ({ destroy }: Props) => { const t = useTranslate(); const commonContext = useCommonContext(); const currentUser = useCurrentUser(); const userStore = useUserStore(); const [newPassword, setNewPassword] = useState(""); const [newPasswordAgain, setNewPasswordAgain] = useState(""); useEffect(() => { if (commonContext.profile.mode === "demo") { toast.error("Demo mode does not support this operation."); destroy(); } }, []); const handleCloseBtnClick = () => { destroy(); }; const handleNewPasswordChanged = (e: React.ChangeEvent) => { setNewPassword(e.target.value); }; const handleNewPasswordAgainChanged = (e: React.ChangeEvent) => { setNewPasswordAgain(e.target.value); }; const handleSaveBtnClick = async () => { if (newPassword === "" || newPasswordAgain === "") { toast.error(t("message.fill-all")); return; } if (newPassword !== newPasswordAgain) { toast.error(t("message.new-password-not-match")); setNewPasswordAgain(""); return; } try { await userStore.updateUser( { name: currentUser.name, password: newPassword, }, ["password"], ); toast.success(t("message.password-changed")); handleCloseBtnClick(); } catch (error: any) { console.error(error); toast.error(error.response.data.message); } }; return ( <>

{t("setting.account-section.change-password")}

{t("auth.new-password")}

{t("auth.repeat-new-password")}

); }; function showChangePasswordDialog() { generateDialog( { className: "change-password-dialog", dialogName: "change-password-dialog", }, ChangePasswordDialog, ); } export default showChangePasswordDialog;