2019-02-03 17:23:33 +07:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import Button from '@material-ui/core/Button';
|
|
|
|
import DialogTitle from '@material-ui/core/DialogTitle';
|
|
|
|
import Dialog from '@material-ui/core/Dialog';
|
|
|
|
import DialogActions from '@material-ui/core/DialogActions';
|
|
|
|
import DialogContent from '@material-ui/core/DialogContent';
|
|
|
|
import SnackbarContent from '@material-ui/core/SnackbarContent';
|
|
|
|
import ErrorIcon from '@material-ui/icons/Error';
|
|
|
|
import InputLabel from '@material-ui/core/InputLabel';
|
|
|
|
import Input from '@material-ui/core/Input';
|
|
|
|
import FormControl from '@material-ui/core/FormControl';
|
|
|
|
import FormHelperText from '@material-ui/core/FormHelperText';
|
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
// @ts-ignore
|
|
|
|
import classes from './login.scss';
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-06-24 04:44:53 +07:00
|
|
|
interface FormFields {
|
|
|
|
required: boolean;
|
|
|
|
pristine: boolean;
|
|
|
|
helperText: string;
|
|
|
|
value: string;
|
|
|
|
}
|
2019-06-26 05:29:53 +07:00
|
|
|
export interface FormError {
|
2019-06-24 04:44:53 +07:00
|
|
|
type: string;
|
|
|
|
title: string;
|
|
|
|
description: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface LoginModalProps {
|
|
|
|
visibility: boolean;
|
2019-06-26 05:29:53 +07:00
|
|
|
error?: FormError;
|
2019-06-24 04:44:53 +07:00
|
|
|
onCancel: () => void;
|
|
|
|
onSubmit: (username: string, password: string) => void;
|
|
|
|
}
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-06-24 04:44:53 +07:00
|
|
|
interface LoginModalState {
|
|
|
|
form: {
|
|
|
|
username: Partial<FormFields>;
|
|
|
|
password: Partial<FormFields>;
|
2019-06-20 19:37:28 +07:00
|
|
|
};
|
2019-06-26 05:29:53 +07:00
|
|
|
error?: FormError;
|
2019-06-24 04:44:53 +07:00
|
|
|
}
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-06-24 04:44:53 +07:00
|
|
|
export default class LoginModal extends Component<Partial<LoginModalProps>, LoginModalState> {
|
2019-06-26 05:29:53 +07:00
|
|
|
constructor(props: LoginModalProps) {
|
2019-02-03 17:23:33 +07:00
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
form: {
|
|
|
|
username: {
|
|
|
|
required: true,
|
|
|
|
pristine: true,
|
|
|
|
helperText: 'Field required',
|
|
|
|
value: '',
|
|
|
|
},
|
2019-06-20 19:37:28 +07:00
|
|
|
password: {
|
2019-02-03 17:23:33 +07:00
|
|
|
required: true,
|
|
|
|
pristine: true,
|
|
|
|
helperText: 'Field required',
|
|
|
|
value: '',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
error: props.error,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-06-24 04:44:53 +07:00
|
|
|
public render(): JSX.Element {
|
2019-06-26 05:29:53 +07:00
|
|
|
const { visibility = true, onCancel = () => null, error } = this.props as LoginModalProps;
|
2019-06-24 04:44:53 +07:00
|
|
|
return (
|
|
|
|
<Dialog fullWidth={true} id={'login--form-container'} maxWidth={'xs'} onClose={onCancel} open={visibility}>
|
|
|
|
<form noValidate={true} onSubmit={this.handleValidateCredentials}>
|
|
|
|
<DialogTitle>{'Login'}</DialogTitle>
|
|
|
|
<DialogContent>
|
2019-06-26 05:29:53 +07:00
|
|
|
{error && this.renderLoginError(error)}
|
2019-06-24 04:44:53 +07:00
|
|
|
{this.renderNameField()}
|
|
|
|
{this.renderPasswordField()}
|
|
|
|
</DialogContent>
|
|
|
|
{this.renderActions()}
|
|
|
|
</form>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-02-03 17:23:33 +07:00
|
|
|
/**
|
|
|
|
* set login modal's username and password to current state
|
|
|
|
* Required to login
|
|
|
|
*/
|
2019-06-24 04:44:53 +07:00
|
|
|
public setCredentials = (name, e) => {
|
2019-02-03 17:23:33 +07:00
|
|
|
const { form } = this.state;
|
|
|
|
this.setState({
|
|
|
|
form: {
|
|
|
|
...form,
|
|
|
|
[name]: {
|
|
|
|
...form[name],
|
|
|
|
value: e.target.value,
|
|
|
|
pristine: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2019-06-20 19:37:28 +07:00
|
|
|
};
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-06-24 04:44:53 +07:00
|
|
|
public handleUsernameChange = event => {
|
2019-02-03 17:23:33 +07:00
|
|
|
this.setCredentials('username', event);
|
2019-06-20 19:37:28 +07:00
|
|
|
};
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-06-24 04:44:53 +07:00
|
|
|
public handlePasswordChange = event => {
|
2019-02-03 17:23:33 +07:00
|
|
|
this.setCredentials('password', event);
|
2019-06-20 19:37:28 +07:00
|
|
|
};
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-06-24 04:44:53 +07:00
|
|
|
public handleValidateCredentials = event => {
|
2019-02-03 17:23:33 +07:00
|
|
|
const { form } = this.state;
|
|
|
|
// prevents default submit behavior
|
|
|
|
event.preventDefault();
|
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
this.setState(
|
|
|
|
{
|
|
|
|
form: Object.keys(form).reduce(
|
|
|
|
(acc, key) => ({
|
|
|
|
...acc,
|
|
|
|
...{ [key]: { ...form[key], pristine: false } },
|
|
|
|
}),
|
2019-06-24 04:44:53 +07:00
|
|
|
{ username: {}, password: {} }
|
2019-06-20 19:37:28 +07:00
|
|
|
),
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
if (!Object.keys(form).some(id => !form[id])) {
|
|
|
|
this.submitCredentials();
|
|
|
|
}
|
2019-02-03 17:23:33 +07:00
|
|
|
}
|
2019-06-20 19:37:28 +07:00
|
|
|
);
|
|
|
|
};
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-06-24 04:44:53 +07:00
|
|
|
public submitCredentials = async () => {
|
2019-02-03 17:23:33 +07:00
|
|
|
const { form } = this.state;
|
2019-06-24 04:44:53 +07:00
|
|
|
const username = (form.username && form.username.value) || '';
|
|
|
|
const password = (form.password && form.password.value) || '';
|
2019-02-03 17:23:33 +07:00
|
|
|
const { onSubmit } = this.props;
|
2019-06-24 04:44:53 +07:00
|
|
|
if (onSubmit) {
|
|
|
|
await onSubmit(username, password);
|
|
|
|
}
|
2019-02-03 17:23:33 +07:00
|
|
|
// let's wait for API response and then set
|
|
|
|
// username and password filed to empty state
|
|
|
|
this.setState({
|
2019-06-20 19:37:28 +07:00
|
|
|
form: Object.keys(form).reduce(
|
|
|
|
(acc, key) => ({
|
|
|
|
...acc,
|
|
|
|
...{ [key]: { ...form[key], value: '', pristine: true } },
|
|
|
|
}),
|
2019-06-24 04:44:53 +07:00
|
|
|
{ username: {}, password: {} }
|
2019-06-20 19:37:28 +07:00
|
|
|
),
|
2019-02-03 17:23:33 +07:00
|
|
|
});
|
2019-06-20 19:37:28 +07:00
|
|
|
};
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-06-26 05:29:53 +07:00
|
|
|
public renderErrorMessage(title, description): JSX.Element {
|
2019-02-03 17:23:33 +07:00
|
|
|
return (
|
|
|
|
<span>
|
|
|
|
<div>
|
2019-06-20 19:37:28 +07:00
|
|
|
<strong>{title}</strong>
|
2019-02-03 17:23:33 +07:00
|
|
|
</div>
|
2019-06-20 19:37:28 +07:00
|
|
|
<div>{description}</div>
|
|
|
|
</span>
|
|
|
|
);
|
2019-02-03 17:23:33 +07:00
|
|
|
}
|
|
|
|
|
2019-06-26 05:29:53 +07:00
|
|
|
public renderMessage(title, description): JSX.Element {
|
2019-02-03 17:23:33 +07:00
|
|
|
return (
|
2019-06-20 19:37:28 +07:00
|
|
|
<div className={classes.loginErrorMsg} id={'client-snackbar'}>
|
2019-02-03 17:23:33 +07:00
|
|
|
<ErrorIcon className={classes.loginIcon} />
|
|
|
|
{this.renderErrorMessage(title, description)}
|
2019-06-20 19:37:28 +07:00
|
|
|
</div>
|
|
|
|
);
|
2019-02-03 17:23:33 +07:00
|
|
|
}
|
|
|
|
|
2019-06-26 05:29:53 +07:00
|
|
|
public renderLoginError({ type, title, description }: FormError): JSX.Element | false {
|
2019-06-20 19:37:28 +07:00
|
|
|
return type === 'error' && <SnackbarContent className={classes.loginError} message={this.renderMessage(title, description)} />;
|
2019-02-03 17:23:33 +07:00
|
|
|
}
|
|
|
|
|
2019-06-26 05:29:53 +07:00
|
|
|
public renderNameField = () => {
|
2019-06-20 19:37:28 +07:00
|
|
|
const {
|
|
|
|
form: { username },
|
|
|
|
} = this.state;
|
2019-02-03 17:23:33 +07:00
|
|
|
return (
|
2019-06-20 19:37:28 +07:00
|
|
|
<FormControl error={!username.value && !username.pristine} fullWidth={true} required={username.required}>
|
|
|
|
<InputLabel htmlFor={'username'}>{'Username'}</InputLabel>
|
2019-06-24 04:44:53 +07:00
|
|
|
<Input id={'login--form-username'} onChange={this.handleUsernameChange} placeholder={'Your username'} value={username.value} />
|
2019-06-20 19:37:28 +07:00
|
|
|
{!username.value && !username.pristine && <FormHelperText id={'username-error'}>{username.helperText}</FormHelperText>}
|
2019-02-03 17:23:33 +07:00
|
|
|
</FormControl>
|
|
|
|
);
|
2019-06-20 19:37:28 +07:00
|
|
|
};
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-06-26 05:29:53 +07:00
|
|
|
public renderPasswordField = () => {
|
2019-06-20 19:37:28 +07:00
|
|
|
const {
|
|
|
|
form: { password },
|
|
|
|
} = this.state;
|
2019-02-03 17:23:33 +07:00
|
|
|
return (
|
2019-06-20 19:37:28 +07:00
|
|
|
<FormControl error={!password.value && !password.pristine} fullWidth={true} required={password.required} style={{ marginTop: '8px' }}>
|
|
|
|
<InputLabel htmlFor={'password'}>{'Password'}</InputLabel>
|
2019-06-24 04:44:53 +07:00
|
|
|
<Input id={'login--form-password'} onChange={this.handlePasswordChange} placeholder={'Your strong password'} type={'password'} value={password.value} />
|
2019-06-20 19:37:28 +07:00
|
|
|
{!password.value && !password.pristine && <FormHelperText id={'password-error'}>{password.helperText}</FormHelperText>}
|
2019-02-03 17:23:33 +07:00
|
|
|
</FormControl>
|
|
|
|
);
|
2019-06-20 19:37:28 +07:00
|
|
|
};
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-06-26 05:29:53 +07:00
|
|
|
public renderActions = () => {
|
2019-06-20 19:37:28 +07:00
|
|
|
const {
|
|
|
|
form: { username, password },
|
|
|
|
} = this.state;
|
2019-02-03 17:23:33 +07:00
|
|
|
const { onCancel } = this.props;
|
|
|
|
return (
|
2019-06-20 19:37:28 +07:00
|
|
|
<DialogActions className={'dialog-footer'}>
|
|
|
|
<Button color={'inherit'} id={'login--form-cancel'} onClick={onCancel} type={'button'}>
|
2019-02-03 17:23:33 +07:00
|
|
|
{'Cancel'}
|
|
|
|
</Button>
|
2019-06-20 19:37:28 +07:00
|
|
|
<Button color={'inherit'} disabled={!password.value || !username.value} id={'login--form-submit'} type={'submit'}>
|
2019-02-03 17:23:33 +07:00
|
|
|
{'Login'}
|
|
|
|
</Button>
|
|
|
|
</DialogActions>
|
|
|
|
);
|
2019-06-20 19:37:28 +07:00
|
|
|
};
|
2019-02-03 17:23:33 +07:00
|
|
|
}
|