From aa89b6cce7701a57b0c377d938788b4c940013d6 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 1 Sep 2020 02:32:06 +0800 Subject: ... --- Timeline/ClientApp/src/app/views/login/index.tsx | 148 +++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 Timeline/ClientApp/src/app/views/login/index.tsx (limited to 'Timeline/ClientApp/src/app/views/login') diff --git a/Timeline/ClientApp/src/app/views/login/index.tsx b/Timeline/ClientApp/src/app/views/login/index.tsx new file mode 100644 index 00000000..e53d0002 --- /dev/null +++ b/Timeline/ClientApp/src/app/views/login/index.tsx @@ -0,0 +1,148 @@ +import React, { Fragment, useState, useEffect } from "react"; +import { useHistory } from "react-router"; +import { useTranslation } from "react-i18next"; +import { + Label, + FormGroup, + Input, + Form, + FormFeedback, + Spinner, + Button, +} from "reactstrap"; + +import { useUser, userService } from "@/services/user"; + +import AppBar from "../common/AppBar"; + +const LoginPage: React.FC = (_) => { + const { t } = useTranslation(); + const history = useHistory(); + const [username, setUsername] = useState(""); + const [usernameDirty, setUsernameDirty] = useState(false); + const [password, setPassword] = useState(""); + const [passwordDirty, setPasswordDirty] = useState(false); + const [rememberMe, setRememberMe] = useState(true); + const [process, setProcess] = useState(false); + const [error, setError] = useState(null); + + const user = useUser(); + + useEffect(() => { + if (user != null) { + const id = setTimeout(() => history.push("/"), 3000); + return () => { + clearTimeout(id); + }; + } + }, [history, user]); + + if (user != null) { + return ( + <> + +

{t("login.alreadyLogin")}

+ + ); + } + + function onSubmit(event: React.SyntheticEvent): void { + if (username === "" || password === "") { + setUsernameDirty(true); + setPasswordDirty(true); + return; + } + + setProcess(true); + userService + .login( + { + username: username, + password: password, + }, + rememberMe + ) + .then( + () => { + if (history.length === 0) { + history.push("/"); + } else { + history.goBack(); + } + }, + (e: Error) => { + setProcess(false); + setError(e.message); + } + ); + event.preventDefault(); + } + + return ( + + +
+

{t("welcome")}

+
+ + + { + setUsername(e.target.value); + setUsernameDirty(true); + }} + value={username} + invalid={usernameDirty && username === ""} + /> + {usernameDirty && username === "" && ( + {t("login.emptyUsername")} + )} + + + + { + setPassword(e.target.value); + setPasswordDirty(true); + }} + value={password} + invalid={passwordDirty && password === ""} + /> + {passwordDirty && password === "" && ( + {t("login.emptyPassword")} + )} + + + { + const v = (e.target as HTMLInputElement).checked; + setRememberMe(v); + }} + /> + + + {error ?

{t(error)}

: null} +
+ {process ? ( + + ) : ( + + )} +
+
+
+
+ ); +}; + +export default LoginPage; -- cgit v1.2.3