-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Login component is a Dialog to input username and password. It receive three properties: - `display`: set `open` in Dialog to show or hide the component. - `hideDialog`: set `display` from parent. - `login`: login user. TODO: create a universal notification dialog.
- Loading branch information
1 parent
1cec091
commit 098fbff
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import React, { useState } from 'react'; | ||
import { | ||
useTheme, useMediaQuery, Dialog, DialogTitle, DialogContent, TextField, DialogActions, Button, | ||
} from '@material-ui/core'; | ||
import { LoginUser } from '../types'; | ||
|
||
type Props = { display: boolean, hideDialog: () => void, login: (arg: LoginUser) => Promise<void> }; | ||
|
||
const Login = ({ display, hideDialog, login }: Props) => { | ||
const [username, setUsername] = useState(''); | ||
const [password, setPassword] = useState(''); | ||
|
||
const theme = useTheme(); | ||
const fullScreen = useMediaQuery(theme.breakpoints.down('sm')); | ||
|
||
const handleExit = () => { | ||
setUsername(''); | ||
setPassword(''); | ||
hideDialog(); | ||
}; | ||
|
||
const onSubmit = (event: React.SyntheticEvent) => { | ||
event.preventDefault(); | ||
login({ username, password }) | ||
.then(() => handleExit()) | ||
.catch((e) => { | ||
if (e.response && e.response.status === 401) { | ||
// TODO: show a dialog to user | ||
console.warn('User input a wrong username or password'); | ||
} else { | ||
// TODO: ask user to report a bug | ||
console.error(e.message); | ||
} | ||
}); | ||
}; | ||
|
||
return ( | ||
<Dialog | ||
fullScreen={fullScreen} | ||
fullWidth | ||
maxWidth="md" | ||
open={display} | ||
aria-labelledby="login-dialog-title" | ||
> | ||
<DialogTitle id="login-dialog-title">登入</DialogTitle> | ||
<form onSubmit={onSubmit}> | ||
<DialogContent> | ||
<TextField | ||
color="primary" | ||
fullWidth | ||
label="用户名" | ||
value={username} | ||
onChange={({ target }) => setUsername(target.value)} | ||
/> | ||
<TextField | ||
color="primary" | ||
fullWidth | ||
label="密码" | ||
type="password" | ||
value={password} | ||
onChange={({ target }) => setPassword(target.value)} | ||
/> | ||
</DialogContent> | ||
<DialogActions> | ||
<Button | ||
onClick={handleExit} | ||
> | ||
取消 | ||
</Button> | ||
<Button | ||
color="secondary" | ||
type="submit" | ||
> | ||
登录 | ||
</Button> | ||
</DialogActions> | ||
</form> | ||
</Dialog> | ||
); | ||
}; | ||
|
||
export default Login; |