2019-08-13 08:23:37 +02:00
|
|
|
//@flow
|
|
|
|
|
import React from "react";
|
|
|
|
|
import { translate } from "react-i18next";
|
2019-10-09 16:17:02 +02:00
|
|
|
import styled from "styled-components";
|
|
|
|
|
import {
|
|
|
|
|
Image,
|
|
|
|
|
ErrorNotification,
|
|
|
|
|
InputField,
|
|
|
|
|
SubmitButton,
|
|
|
|
|
UnauthorizedError
|
|
|
|
|
} from "@scm-manager/ui-components";
|
2019-08-13 08:23:37 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
error?: Error,
|
|
|
|
|
loading: boolean,
|
2019-08-15 10:08:48 +02:00
|
|
|
loginHandler: (username: string, password: string) => void,
|
2019-08-13 08:23:37 +02:00
|
|
|
|
|
|
|
|
// context props
|
2019-10-09 16:17:02 +02:00
|
|
|
t: string => string
|
2019-08-13 08:23:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type State = {
|
|
|
|
|
username: string,
|
|
|
|
|
password: string
|
|
|
|
|
};
|
|
|
|
|
|
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);
|
|
|
|
|
this.state = { username: "", password: "" };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) => {
|
|
|
|
|
this.setState({ username: value });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handlePasswordChange = (value: string) => {
|
|
|
|
|
this.setState({ password: value });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
isValid() {
|
|
|
|
|
return this.state.username && this.state.password;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
areCredentialsInvalid() {
|
|
|
|
|
const { t, error } = this.props;
|
|
|
|
|
if (error instanceof UnauthorizedError) {
|
|
|
|
|
return new Error(t("errorNotification.wrongLoginCredentials"));
|
|
|
|
|
} 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">
|
|
|
|
|
<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>
|
|
|
|
|
<AvatarImage src="/images/blib.jpg" alt={t("login.logo-alt")} />
|
|
|
|
|
</AvatarWrapper>
|
|
|
|
|
<ErrorNotification error={this.areCredentialsInvalid()} />
|
2019-08-13 08:23:37 +02:00
|
|
|
<form onSubmit={this.handleSubmit}>
|
|
|
|
|
<InputField
|
|
|
|
|
placeholder={t("login.username-placeholder")}
|
|
|
|
|
autofocus={true}
|
|
|
|
|
onChange={this.handleUsernameChange}
|
|
|
|
|
/>
|
|
|
|
|
<InputField
|
|
|
|
|
placeholder={t("login.password-placeholder")}
|
|
|
|
|
type="password"
|
|
|
|
|
onChange={this.handlePasswordChange}
|
|
|
|
|
/>
|
|
|
|
|
<SubmitButton
|
|
|
|
|
label={t("login.submit")}
|
|
|
|
|
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-09 16:17:02 +02:00
|
|
|
export default translate("commons")(LoginForm);
|