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

188 lines
4.4 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
import {
InputField,
SubmitButton,
ErrorNotification,
Image
} from "@scm-manager/ui-components";
2018-07-11 14:59:01 +02:00
import classNames from "classnames";
2018-10-11 09:54:12 +02:00
import {
fetchIndexResources,
getLoginLink,
getMeLink
} from "../modules/indexResource";
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-10-11 09:54:12 +02:00
link: string,
2018-07-13 10:57:11 +02:00
// dispatcher props
2018-10-11 08:30:37 +02:00
login: (link: string, username: string, password: string) => void,
2018-10-09 16:17:25 +02:00
fetchIndexResources: () => 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()) {
2018-10-11 08:30:37 +02:00
this.props.login(
2018-10-11 09:54:12 +02:00
this.props.link,
2018-10-11 08:30:37 +02:00
this.state.username,
this.state.password
);
2018-07-11 21:01:29 +02:00
}
};
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 = () => {
2018-10-11 10:26:54 +02:00
this.props.fetchIndexResources();
2018-07-13 10:57:11 +02:00
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}>
2018-08-27 15:47:02 +02:00
<Image
2018-07-11 14:59:01 +02:00
className={classes.avatarImage}
src="/images/blib.jpg"
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);
2018-10-11 09:54:12 +02:00
const link = getLoginLink(state);
return {
authenticated,
loading,
2018-10-11 08:30:37 +02:00
error,
2018-10-11 09:54:12 +02:00
link
};
2018-07-05 16:48:56 +02:00
};
const mapDispatchToProps = dispatch => {
return {
2018-10-11 10:26:54 +02:00
login: (
loginLink: string,
meLink: string,
username: string,
password: string
) => dispatch(login(loginLink, meLink, username, password)),
fetchIndexResources: () => dispatch(fetchIndexResources())
2018-07-05 16:48:56 +02:00
};
};
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);