Files
SCM-Manager/scm-ui/src/containers/Login.js

168 lines
4.1 KiB
JavaScript
Raw Normal View History

2018-07-02 16:05:58 +02:00
//@flow
2018-07-05 16:48:56 +02:00
import React from "react";
2018-07-13 10:57:11 +02:00
import { Redirect, withRouter } from "react-router-dom";
2018-07-05 16:48:56 +02:00
import injectSheet from "react-jss";
import { translate } from "react-i18next";
import {
login,
isAuthenticated,
isLoginPending,
getLoginFailure
} from "../modules/auth";
2018-07-05 16:48:56 +02:00
import { connect } from "react-redux";
2018-07-02 16:05:58 +02:00
2018-07-25 14:29:43 +02:00
import { InputField } from "../components/forms";
import { SubmitButton } from "../components/buttons";
2018-07-11 14:59:01 +02:00
import classNames from "classnames";
import Avatar from "../images/blib.jpg";
2018-07-11 21:01:29 +02:00
import ErrorNotification from "../components/ErrorNotification";
2018-07-11 14:59:01 +02:00
2018-07-02 16:05:58 +02:00
const styles = {
2018-07-11 14:59:01 +02:00
avatar: {
marginTop: "-70px",
paddingBottom: "20px"
},
avatarImage: {
border: "1px solid lightgray",
padding: "5px",
background: "#fff",
borderRadius: "50%",
width: "128px",
height: "128px"
2018-07-02 16:05:58 +02:00
},
2018-07-11 14:59:01 +02:00
avatarSpacing: {
marginTop: "5rem"
2018-07-02 16:05:58 +02:00
}
};
2018-07-10 16:52:23 +02:00
type Props = {
authenticated: boolean,
loading: boolean,
error: Error,
2018-07-13 10:57:11 +02:00
// dispatcher props
login: (username: string, password: string) => void,
// context props
t: string => string,
2018-07-10 16:52:23 +02:00
classes: any,
2018-07-13 10:57:11 +02:00
from: any,
location: any
2018-07-10 16:52:23 +02:00
};
type State = {
username: string,
password: string
};
class Login extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = { username: "", password: "" };
}
2018-07-11 14:59:01 +02:00
handleUsernameChange = (value: string) => {
this.setState({ username: value });
};
2018-07-05 16:48:56 +02:00
2018-07-11 14:59:01 +02:00
handlePasswordChange = (value: string) => {
this.setState({ password: value });
};
2018-07-05 16:48:56 +02:00
2018-07-11 21:01:29 +02:00
handleSubmit = (event: Event) => {
2018-07-05 16:48:56 +02:00
event.preventDefault();
2018-07-11 21:01:29 +02:00
if (this.isValid()) {
this.props.login(this.state.username, this.state.password);
}
};
isValid() {
return this.state.username && this.state.password;
}
isInValid() {
return !this.isValid();
2018-07-05 16:48:56 +02:00
}
2018-07-02 16:05:58 +02:00
2018-07-13 10:57:11 +02:00
renderRedirect = () => {
const { from } = this.props.location.state || { from: { pathname: "/" } };
return <Redirect to={from} />;
};
2018-07-02 16:05:58 +02:00
render() {
const { authenticated, loading, error, t, classes } = this.props;
2018-07-13 10:57:11 +02:00
if (authenticated) {
return this.renderRedirect();
}
2018-07-02 16:05:58 +02:00
return (
2018-07-31 16:32:16 +02:00
<section className="hero">
2018-07-11 21:01:29 +02:00
<div className="hero-body">
2018-07-11 14:59:01 +02:00
<div className="container has-text-centered">
<div className="column is-4 is-offset-4">
<h3 className="title">{t("login.title")}</h3>
<p className="subtitle">{t("login.subtitle")}</p>
2018-07-11 14:59:01 +02:00
<div className={classNames("box", classes.avatarSpacing)}>
<figure className={classes.avatar}>
<img
className={classes.avatarImage}
src={Avatar}
alt={t("login.logo-alt")}
2018-07-11 14:59:01 +02:00
/>
</figure>
2018-07-11 21:01:29 +02:00
<ErrorNotification error={error} />
<form onSubmit={this.handleSubmit}>
2018-07-11 14:59:01 +02:00
<InputField
placeholder={t("login.username-placeholder")}
2018-07-11 21:01:29 +02:00
autofocus={true}
2018-07-11 14:59:01 +02:00
onChange={this.handleUsernameChange}
/>
<InputField
placeholder={t("login.password-placeholder")}
2018-07-11 14:59:01 +02:00
type="password"
onChange={this.handlePasswordChange}
/>
2018-07-11 21:01:29 +02:00
<SubmitButton
label={t("login.submit")}
2018-07-11 21:01:29 +02:00
disabled={this.isInValid()}
fullWidth={true}
2018-07-19 12:05:50 +02:00
loading={loading}
2018-07-11 21:01:29 +02:00
/>
2018-07-11 14:59:01 +02:00
</form>
</div>
</div>
</div>
2018-07-02 16:05:58 +02:00
</div>
2018-07-11 14:59:01 +02:00
</section>
2018-07-02 16:05:58 +02:00
);
}
}
2018-07-05 16:48:56 +02:00
const mapStateToProps = state => {
const authenticated = isAuthenticated(state);
const loading = isLoginPending(state);
const error = getLoginFailure(state);
return {
authenticated,
loading,
error
};
2018-07-05 16:48:56 +02:00
};
const mapDispatchToProps = dispatch => {
return {
login: (username: string, password: string) =>
dispatch(login(username, password))
};
};
const StyledLogin = injectSheet(styles)(
connect(
mapStateToProps,
mapDispatchToProps
)(translate("commons")(Login))
2018-07-05 16:48:56 +02:00
);
2018-07-13 10:57:11 +02:00
export default withRouter(StyledLogin);