1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from 'react';
6 import { useState, useEffect, useRef } from 'react';
7 import { withStyles, WithStyles, StyleRulesCallback } from '@material-ui/core/styles';
8 import CircularProgress from '@material-ui/core/CircularProgress';
9 import { Button, Card, CardContent, TextField, CardActions } from '@material-ui/core';
10 import { green } from '@material-ui/core/colors';
11 import { AxiosPromise } from 'axios';
13 type CssRules = 'root' | 'loginBtn' | 'card' | 'wrapper' | 'progress';
15 const styles: StyleRulesCallback<CssRules> = theme => ({
20 margin: `${theme.spacing.unit} auto`
23 marginTop: theme.spacing.unit,
27 marginTop: theme.spacing.unit,
31 margin: theme.spacing.unit,
44 interface LoginFormProps {
45 handleSubmit: (username: string, password: string) => AxiosPromise;
48 export const LoginForm = withStyles(styles)(
49 ({ handleSubmit, classes }: LoginFormProps & WithStyles<CssRules>) => {
50 const userInput = useRef<HTMLInputElement>(null);
51 const [username, setUsername] = useState('');
52 const [password, setPassword] = useState('');
53 const [isButtonDisabled, setIsButtonDisabled] = useState(true);
54 const [isSubmitting, setSubmitting] = useState(false);
55 const [helperText, setHelperText] = useState('');
56 const [error, setError] = useState(false);
61 if (username.trim() && password.trim()) {
62 setIsButtonDisabled(false);
64 setIsButtonDisabled(true);
66 }, [username, password]);
68 // This only run once after render.
70 userInput.current!.focus();
73 const handleLogin = () => {
75 handleSubmit(username, password)
78 console.log("LOGIN SUCESSFUL: ", response);
83 console.log("ERROR: ", err.response);
84 setHelperText(`${err.response && err.response.data && err.response.data.errors[0] || 'Error logging in: '+err}`);
89 const handleKeyPress = (e: any) => {
90 if (e.keyCode === 13 || e.which === 13) {
91 if (!isButtonDisabled) {
99 <form className={classes.root} noValidate autoComplete="off">
100 <Card className={classes.card}>
101 <div className={classes.wrapper}>
106 disabled={isSubmitting}
107 error={error} fullWidth id="username" type="email"
108 label="Username" margin="normal"
109 onChange={(e) => setUsername(e.target.value)}
110 onKeyPress={(e) => handleKeyPress(e)}
113 disabled={isSubmitting}
114 error={error} fullWidth id="password" type="password"
115 label="Password" margin="normal"
116 helperText={helperText}
117 onChange={(e) => setPassword(e.target.value)}
118 onKeyPress={(e) => handleKeyPress(e)}
123 <Button variant="contained" size="large" color="primary"
124 className={classes.loginBtn} onClick={() => handleLogin()}
125 disabled={isSubmitting || isButtonDisabled}>
129 { isSubmitting && <CircularProgress color='secondary' className={classes.progress} />}