jonbio/components/FormSubscribe.js

77 lines
2.7 KiB
JavaScript
Raw Normal View History

2021-08-18 17:33:15 +02:00
import { useRef, useState } from 'react'
import siteMetadata from '@/data/siteMetadata'
2021-09-02 10:17:34 +02:00
const FormSubscribe = () => {
2021-08-18 17:33:15 +02:00
const inputEl = useRef(null)
const [message, setMessage] = useState('')
2021-09-02 10:17:34 +02:00
const [subscribed, setSubscribed] = useState(false)
2021-08-18 17:33:15 +02:00
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) {
2021-09-02 10:17:34 +02:00
setMessage('Your e-mail adress is invalid or you are already subscribed!')
2021-08-18 17:33:15 +02:00
return
}
inputEl.current.value = ''
2021-09-02 10:17:34 +02:00
setSubscribed(true)
2021-08-18 17:33:15 +02:00
setMessage('Successfully! 🎉 You are now subscribed.')
}
return (
<div>
<form
2021-09-10 14:32:18 +02:00
// className="mt-6 flex flex-col sm:flex-row lg:mt-0 lg:justify-begin"
className="flex flex-col sm:flex-row lg:justify-begin"
2021-08-18 17:33:15 +02:00
onSubmit={subscribe}
>
<div>
<label className="sr-only" htmlFor="email-input">
Email address
</label>
<input
autoComplete="email"
className="appearance-none w-full px-4 py-2 border border-neutrals-cool-grey-300 text-base rounded-md text-neutrals-cool-grey-900 bg-white dark:bg-black placeholder-gray-500 focus:outline-none focus:ring-primary-400 dark:focus:border-primary-600 lg:max-w-xs"
id="email-input"
name="email"
2021-09-10 14:32:18 +02:00
placeholder={subscribed ? "You're subscribed ! 🎉" : 'Enter your email'}
2021-08-18 17:33:15 +02:00
ref={inputEl}
required
type="email"
2021-09-02 10:17:34 +02:00
disabled={subscribed}
2021-08-18 17:33:15 +02:00
/>
</div>
<div className="mt-2 flex-shrink-0 w-full flex rounded-md shadow-sm sm:mt-0 sm:ml-3 sm:w-auto sm:inline-flex">
<button
2021-09-02 20:06:01 +02:00
className={`w-full bg-primary-500 dark:bg-primary-400 px-4 py-2 border border-transparent rounded-md flex items-center justify-center text-base font-medium text-white ${
subscribed ? 'cursor-default' : 'hover:bg-primary-700 dark:hover:bg-primary-300'
} focus:outline-none focus:ring-2 focus:ring-offset-2 focus:primary-700 sm:w-auto sm:inline-flex`}
2021-08-18 17:33:15 +02:00
type="submit"
2021-09-02 10:17:34 +02:00
disabled={subscribed}
2021-08-18 17:33:15 +02:00
>
2021-09-02 10:17:34 +02:00
{subscribed ? 'Thank you!' : 'Sign up'}
2021-08-18 17:33:15 +02:00
</button>
</div>
</form>
2021-09-10 14:32:18 +02:00
{/* <p className="text-sm text-gray-500 dark:text-gray-400 pt-2 flex flex-col sm:flex-row lg:mt-0 lg:justify-begin">
2021-08-18 17:33:15 +02:00
{message ? message : 'Enter your email address to be notified of new posts'}
2021-09-10 14:32:18 +02:00
</p> */}
2021-08-18 17:33:15 +02:00
</div>
)
}
2021-09-02 10:17:34 +02:00
export { FormSubscribe }