tailwind-nextjs-blog/components/NewsletterForm.tsx

94 lines
2.8 KiB
TypeScript
Raw Permalink Normal View History

2022-10-17 23:37:01 +08:00
import React, { useRef, useState } from 'react';
2022-07-17 21:40:41 +08:00
2022-10-17 23:37:01 +08:00
import siteMetadata from '@/data/siteMetadata';
2022-07-17 21:40:41 +08:00
const NewsletterForm = ({ title = 'Subscribe to the newsletter' }) => {
2022-10-17 23:37:01 +08:00
const inputEl = useRef<HTMLInputElement>(null);
const [error, setError] = useState(false);
const [message, setMessage] = useState('');
const [subscribed, setSubscribed] = useState(false);
2022-07-17 21:40:41 +08:00
const subscribe = async (e: React.FormEvent<HTMLFormElement>) => {
2022-10-17 23:37:01 +08:00
e.preventDefault();
2022-07-17 21:40:41 +08:00
const res = await fetch(`/api/${siteMetadata.newsletter.provider}`, {
body: JSON.stringify({
email: inputEl.current.value,
}),
headers: {
'Content-Type': 'application/json',
},
method: 'POST',
2022-10-17 23:37:01 +08:00
});
2022-07-17 21:40:41 +08:00
2022-10-17 23:37:01 +08:00
const { error } = await res.json();
2022-07-17 21:40:41 +08:00
if (error) {
2022-10-17 23:37:01 +08:00
setError(true);
setMessage(
'Your e-mail address is invalid or you are already subscribed!'
);
return;
2022-07-17 21:40:41 +08:00
}
2022-10-17 23:37:01 +08:00
inputEl.current.value = '';
setError(false);
setSubscribed(true);
setMessage('Successfully! 🎉 You are now subscribed.');
};
2022-07-17 21:40:41 +08:00
return (
<div>
2022-10-17 23:37:01 +08:00
<div className="pb-1 text-lg font-semibold text-gray-800 dark:text-gray-100">
{title}
</div>
2022-07-17 21:40:41 +08:00
<form className="flex flex-col sm:flex-row" onSubmit={subscribe}>
<div>
<label className="sr-only" htmlFor="email-input">
Email address
</label>
<input
autoComplete="email"
className="w-72 rounded-md px-4 focus:border-transparent focus:outline-none focus:ring-2 focus:ring-primary-600 dark:bg-black"
id="email-input"
name="email"
2022-10-17 23:37:01 +08:00
placeholder={
subscribed ? "You're subscribed ! 🎉" : 'Enter your email'
}
2022-07-17 21:40:41 +08:00
ref={inputEl}
required
type="email"
disabled={subscribed}
/>
</div>
<div className="mt-2 flex w-full rounded-md shadow-sm sm:mt-0 sm:ml-3">
<button
className={`w-full rounded-md bg-primary-500 py-2 px-4 font-medium text-white sm:py-0 ${
2022-10-17 23:37:01 +08:00
subscribed
? 'cursor-default'
: 'hover:bg-primary-700 dark:hover:bg-primary-400'
2022-07-17 21:40:41 +08:00
} focus:outline-none focus:ring-2 focus:ring-primary-600 focus:ring-offset-2 dark:ring-offset-black`}
type="submit"
2022-10-17 23:37:01 +08:00
disabled={subscribed}>
2022-07-17 21:40:41 +08:00
{subscribed ? 'Thank you!' : 'Sign up'}
</button>
</div>
</form>
{error && (
2022-10-17 23:37:01 +08:00
<div className="w-72 pt-2 text-sm text-red-500 dark:text-red-400 sm:w-96">
{message}
</div>
2022-07-17 21:40:41 +08:00
)}
</div>
2022-10-17 23:37:01 +08:00
);
};
2022-07-17 21:40:41 +08:00
2022-10-17 23:37:01 +08:00
export default NewsletterForm;
2022-07-17 21:40:41 +08:00
export const BlogNewsletterForm = ({ title }) => (
<div className="flex items-center justify-center">
<div className="bg-gray-100 p-6 dark:bg-gray-800 sm:px-14 sm:py-8">
<NewsletterForm title={title} />
</div>
</div>
2022-10-17 23:37:01 +08:00
);