From f5d10683a1edeba4dabe148ff7aa682c044f7496 Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 26 Jul 2020 15:02:55 +0800 Subject: Merge front end repo --- Timeline/ClientApp/src/app/user/Login.tsx | 148 ++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 Timeline/ClientApp/src/app/user/Login.tsx (limited to 'Timeline/ClientApp/src/app/user/Login.tsx') diff --git a/Timeline/ClientApp/src/app/user/Login.tsx b/Timeline/ClientApp/src/app/user/Login.tsx new file mode 100644 index 00000000..a615d8ed --- /dev/null +++ b/Timeline/ClientApp/src/app/user/Login.tsx @@ -0,0 +1,148 @@ +import React, { Fragment, useState, useEffect } from 'react'; +import { useHistory } from 'react-router'; +import { useTranslation } from 'react-i18next'; + +import AppBar from '../common/AppBar'; + +import { useUser, userService } from '../data/user'; +import { + Label, + FormGroup, + Input, + Form, + FormFeedback, + Spinner, + Button, +} from 'reactstrap'; + +const Login: 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 + ) + .subscribe( + (_) => { + 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 Login; -- cgit v1.2.3