2018-08-24 17:03:08 +00:00
|
|
|
import * as React from "react";
|
|
|
|
|
|
|
|
import Typography from "@material-ui/core/Typography";
|
|
|
|
import Paper from "@material-ui/core/Paper";
|
|
|
|
import TextField from "@material-ui/core/TextField";
|
|
|
|
import Grid from "@material-ui/core/Grid";
|
|
|
|
import Button from "@material-ui/core/Button";
|
|
|
|
import LinearProgress from "@material-ui/core/LinearProgress";
|
|
|
|
import Snackbar from "@material-ui/core/Snackbar";
|
|
|
|
|
2018-08-26 14:23:48 +00:00
|
|
|
import { Redirect } from "react-router-dom";
|
|
|
|
|
2018-09-16 15:16:24 +00:00
|
|
|
import { IUser } from "../models/user";
|
|
|
|
|
2018-08-24 17:03:08 +00:00
|
|
|
interface IProps {
|
2018-09-16 15:16:24 +00:00
|
|
|
login: (username: string, password: string) => Promise<IUser | {}>;
|
2018-08-26 14:23:48 +00:00
|
|
|
loggedIn: boolean;
|
2018-08-24 17:03:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface IState {
|
|
|
|
username: string;
|
|
|
|
password: string;
|
|
|
|
|
|
|
|
loading: boolean;
|
|
|
|
|
|
|
|
snack: string; // The message
|
|
|
|
open: boolean;
|
|
|
|
}
|
|
|
|
|
2018-09-16 15:16:24 +00:00
|
|
|
export default class LoginPage extends React.Component<IProps, IState> {
|
2018-08-24 17:03:08 +00:00
|
|
|
constructor(props: any) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
username: "",
|
|
|
|
password: "",
|
2018-09-16 15:16:24 +00:00
|
|
|
loading: false,
|
2018-08-24 17:03:08 +00:00
|
|
|
snack: "",
|
|
|
|
open: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.update = this.update.bind(this);
|
|
|
|
this.performLogin = this.performLogin.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
update(prop: string) {
|
|
|
|
return (event: any) => {
|
|
|
|
this.setState({
|
|
|
|
[prop]: event.target.value,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
performLogin() {
|
2018-09-16 15:16:24 +00:00
|
|
|
const load = (loading: boolean) => {
|
2018-08-24 17:03:08 +00:00
|
|
|
this.setState({
|
|
|
|
loading
|
|
|
|
});
|
|
|
|
}
|
2018-09-16 15:16:24 +00:00
|
|
|
const showSnackbar = (msg: string) => {
|
2018-08-24 17:03:08 +00:00
|
|
|
this.setState({
|
|
|
|
open: true,
|
|
|
|
snack: msg,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
load(true);
|
|
|
|
|
|
|
|
const { username, password } = this.state;
|
2018-09-16 15:16:24 +00:00
|
|
|
console.log(this.state);
|
|
|
|
this.props.login(username, password).then((res: IUser) => {
|
2018-09-06 18:22:41 +00:00
|
|
|
// Set the session key
|
2018-09-16 15:16:24 +00:00
|
|
|
window.sessionStorage.setItem("sessionToken", res.sessionToken);
|
2018-08-24 17:03:08 +00:00
|
|
|
}, (err) => {
|
|
|
|
load(false);
|
|
|
|
showSnackbar("Failed to log in");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const snackbarClose = () => {
|
|
|
|
this.setState({ open: false });
|
|
|
|
};
|
|
|
|
|
|
|
|
return <div>
|
|
|
|
<Grid
|
|
|
|
container
|
|
|
|
spacing={0}
|
|
|
|
direction="column"
|
|
|
|
alignItems="center"
|
|
|
|
justify="center"
|
|
|
|
style={{ minHeight: '100vh' }}>
|
|
|
|
<Grid item xs={12}>
|
|
|
|
<Paper className="paper">
|
|
|
|
<Typography variant="title">Login</Typography>
|
|
|
|
<Grid container direction="column" spacing={8}>
|
|
|
|
<Grid item>
|
|
|
|
<TextField
|
|
|
|
label="Username"
|
2018-09-16 15:16:24 +00:00
|
|
|
onChange={this.update("username")}
|
|
|
|
value={this.state.username} />
|
2018-08-24 17:03:08 +00:00
|
|
|
</Grid>
|
|
|
|
<Grid item>
|
|
|
|
<TextField
|
|
|
|
label="Passwort"
|
|
|
|
type="password"
|
2018-09-16 15:16:24 +00:00
|
|
|
value={this.state.password}
|
|
|
|
onChange={this.update("password")} />
|
2018-08-24 17:03:08 +00:00
|
|
|
</Grid>
|
|
|
|
<Grid item>
|
|
|
|
<Button
|
|
|
|
variant="contained"
|
|
|
|
color="primary"
|
|
|
|
className="login-btn"
|
|
|
|
onClick={() => this.performLogin()}>
|
|
|
|
Login
|
|
|
|
</Button>
|
|
|
|
{
|
|
|
|
this.state.loading ? (
|
|
|
|
<LinearProgress />
|
|
|
|
) : undefined
|
|
|
|
}
|
|
|
|
</Grid>
|
|
|
|
</Grid>
|
|
|
|
</Paper>
|
|
|
|
</Grid>
|
|
|
|
</Grid>
|
|
|
|
<Snackbar
|
|
|
|
open={this.state.open}
|
|
|
|
onClose={snackbarClose}
|
|
|
|
message={this.state.snack}
|
|
|
|
autoHideDuration={6000} />
|
2018-08-26 14:23:48 +00:00
|
|
|
{
|
|
|
|
this.props.loggedIn ? (
|
|
|
|
<Redirect to="/dashboard" />
|
|
|
|
) : undefined
|
|
|
|
}
|
2018-08-24 17:03:08 +00:00
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
};
|