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 } 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 [username, setUsername] = useState('');
51 const [password, setPassword] = useState('');
52 const [isButtonDisabled, setIsButtonDisabled] = useState(true);
53 const [isSubmitting, setSubmitting] = useState(false);
54 const [helperText, setHelperText] = useState('');
55 const [error, setError] = useState(false);
60 if (username.trim() && password.trim()) {
61 setIsButtonDisabled(false);
63 setIsButtonDisabled(true);
65 }, [username, password]);
67 const handleLogin = () => {
69 handleSubmit(username, password)
72 console.log("LOGIN SUCESSFUL: ", response);
77 console.log("ERROR: ", err.response);
78 setHelperText(`${err.response && err.response.data && err.response.data.errors[0] || 'Error logging in: '+err}`);
83 const handleKeyPress = (e: any) => {
84 if (e.keyCode === 13 || e.which === 13) {
85 if (!isButtonDisabled) {
93 <form className={classes.root} noValidate autoComplete="off">
94 <Card className={classes.card}>
95 <div className={classes.wrapper}>
99 disabled={isSubmitting}
100 error={error} fullWidth id="username" type="email"
101 label="Username" margin="normal"
102 onChange={(e) => setUsername(e.target.value)}
103 onKeyPress={(e) => handleKeyPress(e)}
106 disabled={isSubmitting}
107 error={error} fullWidth id="password" type="password"
108 label="Password" margin="normal"
109 helperText={helperText}
110 onChange={(e) => setPassword(e.target.value)}
111 onKeyPress={(e) => handleKeyPress(e)}
116 <Button variant="contained" size="large" color="primary"
117 className={classes.loginBtn} onClick={() => handleLogin()}
118 disabled={isSubmitting || isButtonDisabled}>
122 { isSubmitting && <CircularProgress color='secondary' className={classes.progress} />}