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
|
|
|
|
className="mt-6 flex flex-col sm:flex-row lg:mt-0 lg:justify-begin"
|
|
|
|
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"
|
|
|
|
placeholder="Enter your email"
|
|
|
|
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
|
|
|
|
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 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"
|
|
|
|
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>
|
|
|
|
<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">
|
|
|
|
{message ? message : 'Enter your email address to be notified of new posts'}
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-09-02 10:17:34 +02:00
|
|
|
export { FormSubscribe }
|