import { useRef, useState } from 'react' import siteMetadata from '@/data/siteMetadata' const FormSubscribe = () => { const inputEl = useRef(null) const [message, setMessage] = useState('') const [subscribed, setSubscribed] = useState(false) const subscribe = async (e) => { e.preventDefault() const res = await fetch(`/api/${siteMetadata.newsletter.provider}`, { body: JSON.stringify({ email: inputEl.current.value, }), headers: { 'Content-Type': 'application/json', }, method: 'POST', }) const { error } = await res.json() if (error) { setMessage('Your e-mail adress is invalid or you are already subscribed!') return } inputEl.current.value = '' setSubscribed(true) setMessage('Successfully! 🎉 You are now subscribed.') } return (
{message ? message : 'Enter your email address to be notified of new posts'}