import { useRef, useState } from 'react' import Image from 'next/future/image' import Link from 'next/link' import BlogPostCard from '../components/BlogPostCard' import VideoCard from '../components/VideoCard' import prisma from 'lib/prisma' import { Form, FormState, Subscribers } from 'lib/types' import fetcher from 'lib/fetcher' import { GoogleIcon, LoadingSpinner } from 'components/Icons' import ErrorMessage from 'components/ErrorMessage' import SuccessMessage from 'components/SuccessMessage' import useSWR from 'swr' import gradient from 'lib/gradient' import { Disclosure } from '@headlessui/react' import { ChevronUpIcon } from '@heroicons/react/24/solid' import { useRouter } from 'next/router' import { getGoogleURL } from 'lib/google' export default function Home() { const router = useRouter() const [form, setForm] = useState({ state: Form.Initial }) const emailRef = useRef(null) const passwordRef = useRef(null) const onLogin = async (e) => { e.preventDefault() setForm({ state: Form.Loading }) const res = await fetch(`/api/auth/register`, { method: 'POST', headers: { 'content-type': 'application/json;charset=UTF-8', }, body: JSON.stringify({ email: emailRef.current.value, password: passwordRef.current.value, }), }) const { error } = await res.json() if (error) { setForm({ state: Form.Error, message: error, }) return } else { router.replace('/') } } const onRegister = async (e) => { e.preventDefault() setForm({ state: Form.Loading }) const res = await fetch(`/api/auth/register`, { method: 'POST', headers: { 'content-type': 'application/json;charset=UTF-8', }, body: JSON.stringify({ email: emailRef.current.value, password: passwordRef.current.value, }), }) const { error } = await res.json() if (error) { setForm({ state: Form.Error, message: error, }) return } else { router.replace('/') } } return (
{({ open }) => ( <>

Login

Login with Email/ Password
{form.state === Form.Error ? ( {form.message} ) : form.state === Form.Success ? ( {form.message} ) : null}
)}
{({ open }) => ( <>

Register

Register with Email/ Password
{form.state === Form.Error ? ( {form.message} ) : form.state === Form.Success ? ( {form.message} ) : null}
)}
) }