import { useEffect, useState } from "react"; import { validate, ValidatorConfig } from "../helpers/validator"; import useI18n from "../hooks/useI18n"; import { userService } from "../services"; import Icon from "./Icon"; import { generateDialog } from "./Dialog"; import toastHelper from "./Toast"; import "../less/change-password-dialog.less"; const validateConfig: ValidatorConfig = { minLength: 4, maxLength: 24, noSpace: true, noChinese: true, }; interface Props extends DialogProps {} const ChangePasswordDialog: React.FC = ({ destroy }: Props) => { const { t } = useI18n(); 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("Please fill in all fields."); return; } if (newPassword !== newPasswordAgain) { toastHelper.error("New passwords do not match."); setNewPasswordAgain(""); return; } const passwordValidResult = validate(newPassword, validateConfig); if (!passwordValidResult.result) { toastHelper.error("Password " + passwordValidResult.reason); return; } try { const user = userService.getState().user as User; await userService.patchUser({ id: user.id, password: newPassword, }); toastHelper.info("Password changed."); handleCloseBtnClick(); } catch (error: any) { toastHelper.error(error); } }; return ( <>

Change Password

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