diff options
author | crupest <crupest@outlook.com> | 2023-09-20 20:26:42 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-20 20:26:42 +0800 |
commit | f836d77e73f3ea0af45c5f71dae7268143d6d86f (patch) | |
tree | 573cfafd972106d69bef0d41ff5f270ec3c43ec2 /FrontEnd/src/pages/register/index.tsx | |
parent | 4a069bf1268f393d5467166356f691eb89963152 (diff) | |
parent | 901fe3d7c032d284da5c9bce24c4aaee9054c7ac (diff) | |
download | timeline-f836d77e73f3ea0af45c5f71dae7268143d6d86f.tar.gz timeline-f836d77e73f3ea0af45c5f71dae7268143d6d86f.tar.bz2 timeline-f836d77e73f3ea0af45c5f71dae7268143d6d86f.zip |
Merge pull request #1395 from crupest/dev
Refector 2023 v0.1
Diffstat (limited to 'FrontEnd/src/pages/register/index.tsx')
-rw-r--r-- | FrontEnd/src/pages/register/index.tsx | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/FrontEnd/src/pages/register/index.tsx b/FrontEnd/src/pages/register/index.tsx new file mode 100644 index 00000000..fa25c2c2 --- /dev/null +++ b/FrontEnd/src/pages/register/index.tsx @@ -0,0 +1,130 @@ +import { useState, useEffect } from "react"; +import { useTranslation } from "react-i18next"; +import { useNavigate } from "react-router-dom"; + +import { HttpBadRequestError } from "~src/http/common"; +import { getHttpTokenClient } from "~src/http/token"; +import { userService, useUser } from "~src/services/user"; + +import { LoadingButton } from "~src/components/button"; +import { useInputs, InputGroup } from "~src/components/input/InputGroup"; + +import "./index.css"; + +export default function RegisterPage() { + const navigate = useNavigate(); + + const { t } = useTranslation(); + + const user = useUser(); + + const { hasErrorAndDirty, confirm, setAllDisabled, inputGroupProps } = + useInputs({ + init: { + scheme: { + inputs: [ + { + key: "username", + type: "text", + label: "register.username", + }, + { + key: "password", + type: "text", + label: "register.password", + password: true, + }, + { + key: "confirmPassword", + type: "text", + label: "register.confirmPassword", + password: true, + }, + { + key: "registerCode", + + type: "text", + label: "register.registerCode", + }, + ], + validator: ( + { username, password, confirmPassword, registerCode }, + errors, + ) => { + if (username === "") { + errors["username"] = "register.error.usernameEmpty"; + } + if (password === "") { + errors["password"] = "register.error.passwordEmpty"; + } + if (confirmPassword !== password) { + errors["confirmPassword"] = "register.error.confirmPasswordWrong"; + } + if (registerCode === "") { + errors["registerCode"] = "register.error.registerCodeEmpty"; + } + }, + }, + dataInit: {}, + }, + }); + + const [process, setProcess] = useState<boolean>(false); + const [resultError, setResultError] = useState<string | null>(null); + + useEffect(() => { + if (user != null) { + navigate("/"); + } + }, [navigate, user]); + + return ( + <div className="container register-page"> + <InputGroup {...inputGroupProps} /> + {resultError && <div className="cru-color-danger">{t(resultError)}</div>} + <LoadingButton + text="register.register" + loading={process} + disabled={hasErrorAndDirty} + onClick={() => { + const confirmResult = confirm(); + if (confirmResult.type === "ok") { + const { username, password, registerCode } = confirmResult.values; + setProcess(true); + setAllDisabled(true); + void getHttpTokenClient() + .register({ + username: username as string, + password: password as string, + registerCode: registerCode as string, + }) + .then( + () => { + void userService + .login( + { + username: username as string, + password: password as string, + }, + true, + ) + .then(() => { + navigate("/"); + }); + }, + (error) => { + if (error instanceof HttpBadRequestError) { + setResultError("register.error.registerCodeInvalid"); + } else { + setResultError("error.network"); + } + setProcess(false); + setAllDisabled(false); + }, + ); + } + }} + /> + </div> + ); +} |