aboutsummaryrefslogtreecommitdiff
path: root/modern/src/RegisterDialog.js
blob: e1483bc20f0a54f8514c0df4b67621083ba31707 (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
import React, { useState } from 'react';
import Button from '@material-ui/core/Button';
import Dialog from '@material-ui/core/Dialog';
import DialogActions from '@material-ui/core/DialogActions';
import DialogContent from '@material-ui/core/DialogContent';
import DialogContentText from '@material-ui/core/DialogContentText';
import TextField from '@material-ui/core/TextField';
import Snackbar from '@material-ui/core/Snackbar';
import t from './LocalizationProvider';

const RegisterDialog = ({ showDialog, onResult }) => {
  const [name, setName] = useState('');
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');
  const [snackbarOpen, setSnackbarOpen] = useState(false);

  const submitDisabled = () => !name || !/(.+)@(.+)\.(.{2,})/.test(email) || !password;

  const handleRegister = async () => {
    const response = await fetch('/api/users', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ name, email, password }),
    });

    if (response.ok) {
      showDialog = false;
      setSnackbarOpen(true);
    }
  };

  if (snackbarOpen) {
    return (
      <Snackbar
        anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }}
        open={snackbarOpen}
        autoHideDuration={6000}
        onClose={() => { onResult(true); }}
        message={t('loginCreated')}
      />
    );
  } if (showDialog) {
    return (
      <Dialog
        open
        onClose={() => { onResult(false); }}
      >
        <DialogContent>
          <DialogContentText>{t('loginRegister')}</DialogContentText>
          <TextField
            margin="normal"
            required
            fullWidth
            label={t('sharedName')}
            name="name"
            value={name || ''}
            autoComplete="name"
            autoFocus
            onChange={(event) => setName(event.target.value)}
          />
          <TextField
            margin="normal"
            required
            fullWidth
            type="email"
            label={t('userEmail')}
            name="email"
            value={email || ''}
            autoComplete="email"
            onChange={(event) => setEmail(event.target.value)}
          />
          <TextField
            margin="normal"
            required
            fullWidth
            label={t('userPassword')}
            name="password"
            value={password || ''}
            type="password"
            autoComplete="current-password"
            onChange={(event) => setPassword(event.target.value)}
          />
        </DialogContent>
        <DialogActions>
          <Button color="primary" onClick={handleRegister} disabled={submitDisabled()}>{t('loginRegister')}</Button>
          <Button autoFocus onClick={() => onResult(false)}>{t('sharedCancel')}</Button>
        </DialogActions>
      </Dialog>
    );
  }
  return null;
};

export default RegisterDialog;