From 7691e58db1ea41448ac73b62ed2b4de4d81bd8a4 Mon Sep 17 00:00:00 2001 From: Lyudmil Ivanov <55487633+flmel@users.noreply.github.com> Date: Wed, 8 Jan 2025 12:54:17 +0200 Subject: [PATCH] fix: subscribe form --- src/components/scrollToTop.tsx | 1 - src/config.ts | 2 +- src/pages/api/newsletter/subscribe.ts | 21 ++++++++++++++------- src/pages/newsletter.tsx | 15 +++++++++++---- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/components/scrollToTop.tsx b/src/components/scrollToTop.tsx index 11029ae99..fde501d99 100644 --- a/src/components/scrollToTop.tsx +++ b/src/components/scrollToTop.tsx @@ -14,7 +14,6 @@ const fadeIn = keyframes` const ScrollToTopButton = styled(Button)<{ isVisible: boolean }>` position: fixed; bottom: 15px; - right: 30px; cursor: pointer; z-index: 1000; animation: ${fadeIn} 0.5s; diff --git a/src/config.ts b/src/config.ts index 3f0d93010..f5bc19cb6 100644 --- a/src/config.ts +++ b/src/config.ts @@ -104,7 +104,7 @@ export const googleCalendarApiKey = process.env.NEXT_PUBLIC_GOOGLE_CALENDAR_API_ export const devhubCommunityCalendarId = process.env.NEXT_PUBLIC_DEVHUB_COMMUNITY_CALENDAR_ID ?? ''; export const mailchimpApiKey = process.env.NEXT_PUBLIC_MAILCHIMP_API_KEY ?? ''; export const mailchimpRegion = process.env.NEXT_PUBLIC_MAILCHIMP_REGION ?? ''; -export const mailchimpAudienceId = process.env.NEXT_PUBLIC_MAILCHIMP_AUDIENCE_ID ?? ''; +export const newsletterAudienceId = process.env.NEXT_PUBLIC_NEWSLETTER_AUDIENCE_ID ?? ''; export const EVMWalletChain = evmWalletChains[networkId]; export const commitModalBypassAuthorIds = (process.env.NEXT_PUBLIC_COMMIT_MODAL_BYPASS_AUTHOR_IDS ?? '') diff --git a/src/pages/api/newsletter/subscribe.ts b/src/pages/api/newsletter/subscribe.ts index d37fc2f17..0b7c9fe70 100644 --- a/src/pages/api/newsletter/subscribe.ts +++ b/src/pages/api/newsletter/subscribe.ts @@ -1,7 +1,7 @@ import mailchimp from '@mailchimp/mailchimp_marketing'; import type { NextApiRequest, NextApiResponse } from 'next'; -import { mailchimpApiKey, mailchimpAudienceId, mailchimpRegion } from '../../../config'; +import { mailchimpApiKey, mailchimpRegion, newsletterAudienceId } from '../../../config'; mailchimp.setConfig({ apiKey: mailchimpApiKey, @@ -13,7 +13,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) const { email } = req.body; try { - const response = await mailchimp.lists.addListMember(mailchimpAudienceId, { + const response = await mailchimp.lists.addListMember(newsletterAudienceId, { email_address: email, status: 'pending', }); @@ -25,13 +25,20 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) success: true, message: 'Form submitted successfully! Please confirm your email', }); - } catch (error) { + } catch (error: any) { console.error('Error adding to list:', error); - res.status(500).json({ - success: false, - message: 'An error occurred while processing your request.', - }); + if (error.status === 400 && error.response.body.title === 'Member Exists') { + res.status(500).json({ + success: false, + message: 'Email already subscribed.', + }); + } else { + res.status(500).json({ + success: false, + message: 'An error occurred while processing your request.', + }); + } } } } diff --git a/src/pages/newsletter.tsx b/src/pages/newsletter.tsx index 12d7461b3..96cd34fdf 100644 --- a/src/pages/newsletter.tsx +++ b/src/pages/newsletter.tsx @@ -107,6 +107,7 @@ const NewsPage: NextPageWithLayout = () => { const router = useRouter(); const { id } = router.query; const [isFormSubmitted, setIsFormSubmitted] = useState(false); + const [responseError, setResponseError] = useState(null); const { data: campaigns, isLoading: campaignLoading } = useSWR('/api/newsletter', fetcher); const issueId = useMemo(() => id || campaigns?.[0]?.id, [id, campaigns]); @@ -123,6 +124,7 @@ const NewsPage: NextPageWithLayout = () => { const onSubmit: SubmitHandler = async (data) => { try { + setResponseError(null); const response = await fetch('/api/newsletter/subscribe', { method: 'POST', headers: { @@ -134,9 +136,12 @@ const NewsPage: NextPageWithLayout = () => { }); await response; + const responseData = await response.json(); if (response.ok) { setIsFormSubmitted(true); + } else { + setResponseError(responseData?.message || 'Failed to subscribe. Please try again.'); } } catch (error) { console.error(error); @@ -144,7 +149,6 @@ const NewsPage: NextPageWithLayout = () => { }; if (campaignLoading) return; - console.log('CAMP', campaigns); if ('error' in campaigns) return
Failed to load newsletter
; return ( @@ -166,7 +170,7 @@ const NewsPage: NextPageWithLayout = () => { -

+

Subscribe to the newsletter

{isFormSubmitted ? ( @@ -181,12 +185,15 @@ const NewsPage: NextPageWithLayout = () => { <>
{errors.email && This field is required!} + {responseError && {responseError}} { + setResponseError(null); + }} />