diff options
| author | crupest <crupest@outlook.com> | 2020-08-25 00:05:01 +0800 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-25 00:05:01 +0800 | 
| commit | 2867b65d2215ea89cca0e5c5cfbb536d08f60263 (patch) | |
| tree | 07ae296c0c684fd81b30763ef9d6d27421de30b3 /Timeline/ClientApp/src/app/user/Login.tsx | |
| parent | 4eb7a984e10d15fdb8ef988e1571b114fa74e420 (diff) | |
| parent | 86e22ca4fb7dceec43a528bd250bcda2a0133421 (diff) | |
| download | timeline-2867b65d2215ea89cca0e5c5cfbb536d08f60263.tar.gz timeline-2867b65d2215ea89cca0e5c5cfbb536d08f60263.tar.bz2 timeline-2867b65d2215ea89cca0e5c5cfbb536d08f60263.zip | |
Merge pull request #154 from crupest/toolchain
Upgrade toolchain of front end.
Diffstat (limited to 'Timeline/ClientApp/src/app/user/Login.tsx')
| -rw-r--r-- | Timeline/ClientApp/src/app/user/Login.tsx | 295 | 
1 files changed, 147 insertions, 148 deletions
| diff --git a/Timeline/ClientApp/src/app/user/Login.tsx b/Timeline/ClientApp/src/app/user/Login.tsx index 2f2a3188..db6c43c4 100644 --- a/Timeline/ClientApp/src/app/user/Login.tsx +++ b/Timeline/ClientApp/src/app/user/Login.tsx @@ -1,148 +1,147 @@ -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<string>('');
 -  const [usernameDirty, setUsernameDirty] = useState<boolean>(false);
 -  const [password, setPassword] = useState<string>('');
 -  const [passwordDirty, setPasswordDirty] = useState<boolean>(false);
 -  const [rememberMe, setRememberMe] = useState<boolean>(true);
 -  const [process, setProcess] = useState<boolean>(false);
 -  const [error, setError] = useState<string | null>(null);
 -
 -  const user = useUser();
 -
 -  useEffect(() => {
 -    if (user != null) {
 -      const id = setTimeout(() => history.push('/'), 3000);
 -      return () => {
 -        clearTimeout(id);
 -      };
 -    }
 -  }, [history, user]);
 -
 -  if (user != null) {
 -    return (
 -      <>
 -        <AppBar />
 -        <p className="mt-appbar">{t('login.alreadyLogin')}</p>
 -      </>
 -    );
 -  }
 -
 -  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 (
 -    <Fragment>
 -      <AppBar />
 -      <div className="container login-container mt-appbar">
 -        <h1>{t('welcome')}</h1>
 -        <Form>
 -          <FormGroup>
 -            <Label for="username">{t('user.username')}</Label>
 -            <Input
 -              id="username"
 -              disabled={process}
 -              onChange={(e) => {
 -                setUsername(e.target.value);
 -                setUsernameDirty(true);
 -              }}
 -              value={username}
 -              invalid={usernameDirty && username === ''}
 -            />
 -            {usernameDirty && username === '' && (
 -              <FormFeedback>{t('login.emptyUsername')}</FormFeedback>
 -            )}
 -          </FormGroup>
 -          <FormGroup>
 -            <Label for="password">{t('user.password')}</Label>
 -            <Input
 -              id="password"
 -              type="password"
 -              disabled={process}
 -              onChange={(e) => {
 -                setPassword(e.target.value);
 -                setPasswordDirty(true);
 -              }}
 -              value={password}
 -              invalid={passwordDirty && password === ''}
 -            />
 -            {passwordDirty && password === '' && (
 -              <FormFeedback>{t('login.emptyPassword')}</FormFeedback>
 -            )}
 -          </FormGroup>
 -          <FormGroup check>
 -            <Input
 -              id="remember-me"
 -              type="checkbox"
 -              checked={rememberMe}
 -              onChange={(e) => {
 -                const v = (e.target as HTMLInputElement).checked;
 -                setRememberMe(v);
 -              }}
 -            />
 -            <Label for="remember-me">{t('user.rememberMe')}</Label>
 -          </FormGroup>
 -          {error ? <p className="text-error">{t(error)}</p> : null}
 -          <div>
 -            {process ? (
 -              <Spinner />
 -            ) : (
 -              <Button color="primary" onClick={onSubmit}>
 -                {t('user.login')}
 -              </Button>
 -            )}
 -          </div>
 -        </Form>
 -      </div>
 -    </Fragment>
 -  );
 -};
 -
 -export default Login;
 +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 AppBar from "../common/AppBar"; +import { useUser, userService } from "../data/user"; + +const Login: React.FC = (_) => { +  const { t } = useTranslation(); +  const history = useHistory(); +  const [username, setUsername] = useState<string>(""); +  const [usernameDirty, setUsernameDirty] = useState<boolean>(false); +  const [password, setPassword] = useState<string>(""); +  const [passwordDirty, setPasswordDirty] = useState<boolean>(false); +  const [rememberMe, setRememberMe] = useState<boolean>(true); +  const [process, setProcess] = useState<boolean>(false); +  const [error, setError] = useState<string | null>(null); + +  const user = useUser(); + +  useEffect(() => { +    if (user != null) { +      const id = setTimeout(() => history.push("/"), 3000); +      return () => { +        clearTimeout(id); +      }; +    } +  }, [history, user]); + +  if (user != null) { +    return ( +      <> +        <AppBar /> +        <p className="mt-appbar">{t("login.alreadyLogin")}</p> +      </> +    ); +  } + +  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 ( +    <Fragment> +      <AppBar /> +      <div className="container login-container mt-appbar"> +        <h1>{t("welcome")}</h1> +        <Form> +          <FormGroup> +            <Label for="username">{t("user.username")}</Label> +            <Input +              id="username" +              disabled={process} +              onChange={(e) => { +                setUsername(e.target.value); +                setUsernameDirty(true); +              }} +              value={username} +              invalid={usernameDirty && username === ""} +            /> +            {usernameDirty && username === "" && ( +              <FormFeedback>{t("login.emptyUsername")}</FormFeedback> +            )} +          </FormGroup> +          <FormGroup> +            <Label for="password">{t("user.password")}</Label> +            <Input +              id="password" +              type="password" +              disabled={process} +              onChange={(e) => { +                setPassword(e.target.value); +                setPasswordDirty(true); +              }} +              value={password} +              invalid={passwordDirty && password === ""} +            /> +            {passwordDirty && password === "" && ( +              <FormFeedback>{t("login.emptyPassword")}</FormFeedback> +            )} +          </FormGroup> +          <FormGroup check> +            <Input +              id="remember-me" +              type="checkbox" +              checked={rememberMe} +              onChange={(e) => { +                const v = (e.target as HTMLInputElement).checked; +                setRememberMe(v); +              }} +            /> +            <Label for="remember-me">{t("user.rememberMe")}</Label> +          </FormGroup> +          {error ? <p className="text-error">{t(error)}</p> : null} +          <div> +            {process ? ( +              <Spinner /> +            ) : ( +              <Button color="primary" onClick={onSubmit}> +                {t("user.login")} +              </Button> +            )} +          </div> +        </Form> +      </div> +    </Fragment> +  ); +}; + +export default Login; | 
