Skip to content
This repository has been archived by the owner on Aug 5, 2019. It is now read-only.

Commit

Permalink
Refactor Login component
Browse files Browse the repository at this point in the history
  • Loading branch information
sbardian committed Mar 28, 2019
1 parent 95c47f4 commit b203e18
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions src/client/components/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const Login = ({ classes, history }) => {
const [password, setPassword] = useState('');
const [loginFailure, setLoginFailure] = useState(false);

const clickLogin = event => {
const clickLoginButton = event => {
const loginButton = document.getElementById('login-button');
if (event.keyCode === 13) {
loginButton.click();
Expand All @@ -69,22 +69,24 @@ const Login = ({ classes, history }) => {
useEffect(() => {
document
.getElementById('password')
.addEventListener('keypress', clickLogin);
document.getElementById('email').addEventListener('keypress', clickLogin);
.addEventListener('keypress', clickLoginButton);
document
.getElementById('email')
.addEventListener('keypress', clickLoginButton);
return () => {
document.removeEventListener('password', clickLogin);
document.removeEventListener('email', clickLogin);
document.removeEventListener('password', clickLoginButton);
document.removeEventListener('email', clickLoginButton);
};
}, []);

const { updateUsername } = useContext(UsernameContext);

const register = () => {
const registerButtonClick = () => {
history.push('register');
};

const userLogin = e => {
e.preventDefault();
const userLogin = event => {
event.preventDefault();
utils
.userLogin(email, password)
.then(data => {
Expand All @@ -99,18 +101,12 @@ const Login = ({ classes, history }) => {
});
};

const handleChange = event => {
const { value, id } = event.target;
switch (id) {
case 'email':
setEmail(value);
break;
case 'password':
setPassword(value);
break;
default:
break;
}
const handleEmailChange = event => {
setEmail(event.target.value);
};

const handlePasswordChange = event => {
setPassword(event.target.value);
};

return (
Expand All @@ -137,7 +133,7 @@ const Login = ({ classes, history }) => {
<TextValidator
id="email"
label="Email"
onChange={handleChange}
onChange={handleEmailChange}
name="email"
placeholder="user@domain.com"
className={classes.textField}
Expand All @@ -152,7 +148,7 @@ const Login = ({ classes, history }) => {
<TextValidator
id="password"
label="Password"
onChange={handleChange}
onChange={handlePasswordChange}
name="password"
placeholder="Password"
className={classes.textField}
Expand All @@ -176,7 +172,7 @@ const Login = ({ classes, history }) => {
<Button
variant="contained"
className={classes.button}
onClick={() => register()}
onClick={() => registerButtonClick()}
>
Register
</Button>
Expand Down

0 comments on commit b203e18

Please sign in to comment.