import { FC, useEffect, useState, useCallback } from 'react' import { Logo, Modal, Button, Input } from '@components/ui' import useLogin from '@lib/bigcommerce/use-login' import { useUI } from '@components/ui/context' import { validate } from 'email-validator' interface Props {} const LoginView: FC = () => { // Form State const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [loading, setLoading] = useState(false) const [message, setMessage] = useState('') const [dirty, setDirty] = useState(false) const [disabled, setDisabled] = useState(false) const { setModalView, closeModal } = useUI() const login = useLogin() const handleLogin = async () => { if (!dirty && !disabled) { setDirty(true) handleValidation() } try { setLoading(true) setMessage('') await login({ email, 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}. Did you {` `} setModalView('FORGOT_VIEW')} > forgot your password?
)}
Don't have an account? {` `} setModalView('SIGNUP_VIEW')} > Sign Up
) } export default LoginView