diff options
author | Eugene Sokolov <eug-vs@keemail.me> | 2020-08-08 11:28:07 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-08 11:28:07 +0300 |
commit | 70d533d2bcbaa689d3de6ecb532997cd68a7a842 (patch) | |
tree | 0199ab8a91f3e1bd5df0865c10695dde20a5303f /src/pages/LoginPage | |
parent | 84eaed2f29ac370eea7c4a7ded6fb3d4661c9679 (diff) | |
parent | 104c658fc411536e09931191721411de448f964f (diff) | |
download | which-ui-70d533d2bcbaa689d3de6ecb532997cd68a7a842.tar.gz |
Merge pull request #72 from which-ecosystem/feat/routing
Add basic routing
Diffstat (limited to 'src/pages/LoginPage')
-rw-r--r-- | src/pages/LoginPage/LoginPage.tsx | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/src/pages/LoginPage/LoginPage.tsx b/src/pages/LoginPage/LoginPage.tsx new file mode 100644 index 0000000..335cbb1 --- /dev/null +++ b/src/pages/LoginPage/LoginPage.tsx @@ -0,0 +1,103 @@ +import React, { useState, useRef } from 'react'; +import { useHistory } from 'react-router-dom'; +import { makeStyles } from '@material-ui/core/styles'; +import { + TextField, + Button, + FormControlLabel, + Switch +} from '@material-ui/core'; +import { useAuth } from '../../hooks/useAuth'; + +const useStyles = makeStyles(theme => ({ + root: { + '& > *': { + margin: theme.spacing(1), + width: theme.spacing(35) + }, + display: 'flex', + flexDirection: 'column', + alignItems: 'center', + textAlign: 'center' + }, + formHeader: { + textAlign: 'center', + fontSize: 25 + }, + formTransfer: { + display: 'flex', + justifyContent: 'center' + }, + transferButton: { + marginLeft: 10, + color: 'green', + cursor: 'pointer' + } +})); + +const LoginPage: React.FC = () => { + const [error, setError] = useState<boolean>(false); + const [remember, setRemember] = useState<boolean>(true); + const classes = useStyles(); + const nameRef = useRef<HTMLInputElement>(); + const passwordRef = useRef<HTMLInputElement>(); + const { login } = useAuth(); + const history = useHistory(); + + const handleCheck = () => { + setRemember(!remember); + }; + + const handleSubmit = async () => { + const name = nameRef.current?.value?.toLowerCase(); + const password = passwordRef.current?.value; + if (name && password) { + login(name, password, remember).then(success => { + if (success) history.push(`/profile/${name}`); + else setError(true); + }); + } + }; + + const handleRegistration = () => { + history.push('/registration'); + }; + + return ( + <> + <div className={classes.formHeader}>Sign In</div> + <form className={classes.root} noValidate autoComplete="off"> + <TextField + inputRef={nameRef} + error={error} + label="Login" + /> + <TextField + inputRef={passwordRef} + error={error} + helperText={error && 'Invalid credentials'} + label="Password" + type="password" + /> + <FormControlLabel + control={<Switch color="primary" onClick={handleCheck} checked={remember} size="small" />} + label="Remember me" + /> + <Button variant="contained" onClick={handleSubmit}>submit</Button> + </form> + <div className={classes.formTransfer}> + <div>{'Don\'t have an account?'}</div> + <span + onClick={handleRegistration} + className={classes.transferButton} + role="presentation" + > + Sign up + </span> + </div> + </> + ); +}; + +export default LoginPage; + |