import React from "react"; import axios from 'axios'; import { makeStyles } from "@material-ui/core/styles"; import TextField from '@material-ui/core/TextField'; import Card from '@material-ui/core/Card'; import CardContent from '@material-ui/core/CardContent'; import CardActions from '@material-ui/core/CardActions'; import Button from '@material-ui/core/Button'; import config from "../config/config" const useStyles = makeStyles(theme => ({ container: { display: 'flex', flexWrap: 'wrap', justifyContent: "center", maxWidth: 400, margin: `${theme.spacing(0)} auto` }, signup_button: { marginBottom: theme.spacing(2), flexGrow: 1 }, card: { marginTop: theme.spacing(10) }, CardActions: { 'flex-wrap': 'wrap' } })); function SignUpForm() { const classes = useStyles(); const [email, setEmail] = React.useState(''); const [password, setPassword] = React.useState(''); const [password2, setPassword2] = React.useState(''); const [isButtonDisabled, setIsButtonDisabled] = React.useState(true); const [helperText, setHelperText] = React.useState(''); const [error, setError] = React.useState(false); function validateEmail(email: string) { const re = /\S+@\S+\.\S+/; return re.test(email); } React.useEffect(() => { if (email.trim() && password.trim() && password2.trim()) { setIsButtonDisabled(false); } else { setIsButtonDisabled(true); } }, [email, password, password2]); const handleSignUp = (token: string) => { const login_data = { email: email, password: password, 'g-recaptcha-response': token } axios(`/signup`, { method: "post", data: login_data, withCredentials: true }).then((resp) => { setError(false); setHelperText(resp.data.message); console.log(resp) }).catch((err) => { console.log(err) setError(true); if (err.response) { setHelperText(err.response.data.message) console.log(err.response) } else { setHelperText("Unknown error") } }) }; const _handleSignUp = () => { /*global grecaptcha*/ // defined in pages/_document.tsx if (validateEmail(email)) { if (password === password2) { if (password.length > 7 && password.length < 129) { grecaptcha.ready(function () { grecaptcha.execute(config.recaptcha_site_key, { action: 'signup' }).then(function (token) { handleSignUp(token) }); }) } else { setError(true) setHelperText("password length must be greater than 7 and less than 129") } } else { setError(true) setHelperText("passwords do not match") } } else { setError(true) setHelperText("email is invalid") } } const handleKeyPress = (e: React.KeyboardEvent) => { if (e.keyCode === 13 || e.which === 13) { isButtonDisabled || _handleSignUp(); } }; return (
setEmail(e.target.value)} onKeyPress={(e) => handleKeyPress(e)} /> setPassword(e.target.value)} onKeyPress={(e) => handleKeyPress(e)} /> setPassword2(e.target.value)} onKeyPress={(e) => handleKeyPress(e)} />
); } export default SignUpForm;