|
1 |
| -// tslint:disable:no-magic-numbers |
2 |
| -/* istanbul ignore next */ |
3 | 1 | import Avatar from '@material-ui/core/Avatar';
|
4 | 2 | import Button from '@material-ui/core/Button';
|
5 |
| -import Container from '@material-ui/core/Container'; |
6 |
| -import CssBaseline from '@material-ui/core/CssBaseline'; |
7 | 3 | import Grid from '@material-ui/core/Grid';
|
8 | 4 | import TextField from '@material-ui/core/TextField';
|
9 | 5 | import Typography from '@material-ui/core/Typography';
|
10 | 6 | import LockOutlinedIcon from '@material-ui/icons/LockOutlined';
|
11 | 7 | import { Formik } from 'formik';
|
| 8 | +import _isNil from 'ramda/src/isNil'; |
12 | 9 | import React from 'react';
|
13 | 10 | import { useTranslation } from 'react-i18next';
|
14 |
| -import { RouteProps } from 'react-router'; |
| 11 | +import { useDispatch, useSelector } from 'react-redux'; |
15 | 12 | import { Link } from 'react-router-dom';
|
16 | 13 | import PasswordField from '../../atoms/PasswordField';
|
17 | 14 | import { REGISTER, REMIND_PASSWORD } from '../../constants/routes';
|
18 | 15 | import { LoginOptions, loginRequested } from '../../redux/auth/actionCreators';
|
19 |
| -import { AuthState } from '../../redux/auth/reducer'; |
| 16 | +import { State } from '../../redux/rootReducer'; |
20 | 17 | import loginSchema from '../../utils/schemas/login';
|
21 | 18 | import useStyles from './styles';
|
22 | 19 |
|
23 |
| -interface LoginFormProps extends AuthState, RouteProps { |
24 |
| - readonly login: (options: LoginOptions) => ReturnType<typeof loginRequested>; |
25 |
| -} |
26 |
| - |
27 |
| -const LoginForm = (props: LoginFormProps) => { |
| 20 | +const LoginForm = () => { |
28 | 21 | const classes = useStyles();
|
| 22 | + |
29 | 23 | const { t } = useTranslation();
|
30 | 24 |
|
31 |
| - const { loginLoading, login } = props; |
| 25 | + const { loginLoading } = useSelector(({ auth }: State) => auth); |
| 26 | + |
| 27 | + const dispatch = useDispatch(); |
| 28 | + |
| 29 | + const login = (options: LoginOptions) => dispatch(loginRequested(options)); |
32 | 30 |
|
33 | 31 | return (
|
34 |
| - <Container component="main" maxWidth="xs"> |
35 |
| - <CssBaseline /> |
36 |
| - <div className={classes.paper}> |
37 |
| - <Avatar className={classes.avatar}> |
38 |
| - <LockOutlinedIcon /> |
39 |
| - </Avatar> |
| 32 | + <div className={classes.paper}> |
| 33 | + <Avatar className={classes.avatar}> |
| 34 | + <LockOutlinedIcon /> |
| 35 | + </Avatar> |
40 | 36 |
|
41 |
| - <Typography component="h1" variant="h5"> |
42 |
| - {t('auth.login')} |
43 |
| - </Typography> |
| 37 | + <Typography component="h1" variant="h5"> |
| 38 | + {t('auth.login')} |
| 39 | + </Typography> |
44 | 40 |
|
45 |
| - <Formik |
46 |
| - validationSchema={loginSchema} |
47 |
| - initialValues={{ email: '', password: '' }} |
48 |
| - validateOnChange={false} |
49 |
| - onSubmit={login} |
50 |
| - render={({ |
51 |
| - handleSubmit, |
52 |
| - handleChange, |
53 |
| - handleBlur, |
54 |
| - values, |
55 |
| - errors, |
56 |
| - touched, |
57 |
| - }) => { |
58 |
| - const hasEmailError = Boolean(errors.email && touched.email); |
59 |
| - const hasPasswordError = Boolean( |
60 |
| - errors.password && touched.password |
61 |
| - ); |
| 41 | + <Formik |
| 42 | + validationSchema={loginSchema} |
| 43 | + initialValues={{ email: '', password: '' }} |
| 44 | + validateOnChange={false} |
| 45 | + onSubmit={login} |
| 46 | + render={({ |
| 47 | + handleSubmit, |
| 48 | + handleChange, |
| 49 | + handleBlur, |
| 50 | + values, |
| 51 | + errors, |
| 52 | + touched, |
| 53 | + }) => { |
| 54 | + const hasEmailError = Boolean(errors.email && touched.email); |
| 55 | + const hasPasswordError = Boolean(errors.password && touched.password); |
62 | 56 |
|
63 |
| - return ( |
64 |
| - <form className={classes.form} noValidate onSubmit={handleSubmit}> |
65 |
| - <TextField |
66 |
| - helperText={errors.email} |
67 |
| - error={hasEmailError} |
68 |
| - variant="outlined" |
69 |
| - margin="normal" |
70 |
| - required |
71 |
| - fullWidth |
72 |
| - id="email" |
73 |
| - label={t('auth.email')} |
74 |
| - name="email" |
75 |
| - autoComplete="email" |
76 |
| - autoFocus |
77 |
| - value={values.email} |
78 |
| - onChange={handleChange} |
79 |
| - onBlur={handleBlur} |
80 |
| - /> |
| 57 | + return ( |
| 58 | + <form className={classes.form} noValidate onSubmit={handleSubmit}> |
| 59 | + <TextField |
| 60 | + helperText={errors.email} |
| 61 | + error={hasEmailError} |
| 62 | + variant="outlined" |
| 63 | + margin="normal" |
| 64 | + required |
| 65 | + fullWidth |
| 66 | + id="email" |
| 67 | + label={t('auth.email')} |
| 68 | + name="email" |
| 69 | + autoComplete="email" |
| 70 | + autoFocus |
| 71 | + value={values.email} |
| 72 | + onChange={handleChange} |
| 73 | + onBlur={handleBlur} |
| 74 | + /> |
81 | 75 |
|
82 |
| - <PasswordField |
83 |
| - helperText={errors.password} |
84 |
| - error={hasPasswordError} |
85 |
| - variant="outlined" |
86 |
| - margin="normal" |
87 |
| - required |
88 |
| - fullWidth |
89 |
| - name="password" |
90 |
| - label={t('auth.password')} |
91 |
| - id="password" |
92 |
| - autoComplete="current-password" |
93 |
| - value={values.password} |
94 |
| - onChange={handleChange} |
95 |
| - onBlur={handleBlur} |
96 |
| - /> |
| 76 | + <PasswordField |
| 77 | + helperText={errors.password} |
| 78 | + error={hasPasswordError} |
| 79 | + variant="outlined" |
| 80 | + margin="normal" |
| 81 | + required |
| 82 | + fullWidth |
| 83 | + name="password" |
| 84 | + label={t('auth.password')} |
| 85 | + id="password" |
| 86 | + autoComplete="current-password" |
| 87 | + value={values.password} |
| 88 | + onChange={handleChange} |
| 89 | + onBlur={handleBlur} |
| 90 | + /> |
97 | 91 |
|
98 |
| - <Button |
99 |
| - disabled={loginLoading} |
100 |
| - type="submit" |
101 |
| - fullWidth |
102 |
| - variant="contained" |
103 |
| - color="primary" |
104 |
| - size="large" |
105 |
| - className={classes.submit} |
106 |
| - > |
107 |
| - {t('auth.loginAction')} |
108 |
| - </Button> |
| 92 | + <Button |
| 93 | + disabled={loginLoading} |
| 94 | + type="submit" |
| 95 | + fullWidth |
| 96 | + variant="contained" |
| 97 | + color="primary" |
| 98 | + size="large" |
| 99 | + className={classes.submit} |
| 100 | + > |
| 101 | + {t('auth.loginAction')} |
| 102 | + </Button> |
109 | 103 |
|
110 |
| - <Grid container> |
111 |
| - <Grid item xs> |
112 |
| - <Link to={REMIND_PASSWORD}> |
113 |
| - {t('auth.forgotPassword')} |
114 |
| - </Link> |
115 |
| - </Grid> |
116 |
| - <Grid item> |
117 |
| - <Link to={REGISTER}>{t('auth.dontHaveAccount')}</Link> |
118 |
| - </Grid> |
| 104 | + <Grid container> |
| 105 | + <Grid item xs> |
| 106 | + <Link to={REMIND_PASSWORD}>{t('auth.forgotPassword')}</Link> |
| 107 | + </Grid> |
| 108 | + <Grid item> |
| 109 | + <Link to={REGISTER}>{t('auth.dontHaveAccount')}</Link> |
119 | 110 | </Grid>
|
120 |
| - </form> |
121 |
| - ); |
122 |
| - }} |
123 |
| - /> |
124 |
| - </div> |
125 |
| - </Container> |
| 111 | + </Grid> |
| 112 | + </form> |
| 113 | + ); |
| 114 | + }} |
| 115 | + /> |
| 116 | + </div> |
126 | 117 | );
|
127 | 118 | };
|
128 | 119 | // tslint:disable-next-line:max-file-line-count
|
|
0 commit comments