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

100 lines
2.2 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";
import {
login,
isAuthenticated,
isLoginPending,
getLoginFailure
} from "../modules/auth";
2018-07-05 16:48:56 +02:00
import { connect } from "react-redux";
import { getLoginLink } from "../modules/indexResource";
2019-08-13 08:23:37 +02:00
import LoginForm from "../components/LoginForm";
import LoginInfo from "../components/LoginInfo";
import classNames from "classnames";
import injectSheet from "react-jss";
2018-07-11 14:59:01 +02:00
2018-07-02 16:05:58 +02:00
const styles = {
2019-08-13 08:23:37 +02:00
section: {
paddingTop: "2em"
2018-07-02 16:05:58 +02:00
}
};
2018-07-10 16:52:23 +02:00
type Props = {
authenticated: boolean,
loading: boolean,
2019-08-13 08:23:37 +02:00
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,
// context props
2018-07-10 16:52:23 +02:00
classes: any,
2019-08-13 08:23:37 +02:00
t: string => string,
2018-07-13 10:57:11 +02:00
from: any,
location: any
2018-07-10 16:52:23 +02:00
};
2019-08-13 08:23:37 +02:00
class Login extends React.Component<Props> {
2018-07-05 16:48:56 +02:00
2019-08-13 08:23:37 +02:00
login = (username: string, password: string) => {
const { link, login } = this.props;
login(link, username, password);
2018-07-11 14:59:01 +02:00
};
2018-07-05 16:48:56 +02:00
2018-07-13 10:57:11 +02:00
renderRedirect = () => {
const { from } = this.props.location.state || { from: { pathname: "/" } };
2019-08-13 08:23:37 +02:00
return <Redirect to={from}/>;
2018-07-13 10:57:11 +02:00
};
2018-07-02 16:05:58 +02:00
render() {
2019-08-13 08:23:37 +02:00
const { authenticated, loading, error, 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 (
2019-08-13 08:23:37 +02:00
<section className={classNames("hero", classes.section )}>
2018-07-11 21:01:29 +02:00
<div className="hero-body">
2019-08-13 08:23:37 +02:00
<div className="container">
<div className="columns">
<LoginForm loading={loading} error={error} login={this.login}/>
<LoginInfo/>
2018-07-11 14:59:01 +02:00
</div>
</div>
2018-07-02 16:05:58 +02:00
</div>
2018-07-11 14:59:01 +02:00
</section>
2019-08-13 08:23:37 +02:00
);
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 11:52:05 +02:00
login: (loginLink: string, username: string, password: string) =>
dispatch(login(loginLink, username, password))
2018-07-05 16:48:56 +02:00
};
};
const StyledLogin = injectSheet(styles)(
connect(
mapStateToProps,
mapDispatchToProps
2019-08-13 08:23:37 +02:00
)(Login)
2018-07-05 16:48:56 +02:00
);
2018-07-13 10:57:11 +02:00
export default withRouter(StyledLogin);