From bf7c63cd53dde93bffe24eb1426b8bfc2037646b Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sat, 29 Aug 2020 15:30:48 +0300 Subject: feat: use Formik in Login form --- src/containers/Login/Login.tsx | 104 +++++++++++++++++---------- src/containers/Registration/Registration.tsx | 17 ++--- 2 files changed, 72 insertions(+), 49 deletions(-) (limited to 'src/containers') diff --git a/src/containers/Login/Login.tsx b/src/containers/Login/Login.tsx index bec0db5..9814d01 100644 --- a/src/containers/Login/Login.tsx +++ b/src/containers/Login/Login.tsx @@ -1,14 +1,24 @@ -import React, { useState, useRef } from 'react'; +import React, { useState } from 'react'; import { useHistory } from 'react-router-dom'; +import { Formik, Form, Field } from 'formik'; import { makeStyles } from '@material-ui/core/styles'; import { TextField, Button, FormControlLabel, - Switch + Switch, + InputAdornment, + IconButton } from '@material-ui/core'; +import { Visibility, VisibilityOff } from '@material-ui/icons'; import { useAuth } from '../../hooks/useAuth'; +interface Fields { + username: string; + password: string; + remember: boolean; +} + const useStyles = makeStyles(theme => ({ root: { '& > *': { @@ -37,54 +47,76 @@ const useStyles = makeStyles(theme => ({ const Login: React.FC = () => { const [error, setError] = useState(false); - const [remember, setRemember] = useState(true); + const [showPassword, setShowPassword] = useState(false); const classes = useStyles(); - const nameRef = useRef(); - const passwordRef = useRef(); const { login } = useAuth(); const history = useHistory(); - const handleCheck = () => { - setRemember(!remember); - }; - - const handleSubmit = async () => { - const name = nameRef.current?.value?.toLowerCase(); - const password = passwordRef.current?.value; - if (name && password) { - login(name, password, remember).then(success => { - if (success) history.push(`/profile/${name}`); + const handleSubmit = async ({ username, password, remember }: Fields) => { + console.log({ username, password, remember }) + if (username && password) { + login(username, password, remember).then(success => { + if (success) history.push(`/profile/${username}`); else setError(true); }); - } + } else setError(true); }; - const handleRegistration = () => { history.push('/registration'); }; + const toggleShowPassword = () => { + setShowPassword(prevState => !prevState); + }; + return ( <>
Sign In
-
- - - } - label="Remember me" - /> - - + + {({ values, touched, isSubmitting }) => ( +
+ + + + {showPassword ? : } + + + ) + }} + /> + } + /> + + + )} +
{'Don\'t have an account?'}
({ color: 'green', cursor: 'pointer' }, - textField: { - height: theme.spacing(8) - } })); const Registration: React.FC = () => { @@ -75,7 +73,7 @@ const Registration: React.FC = () => { .then(() => history.push(`/profile/${username}`)); } - const handleClickShowPassword = () => { + const toggleShowPassword = () => { setShowPassword(prevState => !prevState); }; @@ -88,7 +86,7 @@ const Registration: React.FC = () => { onSubmit={handleSubmit} > {({ values, errors, touched, isSubmitting }) => ( -
+ { error={touched.username && !!errors.username} helperText={touched.username && errors.username} required - className={classes.textField} as={TextField} /> { error={touched.email && !!errors.email} helperText={touched.email && errors.email} required - className={classes.textField} as={TextField} /> { InputProps={{ endAdornment: ( - + {showPassword ? : } ) }} - className={classes.textField} /> -- cgit v1.2.3