2019-10-19 16:38:07 +02:00
|
|
|
import React from 'react';
|
|
|
|
|
import { translate } from 'react-i18next';
|
|
|
|
|
import styled from 'styled-components';
|
2019-10-09 16:17:02 +02:00
|
|
|
import {
|
|
|
|
|
Image,
|
|
|
|
|
ErrorNotification,
|
|
|
|
|
InputField,
|
|
|
|
|
SubmitButton,
|
2019-10-19 16:38:07 +02:00
|
|
|
UnauthorizedError,
|
|
|
|
|
} from '@scm-manager/ui-components';
|
2019-08-13 08:23:37 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
2019-10-19 16:38:07 +02:00
|
|
|
error?: Error;
|
|
|
|
|
loading: boolean;
|
|
|
|
|
loginHandler: (username: string, password: string) => void;
|
2019-08-13 08:23:37 +02:00
|
|
|
|
|
|
|
|
// context props
|
2019-10-19 16:38:07 +02:00
|
|
|
t: (p: string) => string;
|
2019-08-13 08:23:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type State = {
|
2019-10-19 16:38:07 +02:00
|
|
|
username: string;
|
|
|
|
|
password: string;
|
2019-08-13 08:23:37 +02:00
|
|
|
};
|
|
|
|
|
|
2019-10-09 16:17:02 +02:00
|
|
|
const TopMarginBox = styled.div`
|
|
|
|
|
margin-top: 5rem;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const AvatarWrapper = styled.figure`
|
|
|
|
|
margin-top: -70px;
|
|
|
|
|
padding-bottom: 20px;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const AvatarImage = styled(Image)`
|
|
|
|
|
width: 128px;
|
|
|
|
|
height: 128px;
|
|
|
|
|
padding: 5px;
|
|
|
|
|
background: #fff;
|
|
|
|
|
border: 1px solid lightgray;
|
|
|
|
|
border-radius: 50%;
|
|
|
|
|
`;
|
2019-08-13 08:23:37 +02:00
|
|
|
|
2019-10-09 16:17:02 +02:00
|
|
|
class LoginForm extends React.Component<Props, State> {
|
2019-08-13 08:23:37 +02:00
|
|
|
constructor(props: Props) {
|
|
|
|
|
super(props);
|
2019-10-19 16:38:07 +02:00
|
|
|
this.state = {
|
|
|
|
|
username: '',
|
|
|
|
|
password: '',
|
|
|
|
|
};
|
2019-08-13 08:23:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleSubmit = (event: Event) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
if (this.isValid()) {
|
2019-10-09 16:17:02 +02:00
|
|
|
this.props.loginHandler(this.state.username, this.state.password);
|
2019-08-13 08:23:37 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handleUsernameChange = (value: string) => {
|
2019-10-19 16:38:07 +02:00
|
|
|
this.setState({
|
|
|
|
|
username: value,
|
|
|
|
|
});
|
2019-08-13 08:23:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handlePasswordChange = (value: string) => {
|
2019-10-19 16:38:07 +02:00
|
|
|
this.setState({
|
|
|
|
|
password: value,
|
|
|
|
|
});
|
2019-08-13 08:23:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
isValid() {
|
|
|
|
|
return this.state.username && this.state.password;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
areCredentialsInvalid() {
|
|
|
|
|
const { t, error } = this.props;
|
|
|
|
|
if (error instanceof UnauthorizedError) {
|
2019-10-19 16:38:07 +02:00
|
|
|
return new Error(t('errorNotification.wrongLoginCredentials'));
|
2019-08-13 08:23:37 +02:00
|
|
|
} else {
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
2019-10-09 16:17:02 +02:00
|
|
|
const { loading, t } = this.props;
|
2019-08-13 08:23:37 +02:00
|
|
|
return (
|
|
|
|
|
<div className="column is-4 box has-text-centered has-background-white-ter">
|
2019-10-19 16:38:07 +02:00
|
|
|
<h3 className="title">{t('login.title')}</h3>
|
|
|
|
|
<p className="subtitle">{t('login.subtitle')}</p>
|
2019-10-09 16:17:02 +02:00
|
|
|
<TopMarginBox className="box">
|
|
|
|
|
<AvatarWrapper>
|
2019-10-19 16:38:07 +02:00
|
|
|
<AvatarImage src="/images/blib.jpg" alt={t('login.logo-alt')} />
|
2019-10-09 16:17:02 +02:00
|
|
|
</AvatarWrapper>
|
|
|
|
|
<ErrorNotification error={this.areCredentialsInvalid()} />
|
2019-08-13 08:23:37 +02:00
|
|
|
<form onSubmit={this.handleSubmit}>
|
|
|
|
|
<InputField
|
2019-10-19 16:38:07 +02:00
|
|
|
placeholder={t('login.username-placeholder')}
|
2019-08-13 08:23:37 +02:00
|
|
|
autofocus={true}
|
|
|
|
|
onChange={this.handleUsernameChange}
|
|
|
|
|
/>
|
|
|
|
|
<InputField
|
2019-10-19 16:38:07 +02:00
|
|
|
placeholder={t('login.password-placeholder')}
|
2019-08-13 08:23:37 +02:00
|
|
|
type="password"
|
|
|
|
|
onChange={this.handlePasswordChange}
|
|
|
|
|
/>
|
|
|
|
|
<SubmitButton
|
2019-10-19 16:38:07 +02:00
|
|
|
label={t('login.submit')}
|
2019-08-13 08:23:37 +02:00
|
|
|
fullWidth={true}
|
|
|
|
|
loading={loading}
|
|
|
|
|
/>
|
|
|
|
|
</form>
|
2019-10-09 16:17:02 +02:00
|
|
|
</TopMarginBox>
|
2019-08-13 08:23:37 +02:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
export default translate('commons')(LoginForm);
|