import t from './common/localization' 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'; const RegisterDialog = ({ showDialog, onResult }) => { const [formFields, setFormFields] = useState({}); const [validationErrors, setValidationErrors] = useState({}); const [snackbarOpen, setSnackbarOpen] = useState(false); const handleChange = (event) => { setFormFields({ ...formFields, [event.target.name]: event.target.value }); setValidationErrors({ ...validationErrors, [event.target.name]: false }); } const handleRegister = async (event) => { let objErrors = {}; if (formFields.name.trim() === '') { objErrors.name = true; } if (!/(.+)@(.+)\.(.{2,})/.test(formFields.email)) { objErrors.email = true; } if (formFields.password.trim() === '') { objErrors.password = true; } if (Object.keys(objErrors).length !== 0) { setValidationErrors(objErrors); return; } const response = await fetch('/api/users', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(formFields) }); if (response.ok) { showDialog = false; setSnackbarOpen(true); } } if (snackbarOpen) { return { onResult(true) }} message={t('loginCreated')} /> } else if (showDialog) { return { onResult(false) }}> {t('loginRegister')} ; } else return null; }; export default RegisterDialog;