import { useEffect, useState } from "react"; import { toast } from "react-hot-toast"; import { useTranslate } from "@/utils/i18n"; import { useGlobalStore, useUserStore } from "@/store/module"; import Icon from "./Icon"; import { generateDialog } from "./Dialog"; type Props = DialogProps; const ChangePasswordDialog: React.FC = ({ destroy }: Props) => { const t = useTranslate(); const userStore = useUserStore(); const globalStore = useGlobalStore(); const profile = globalStore.state.systemStatus.profile; const [newPassword, setNewPassword] = useState(""); const [newPasswordAgain, setNewPasswordAgain] = useState(""); useEffect(() => { if (profile.mode === "demo" && userStore.state.user?.id === userStore.state.host?.id) { toast.error("Demo mode does not support this operation."); destroy(); } }, []); const handleCloseBtnClick = () => { destroy(); }; const handleNewPasswordChanged = (e: React.ChangeEvent) => { const text = e.target.value as string; setNewPassword(text); }; const handleNewPasswordAgainChanged = (e: React.ChangeEvent) => { const text = e.target.value as string; setNewPasswordAgain(text); }; 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 { const user = userStore.getState().user as User; await userStore.patchUser({ id: user.id, password: newPassword, }); 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")}

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