import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { validate, ValidatorConfig } from "../helpers/validator"; import { userService } from "../services"; import Icon from "./Icon"; import { generateDialog } from "./Dialog"; import toastHelper from "./Toast"; const validateConfig: ValidatorConfig = { minLength: 4, maxLength: 320, noSpace: true, noChinese: true, }; type Props = DialogProps; const ChangePasswordDialog: React.FC = ({ destroy }: Props) => { const { t } = useTranslation(); const [newPassword, setNewPassword] = useState(""); const [newPasswordAgain, setNewPasswordAgain] = useState(""); useEffect(() => { // do nth }, []); 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 === "") { toastHelper.error(t("message.fill-all")); return; } if (newPassword !== newPasswordAgain) { toastHelper.error(t("message.new-password-not-match")); setNewPasswordAgain(""); return; } const passwordValidResult = validate(newPassword, validateConfig); if (!passwordValidResult.result) { toastHelper.error(`${t("common.password")} ${t(passwordValidResult.reason as string)}`); return; } try { const user = userService.getState().user as User; await userService.patchUser({ id: user.id, password: newPassword, }); toastHelper.info(t("message.password-changed")); handleCloseBtnClick(); } catch (error: any) { console.error(error); toastHelper.error(error.response.data.message); } }; return ( <>

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

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

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

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