Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix/CLAN6-120-clear-login-input-fields #71

Merged
merged 2 commits into from
Oct 11, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cogboard-webapp/src/actions/actionCreators.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
SAVE_DATA_SUCCESS,
LOGIN_SUCCESS,
LOGIN_FAILURE,
CLEAR_LOGIN_ERROR_MESSAGE,
LOGOUT,
INIT_BOARD_PROPS
} from './types';
Expand All @@ -36,6 +37,10 @@ export const loginFailure = (data) => ({
payload: data
});

export const clearLoginErrorMessage = () => ({
type: CLEAR_LOGIN_ERROR_MESSAGE,
});

export const logout = () => ({
type: LOGOUT
});
Expand Down
1 change: 1 addition & 0 deletions cogboard-webapp/src/actions/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export const SAVE_DATA_START = 'SAVE_DATA_START';
export const SAVE_DATA_SUCCESS = 'SAVE_DATA_SUCCESS';
export const LOGIN_SUCCESS = 'LOGIN_SUCCESS';
export const LOGIN_FAILURE = 'LOGIN_FAILURE';
export const CLEAR_LOGIN_ERROR_MESSAGE = 'CLEAR_LOGIN_ERROR_MESSAGE';
export const LOGOUT = 'LOGOUT';
41 changes: 26 additions & 15 deletions cogboard-webapp/src/components/UserLogin.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React, {useEffect} from 'react';
import { useDispatch, useSelector } from 'react-redux';

import { useFormData, useToggle } from '../hooks';
import { useToggle } from '../hooks';
import { login, logout } from '../actions/thunks';
import { clearLoginErrorMessage } from "../actions/actionCreators";
import { getIsAuthenticated } from '../selectors';

import { Button, IconButton, TextField, Typography } from '@material-ui/core';
Expand All @@ -13,7 +14,6 @@ import { StyledFieldset } from './styled';

const UserLogin = () => {
const dispatch = useDispatch();
const {values, handleChange} = useFormData({username: '', password: ''});
const errorMsg = useSelector(({app}) => app.loginErrorMessage);
const isAuthenticated = useSelector(getIsAuthenticated);
const [dialogOpened, openDialog, handleDialogClose] = useToggle();
Expand All @@ -27,13 +27,14 @@ const UserLogin = () => {
}
}, [isAuthenticated, handleDialogClose, openLoginSnackbar]);

const handleLoginButtonClick = (credentials) => () => {
const handleLoginButtonClick = () => {
const credentials = getCredentials();
dispatch(login(credentials))
};

const handleLoginOnEnterPress = (event, credentials) => {
const handleLoginOnEnterPress = event => {
if (event.key === 'Enter') {
dispatch(login(credentials))
handleLoginButtonClick();
}
};

Expand All @@ -46,6 +47,20 @@ const UserLogin = () => {
openLogoutSnackbar();
};

const getCredentials = () => {
goeson00 marked this conversation as resolved.
Show resolved Hide resolved
const usernameField = document.getElementById("username");
const passwordField = document.getElementById("password");
return {
username: usernameField ? usernameField.value : "",
password: passwordField ? passwordField.value : "",
};
};

function closeDialog() {
handleDialogClose();
dispatch(clearLoginErrorMessage());
}

return (
<>
{!isAuthenticated &&
Expand All @@ -71,7 +86,7 @@ const UserLogin = () => {
</IconButton>
}
<AppDialog
handleDialogClose={handleDialogClose}
handleDialogClose={closeDialog}
open={dialogOpened}
title='User Login'>
<StyledFieldset component="fieldset">
Expand All @@ -80,33 +95,29 @@ const UserLogin = () => {
{errorMsg}
</Typography>}
<TextField
onChange={handleChange('username')}
id="username"
InputLabelProps={{
shrink: true
}}
label="Username"
margin="normal"
value={values.username}
onKeyPress={(ev) => handleLoginOnEnterPress(ev, values)}
onKeyPress={handleLoginOnEnterPress}
inputProps={{'data-cy': 'user-login-username-input'}}
/>
<TextField
onChange={handleChange('password')}
id="password"
InputLabelProps={{
shrink: true
}}
type="password"
label="Password"
margin="normal"
value={values.password}
onKeyPress={(ev) => handleLoginOnEnterPress(ev, values)}
onKeyPress={handleLoginOnEnterPress}
inputProps={{'data-cy': 'user-login-password-input'}}
/>
<Button
color="primary"
onClick={handleLoginButtonClick(values)}
onClick={handleLoginButtonClick}
variant="contained"
data-cy='user-login-submit-button'>
Login
Expand All @@ -117,7 +128,7 @@ const UserLogin = () => {
open={loginSnackbarOpened}
handleClose={handleLoginSnackbarClose}
hideAfter={3000}
message={`Logged in as ${values.username}`}
message={`Logged in as ${getCredentials().username}`}
vertical="top"
horizontal="center"
variant="success"
Expand All @@ -126,7 +137,7 @@ const UserLogin = () => {
open={logoutSnackbarOpened}
handleClose={handleLogoutSnackbarClose}
hideAfter={3000}
message={`${values.username} was logged out successfully`}
message={`Logged out successfully`}
vertical="top"
horizontal="center"
variant="info"
Expand Down
3 changes: 2 additions & 1 deletion cogboard-webapp/src/reducers/app/loginErrorMessage.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { LOGIN_FAILURE, LOGIN_SUCCESS, LOGOUT } from '../../actions/types';
import { LOGIN_FAILURE, LOGIN_SUCCESS, CLEAR_LOGIN_ERROR_MESSAGE, LOGOUT } from '../../actions/types';

const loginErrorMessage = (state = '', { type, payload }) => {
switch (type) {
case LOGIN_FAILURE:
return payload;
case LOGIN_SUCCESS:
case CLEAR_LOGIN_ERROR_MESSAGE:
case LOGOUT:
return '';
default:
Expand Down