|
| 1 | +import { useRef, useState } from 'react' |
| 2 | + |
| 3 | +import siteMetadata from '@/data/siteMetadata' |
| 4 | + |
| 5 | +const NewsletterForm = ({ title = 'Subscribe to the newsletter' }) => { |
| 6 | + const inputEl = useRef(null) |
| 7 | + const [error, setError] = useState(false) |
| 8 | + const [message, setMessage] = useState('') |
| 9 | + const [subscribed, setSubscribed] = useState(false) |
| 10 | + |
| 11 | + const subscribe = async (e) => { |
| 12 | + e.preventDefault() |
| 13 | + |
| 14 | + const res = await fetch(`/api/${siteMetadata.newsletter.provider}`, { |
| 15 | + body: JSON.stringify({ |
| 16 | + email: inputEl.current.value, |
| 17 | + }), |
| 18 | + headers: { |
| 19 | + 'Content-Type': 'application/json', |
| 20 | + }, |
| 21 | + method: 'POST', |
| 22 | + }) |
| 23 | + |
| 24 | + const { error } = await res.json() |
| 25 | + if (error) { |
| 26 | + setError(true) |
| 27 | + setMessage('Your e-mail address is invalid or you are already subscribed!') |
| 28 | + return |
| 29 | + } |
| 30 | + |
| 31 | + inputEl.current.value = '' |
| 32 | + setError(false) |
| 33 | + setSubscribed(true) |
| 34 | + setMessage('Successfully! 🎉 You are now subscribed.') |
| 35 | + } |
| 36 | + |
| 37 | + return ( |
| 38 | + <div> |
| 39 | + <div className="pb-1 text-lg font-semibold text-gray-800 dark:text-gray-100">{title}</div> |
| 40 | + <form className="flex flex-col sm:flex-row" onSubmit={subscribe}> |
| 41 | + <div> |
| 42 | + <label className="sr-only" htmlFor="email-input"> |
| 43 | + Email address |
| 44 | + </label> |
| 45 | + <input |
| 46 | + autoComplete="email" |
| 47 | + className="w-72 rounded-md px-4 focus:border-transparent focus:outline-none focus:ring-2 focus:ring-primary-600 dark:bg-black" |
| 48 | + id="email-input" |
| 49 | + name="email" |
| 50 | + placeholder={subscribed ? "You're subscribed ! 🎉" : 'Enter your email'} |
| 51 | + ref={inputEl} |
| 52 | + required |
| 53 | + type="email" |
| 54 | + disabled={subscribed} |
| 55 | + /> |
| 56 | + </div> |
| 57 | + <div className="mt-2 flex w-full rounded-md shadow-sm sm:mt-0 sm:ml-3"> |
| 58 | + <button |
| 59 | + className={`w-full rounded-md bg-primary-500 py-2 px-4 font-medium text-white sm:py-0 ${ |
| 60 | + subscribed ? 'cursor-default' : 'hover:bg-primary-700 dark:hover:bg-primary-400' |
| 61 | + } focus:outline-none focus:ring-2 focus:ring-primary-600 focus:ring-offset-2 dark:ring-offset-black`} |
| 62 | + type="submit" |
| 63 | + disabled={subscribed} |
| 64 | + > |
| 65 | + {subscribed ? 'Thank you!' : 'Sign up'} |
| 66 | + </button> |
| 67 | + </div> |
| 68 | + </form> |
| 69 | + {error && ( |
| 70 | + <div className="w-72 pt-2 text-sm text-red-500 dark:text-red-400 sm:w-96">{message}</div> |
| 71 | + )} |
| 72 | + </div> |
| 73 | + ) |
| 74 | +} |
| 75 | + |
| 76 | +export default NewsletterForm |
| 77 | + |
| 78 | +export const BlogNewsletterForm = ({ title }) => ( |
| 79 | + <div className="flex items-center justify-center"> |
| 80 | + <div className="bg-gray-100 p-6 dark:bg-gray-800 sm:px-14 sm:py-8"> |
| 81 | + <NewsletterForm title={title} /> |
| 82 | + </div> |
| 83 | + </div> |
| 84 | +) |
0 commit comments