import { FunctionComponent, Fragment, memo, useState, SyntheticEvent, } from 'react' import { GoogleLoginButton } from '../google-login' import { useRouter } from 'next/router' import { useMutation } from '@apollo/react-hooks' import { REGISTER, LOGIN } from '@app/mutations' import { AppManager, UserManager } from '@app/managers' import { Link } from '../link' import { LinearBottom } from '../loaders' import { DOMAIN_NAME } from '@app/configs' import { GrGithub } from 'react-icons/gr' import { REST_API } from '@app/configs/app-config' import { Header } from '../header' import { Button } from '../buttons' import { FormControl } from '../form-control' import { TextField } from '../text-field' const clientID = process.env.GITHUB_CLIENT_ID const redirectGithub = `${REST_API}/github/callback` interface SignOnProps { loginView?: boolean googleLoginSkeleton?: boolean } const SignOnFormWrapper: FunctionComponent = ({ loginView, googleLoginSkeleton = false, }) => { const router = useRouter() const [signOnMutation, { loading }] = useMutation( loginView ? LOGIN : REGISTER ) const [email, setEmail] = useState('') const [password, setPassword] = useState('') const onSuccessAuth = (data: any) => { const user = data && data[loginView ? 'login' : 'register'] if (user) { UserManager.setUser(user) const p = router?.query?.plan const plan = p && (String(p).toLocaleLowerCase() as string) const urlRoute = plan && plan !== 'free' ? `/payments?plan=${router?.query?.plan}` : '/dashboard' window.location.href = urlRoute } } const submit = async (e: any) => { e?.preventDefault() let data = null if (!password || !email) { AppManager.toggleSnack( true, !password ? 'Please enter a password of at least 6 characters.' : 'Please check your email and password and try again.', 'error' ) } else { try { const res = await signOnMutation({ variables: { email, password, }, }) if (res) { data = res.data } } catch (e) { console.error(e) } onSuccessAuth(data) } } const onGoogleAuth = async (response: any) => { let data try { const res = await signOnMutation({ variables: { email: response?.profileObj?.email, googleId: response?.googleId, password: '', }, }) if (res) { data = res.data } } catch (e) { console.error(e) } onSuccessAuth(data) } const onChangeEmailEvent = ( e: SyntheticEvent ) => setEmail(e.currentTarget.value) const onChangePasswordEvent = ( e: SyntheticEvent ) => setPassword(e.currentTarget.value) const Heading = Header return (
{(loginView && 'Login') || 'Register'}

Or

Email

{`We'll never share your email.`}

Password

{`We'll never share your password.`}

Forgot Password?{' '} Reset

This site is protected by the{' '} Google Privacy Policy {' '} and{' '} Terms of Service {' '} apply. By clicking {`"Create account"`}, I agree to {`A11yWatch's `} TOS {' '} and{' '} Privacy Policy .
) } export const SignOnForm = memo(SignOnFormWrapper)