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"; import { withRouter } from "react-router-dom"; import { IUser } from "../models/user"; import { IResponse } from "../models/server"; interface IProps { login: (username: string, password: string) => Promise; authenticated: boolean; history: any; setLoading: (state: boolean) => void; setSnackbar: (state: boolean, msg: string) => void; loading: boolean; snackOpen: boolean; snackMsg: string; } const LoginPageWithRouter = withRouter( class LoginPage extends React.Component { private usernameRef: any = undefined; private passwordRef: any = undefined; performLogin = () => { this.props.setLoading(true); const username = this.usernameRef.value || ""; const password = this.passwordRef.value || ""; this.props.login(username, password).then((res: IUser) => { // Stop the loading animation this.props.setLoading(false); if (res.showWelcome) { // If the user logs in for the first time, a welcome // screen should be shown this.props.history.push("/welcome"); } else { this.props.history.push("/dashboard"); } }, (err: IResponse) => { this.props.setLoading(false); this.props.setSnackbar(true, "Failed to log in"); }); } componentDidMount() { // If we're already authenticated, we can skip the login page if (this.props.authenticated) { this.props.history.push("/dashboard"); } } render() { return
Login this.usernameRef = node} /> this.passwordRef = node} /> { this.props.loading ? ( ) : undefined } this.props.setSnackbar(false, "")} message={this.props.snackMsg} autoHideDuration={6000} />
; } } ); export default LoginPageWithRouter;