import React, { useState } from 'react'; import { useDispatch } from 'react-redux'; import { useHistory } from 'react-router-dom'; import { sessionActions } from './store'; import Button from '@material-ui/core/Button'; import FormHelperText from '@material-ui/core/FormHelperText'; import FormControl from '@material-ui/core/FormControl'; import Input from '@material-ui/core/Input'; import InputLabel from '@material-ui/core/InputLabel'; import Paper from '@material-ui/core/Paper'; import { makeStyles } from '@material-ui/core'; import t from './common/localization'; const useStyles = makeStyles(theme => ({ root: { width: 'auto', display: 'block', // Fix IE11 issue. marginLeft: theme.spacing(3), marginRight: theme.spacing(3), [theme.breakpoints.up(400 + theme.spacing(3 * 2))]: { width: 400, marginLeft: 'auto', marginRight: 'auto', }, }, paper: { marginTop: theme.spacing(8), display: 'flex', flexDirection: 'column', alignItems: 'center', padding: `${theme.spacing(3)}px`, }, logo: { margin: `${theme.spacing(2)}px 0 ${theme.spacing(1)}px` }, buttons: { width: '100%', display: 'flex', flexDirection: 'row' }, button: { flex: '1 1 0', margin: `${theme.spacing(3)}px ${theme.spacing(1)}px 0` }, })); const LoginPage = () => { const dispatch = useDispatch(); const [failed, setFailed] = useState(false); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const classes = useStyles(); const history = useHistory(); const handleEmailChange = (event) => { setEmail(event.target.value); } const handlePasswordChange = (event) => { setPassword(event.target.value); } const handleRegister = () => { // TODO: Implement registration } const handleLogin = (event) => { event.preventDefault(); fetch('/api/session', { method: 'POST', body: new URLSearchParams(`email=${email}&password=${password}`) }).then(response => { if (response.ok) { dispatch(sessionActions.authenticated(true)); history.push('/'); } else { setFailed(true); setPassword(''); } }); } return (
Traccar
{t('userEmail')} {failed && Invalid username or password} {t('userPassword')}
); } export default LoginPage;