aboutsummaryrefslogtreecommitdiff
path: root/modern/src/LoginPage.js
blob: f029cbc39d87ef8fc0f7e566b89b1d6c662193bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import React, { useState } from 'react';
import { useDispatch } from 'react-redux';
import { useHistory } from 'react-router-dom';
import { sessionActions } from './store';
import { Grid, useMediaQuery, makeStyles, InputLabel, Select, MenuItem, FormControl, Button, TextField, Paper } from '@material-ui/core';
import { useTheme } from '@material-ui/core/styles';
import RegisterDialog from './RegisterDialog';
import { useSelector } from 'react-redux';

import t from './common/localization';
import Logo from './Logo';

const useStyles = makeStyles(theme => ({
  root: {
    display: 'flex',
    height: '100vh',
  },
  sidebar: {
    display: 'flex',
    justifyContent: 'center',
    alignItems: 'center',
    background: theme.palette.common.purple,
    paddingBottom: theme.spacing(5),
    width: theme.dimensions.sidebarWidth,
    [theme.breakpoints.down('md')]: {
      width: theme.dimensions.tabletSidebarWidth,
    },
    [theme.breakpoints.down('xs')]: {
      width: '0px',
    },
  },
  paper: {
    display:'flex',
    flexDirection: 'column',
    justifyContent: 'center',
    alignItems: 'center',
    flex: 1,
    boxShadow: '-2px 0px 16px rgba(0, 0, 0, 0.25)',
    [theme.breakpoints.up('lg')]: {
      padding: theme.spacing(0, 25, 0, 0)
    },
  },
  form: {
    maxWidth: theme.spacing(52),
    padding: theme.spacing(5),
    width: "100%",
  },
  logo: {
    textAlign: 'center',
  },
}));

const LoginPage = () => {
  const classes = useStyles();
  const theme = useTheme();

  const dispatch = useDispatch();
  const history = useHistory();

  const [failed, setFailed] = useState(false);
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');
  const [registerDialogShown, setRegisterDialogShown] = useState(false);
  const registrationEnabled =  useSelector(state => state.session.server ? state.session.server['registration'] : false);
  const matches = useMediaQuery(theme.breakpoints.down('md'));

  const handleEmailChange = (event) => {
    setEmail(event.target.value);
  }

  const handlePasswordChange = (event) => {
    setPassword(event.target.value);
  }

  const handleRegister = () => {
    setRegisterDialogShown(true);
  }

  const handleRegisterResult = () => {
    setRegisterDialogShown(false);
  }

  const handleLogin = async (event) => {
    event.preventDefault();
    const response = await fetch('/api/session', { method: 'POST', body: new URLSearchParams(`email=${email}&password=${password}`) });
    if (response.ok) {
      const user = await response.json();
      dispatch(sessionActions.updateUser(user));
      history.push('/');
    } else {
      setFailed(true);
      setPassword('');
    }
  }

  return (
    <main className={classes.root}>
      <div className={classes.sidebar}>
        {!matches && <Logo fill='#fff'/> }
      </div>
      <Paper className={classes.paper}>
        <form className={classes.form} onSubmit={handleLogin}>
          <Grid container direction='column' spacing={3}>
            <Grid item className={classes.logo}>
              {matches && <Logo fill='#333366'/>}
            </Grid>
            <Grid item>
              <TextField
                required
                fullWidth
                error={failed}
                label={t('userEmail')}
                name='email'
                value={email}
                autoComplete='email'
                autoFocus
                onChange={handleEmailChange}
                helperText={failed && 'Invalid username or password'}
                variant='filled' />
            </Grid>
            <Grid item>
              <TextField
                required
                fullWidth
                error={failed}
                label={t('userPassword')}
                name='password'
                value={password}
                type='password'
                autoComplete='current-password'
                onChange={handlePasswordChange}
                variant='filled' />
            </Grid>
            <Grid item>
              <Button 
                type='submit'
                variant='contained' 
                color='secondary'
                disabled={!email || !password}
                fullWidth>
                {t('loginLogin')}
              </Button>
            </Grid>
            <Grid item container>
              <Grid item>
                <Button onClick={handleRegister} disabled={!registrationEnabled} color="secondary">
                  {t('loginRegister')}
                </Button>
              </Grid>
              <Grid item xs>
                <FormControl variant="filled" fullWidth>
                  <InputLabel>{t('loginLanguage')}</InputLabel>
                  <Select>
                    <MenuItem value="en">English</MenuItem>
                  </Select>
                </FormControl>
              </Grid>
            </Grid>
            <Grid container justify="flex-end">
              <Grid item>
                <Button color="primary">
                  {t('loginReset')}
                </Button>              
              </Grid>
            </Grid>            
          </Grid>
        </form>
      </Paper>
    </main>
  )
}
export default LoginPage;