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

137 lines
3.3 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";
import injectSheet from "react-jss";
import { login } 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-11 14:59:01 +02:00
import InputField from "../components/InputField";
import SubmitButton from "../components/SubmitButton";
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 = {
2018-07-11 21:01:29 +02:00
loading: boolean,
error: Error,
2018-07-10 16:52:23 +02:00
classes: any,
login: (username: string, password: string) => void
};
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
render() {
2018-07-11 21:01:29 +02:00
const { classes, loading, error } = this.props;
2018-07-02 16:05:58 +02:00
return (
2018-07-11 21:01:29 +02:00
<section className="hero has-background-light">
<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">Login</h3>
<p className="subtitle">Please login to proceed.</p>
<div className={classNames("box", classes.avatarSpacing)}>
<figure className={classes.avatar}>
<img
className={classes.avatarImage}
src={Avatar}
alt="SCM-Manager"
/>
</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="Your Username"
2018-07-11 21:01:29 +02:00
autofocus={true}
2018-07-11 14:59:01 +02:00
onChange={this.handleUsernameChange}
/>
<InputField
placeholder="Your Password"
type="password"
onChange={this.handlePasswordChange}
/>
2018-07-11 21:01:29 +02:00
<SubmitButton
value="Login"
disabled={this.isInValid()}
fullWidth={true}
isLoading={loading}
/>
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 => {
2018-07-11 21:01:29 +02:00
return state.login || {};
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
)(Login)
);
export default StyledLogin;