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)
|
2021-09-11 12:23:05 +08:00
|
|
|
const [error, setError] = useState(false)
|
2021-08-18 17:33:15 +02:00
|
|
|
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-11 12:23:05 +08:00
|
|
|
setError(true)
|
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-11 12:23:05 +08:00
|
|
|
setError(false)
|
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>
|
2021-09-10 22:50:32 +08:00
|
|
|
<div className="pb-1 text-lg font-semibold text-gray-800 dark:text-gray-100">
|
|
|
|
Subscribe to the newsletter
|
|
|
|
</div>
|
|
|
|
<form className="flex flex-col sm:flex-row" onSubmit={subscribe}>
|
2021-08-18 17:33:15 +02:00
|
|
|
<div>
|
|
|
|
<label className="sr-only" htmlFor="email-input">
|
|
|
|
Email address
|
|
|
|
</label>
|
|
|
|
<input
|
|
|
|
autoComplete="email"
|
2021-09-11 12:23:05 +08:00
|
|
|
className="px-4 rounded-md w-72 dark:bg-black focus:outline-none focus:ring-2 focus:border-transparent focus:ring-primary-600"
|
2021-08-18 17:33:15 +02:00
|
|
|
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>
|
2021-09-10 22:50:32 +08:00
|
|
|
<div className="flex w-full mt-2 rounded-md shadow-sm sm:mt-0 sm:ml-3">
|
2021-08-18 17:33:15 +02:00
|
|
|
<button
|
2021-09-11 12:23:05 +08:00
|
|
|
className={`w-full bg-primary-500 px-4 py-2 rounded-md font-medium text-white ${
|
2021-09-10 22:50:32 +08:00
|
|
|
subscribed ? 'cursor-default' : 'hover:bg-primary-700 dark:hover:bg-primary-400'
|
2021-09-11 12:23:05 +08:00
|
|
|
} focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary-600 dark:ring-offset-black`}
|
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-11 12:23:05 +08:00
|
|
|
{error && (
|
|
|
|
<p className="pt-2 text-sm text-red-500 w-72 sm:w-96 dark:text-red-400">{message}</p>
|
|
|
|
)}
|
2021-08-18 17:33:15 +02:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-09-02 10:17:34 +02:00
|
|
|
export { FormSubscribe }
|