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

98 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";
2019-10-17 12:08:39 +02:00
import { connect } from "react-redux";
import { Redirect, withRouter } from "react-router-dom";
import { compose } from "redux";
import { translate } from "react-i18next";
import styled from "styled-components";
2019-10-17 12:08:39 +02:00
import {
getLoginFailure,
isAuthenticated,
isLoginPending,
login
} from "../modules/auth";
import { getLoginInfoLink, getLoginLink } from "../modules/indexResource";
2019-08-13 08:23:37 +02:00
import LoginInfo from "../components/LoginInfo";
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,
loginInfoLink?: 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
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
};
const HeroSection = styled.section`
padding-top: 2em;
`;
2018-07-05 16:48:56 +02:00
2019-08-13 08:23:37 +02:00
class Login extends React.Component<Props> {
handleLogin = (username: string, password: string): void => {
2019-08-13 08:23:37 +02:00
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: "/" } };
return <Redirect to={from} />;
2018-07-13 10:57:11 +02:00
};
2018-07-02 16:05:58 +02:00
render() {
const { authenticated, ...restProps } = this.props;
2018-07-13 10:57:11 +02:00
if (authenticated) {
return this.renderRedirect();
}
2018-07-02 16:05:58 +02:00
return (
<HeroSection className="hero">
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 is-centered">
<LoginInfo loginHandler={this.handleLogin} {...restProps} />
2018-07-11 14:59:01 +02:00
</div>
</div>
2018-07-02 16:05:58 +02:00
</div>
</HeroSection>
);
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);
const loginInfoLink = getLoginInfoLink(state);
return {
authenticated,
loading,
2018-10-11 08:30:37 +02:00
error,
link,
loginInfoLink
};
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
};
};
export default compose(
withRouter,
2018-07-05 16:48:56 +02:00
connect(
mapStateToProps,
mapDispatchToProps
)
)(Login);