import { FC, useEffect, useState, useCallback } from 'react' import { validate } from 'email-validator' import { Info } from '@components/icons' import { useUI } from '@components/ui/context' import { Logo, Button, Input } from '@components/ui' import useSignup from '@framework/use-signup' interface Props {} const SignUpView: FC = () => { // Form State const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [firstName, setFirstName] = useState('') const [lastName, setLastName] = useState('') const [loading, setLoading] = useState(false) const [message, setMessage] = useState('') const [dirty, setDirty] = useState(false) const [disabled, setDisabled] = useState(false) const signup = useSignup() const { setModalView, closeModal } = useUI() const handleSignup = async (e: React.SyntheticEvent) => { e.preventDefault() if (!dirty && !disabled) { setDirty(true) handleValidation() } try { setLoading(true) setMessage('') await signup({ email, firstName, lastName, password, }) setLoading(false) closeModal() } catch ({ errors }) { setMessage(errors[0].message) setLoading(false) } } const handleValidation = useCallback(() => { // Test for Alphanumeric password const validPassword = /^(?=.*[a-zA-Z])(?=.*[0-9])/.test(password) // Unable to send form unless fields are valid. if (dirty) { setDisabled(!validate(email) || password.length < 7 || !validPassword) } }, [email, password, dirty]) useEffect(() => { handleValidation() }, [handleValidation]) return (
{message && (
{message}
)} {' '} Info: Passwords must be longer than 7 chars and include numbers.{' '}
Do you have an account? {` `} setModalView('LOGIN_VIEW')} > Log In
) } export default SignUpView