From fa133c40edb633c63d37619682ba0771d4481ed9 Mon Sep 17 00:00:00 2001 From: ilyayudovin Date: Fri, 28 Aug 2020 02:24:00 +0300 Subject: fix input validation errors --- src/containers/Registration/Registration.tsx | 103 ++++++++++++++++----------- 1 file changed, 63 insertions(+), 40 deletions(-) (limited to 'src/containers/Registration') diff --git a/src/containers/Registration/Registration.tsx b/src/containers/Registration/Registration.tsx index 9bcea8e..7d2a758 100644 --- a/src/containers/Registration/Registration.tsx +++ b/src/containers/Registration/Registration.tsx @@ -1,10 +1,13 @@ import React, { useState, useRef } from 'react'; import { useHistory } from 'react-router-dom'; import { makeStyles } from '@material-ui/core/styles'; -import TextField from '@material-ui/core/TextField'; -import Button from '@material-ui/core/Button'; -import CheckCircleIcon from '@material-ui/icons/CheckCircle'; -import InputAdornment from '@material-ui/core/InputAdornment'; +import { + TextField, + Button, + InputAdornment, + IconButton +} from '@material-ui/core'; +import { CheckCircle, Visibility, VisibilityOff } from '@material-ui/icons'; import { post } from '../../requests'; import { useAuth } from '../../hooks/useAuth'; @@ -35,18 +38,20 @@ const useStyles = makeStyles(theme => ({ } })); -const inputStyle = { WebkitBoxShadow: '0 0 0 1000px snow inset' }; - +interface ValidationStates { + validUsername: boolean | undefined; + validEmail: boolean | undefined; + validPassword: boolean | undefined; + showPassword: boolean; +} const Registration: React.FC = () => { - const errorOutputs = { - usernameError: 'Username is required', - emailError: 'Invalid email address', - passwordError: 'Should be at least 6 characters' - }; - const [errorPassword, setErrorPassword] = useState(false); - const [errorEmail, setErrorEmail] = useState(false); - const [errorUsername, setErrorUsername] = useState(false); + const [values, setValues] = useState({ + validUsername: undefined, + validEmail: undefined, + validPassword: undefined, + showPassword: false + }); const classes = useStyles(); const usernameRef = useRef(); @@ -55,11 +60,15 @@ const Registration: React.FC = () => { const { login } = useAuth(); const history = useHistory(); + const checkFromValidation = () => { + return values.validUsername && values.validEmail && values.validPassword; + }; + const handleSubmit = () => { const username = usernameRef.current?.value?.toLowerCase(); const password = passwordRef.current?.value; const email = emailRef.current?.value; - if (username && password) { + if (username && password && checkFromValidation()) { post('/users', { username, password, email }) .then(() => login(username, password)) .then(() => history.push(`/profile/${username}`)); @@ -70,14 +79,20 @@ const Registration: React.FC = () => { history.push('/login'); }; - const handleLoginChange = (e: React.ChangeEvent) => { - setErrorUsername(e.currentTarget.value.length === 0); + const handleClickShowPassword = () => { + setValues({ ...values, showPassword: !values.showPassword }); + }; + const handleMouseDownPassword = (event: React.MouseEvent) => { + event.preventDefault(); + }; + const handleUsernameChange = (e: React.ChangeEvent) => { + setValues({ ...values, validUsername: e.currentTarget.value.length > 0 }); }; const handleEmailChange = (e: React.ChangeEvent) => { - setErrorEmail(!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i.test(e.currentTarget.value)); + setValues({ ...values, validEmail: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i.test(e.currentTarget.value) }); }; const handlePasswordChange = (e: React.ChangeEvent) => { - setErrorPassword(e.currentTarget.value.length < 6); + setValues({ ...values, validPassword: e.currentTarget.value.length > 6 }); }; return ( @@ -87,46 +102,54 @@ const Registration: React.FC = () => { + {values.validUsername && values.validUsername !== undefined && } + + ) + }} /> - + {values.validEmail && values.validEmail !== undefined && } - ), - inputProps: { - style: inputStyle - } + ) }} /> - + + {values.showPassword ? : } + + {values.validPassword && values.validPassword !== undefined && } - ), - inputProps: { - style: inputStyle - } + ) }} /> -- cgit v1.2.3