import { useState } from "react"; import { useAppSelector } from "../../store"; import { userService } from "../../services"; import { validate, ValidatorConfig } from "../../helpers/validator"; import toastHelper from "../Toast"; import showChangePasswordDialog from "../ChangePasswordDialog"; import showConfirmResetOpenIdDialog from "../ConfirmResetOpenIdDialog"; import "../../less/settings/my-account-section.less"; const validateConfig: ValidatorConfig = { minLength: 4, maxLength: 24, noSpace: true, noChinese: true, }; interface Props {} const MyAccountSection: React.FC = () => { const user = useAppSelector((state) => state.user.user as User); const [username, setUsername] = useState(user.name); const openAPIRoute = `${window.location.origin}/api/memo?openId=${user.openId}`; const handleUsernameChanged = (e: React.ChangeEvent) => { const nextUsername = e.target.value as string; setUsername(nextUsername); }; const handleConfirmEditUsernameBtnClick = async () => { if (username === user.name) { return; } const usernameValidResult = validate(username, validateConfig); if (!usernameValidResult.result) { toastHelper.error("Username " + usernameValidResult.reason); return; } try { await userService.patchUser({ id: user.id, name: username, }); toastHelper.info("Username changed"); } catch (error: any) { toastHelper.error(error.message); } }; const handleChangePasswordBtnClick = () => { showChangePasswordDialog(); }; const handleResetOpenIdBtnClick = async () => { showConfirmResetOpenIdDialog(); }; const handlePreventDefault = (e: React.MouseEvent) => { e.preventDefault(); e.stopPropagation(); }; return ( <>

Account Information

Open API

{openAPIRoute}

Reset API

Usage guide:

{`POST ${openAPIRoute}\nContent-type: application/json\n{\n  "content": "Hello #memos from ${window.location.origin}"\n}`}
); }; export default MyAccountSection;