Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: subscribe form #1365

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/components/scrollToTop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? '')
Expand Down
21 changes: 14 additions & 7 deletions src/pages/api/newsletter/subscribe.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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',
});
Expand All @@ -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.',
});
}
}
}
}
15 changes: 11 additions & 4 deletions src/pages/newsletter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const NewsPage: NextPageWithLayout = () => {
const router = useRouter();
const { id } = router.query;
const [isFormSubmitted, setIsFormSubmitted] = useState(false);
const [responseError, setResponseError] = useState<string | null>(null);

const { data: campaigns, isLoading: campaignLoading } = useSWR('/api/newsletter', fetcher);
const issueId = useMemo(() => id || campaigns?.[0]?.id, [id, campaigns]);
Expand All @@ -123,6 +124,7 @@ const NewsPage: NextPageWithLayout = () => {

const onSubmit: SubmitHandler<SubscribeForm> = async (data) => {
try {
setResponseError(null);
const response = await fetch('/api/newsletter/subscribe', {
method: 'POST',
headers: {
Expand All @@ -134,17 +136,19 @@ 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);
}
};

if (campaignLoading) return;
console.log('CAMP', campaigns);
if ('error' in campaigns) return <Section grow="available"> Failed to load newsletter </Section>;

return (
Expand All @@ -166,7 +170,7 @@ const NewsPage: NextPageWithLayout = () => {
<Flex style={{ flexDirection: 'column' }}>
<Fixed>
<GreenBox style={{ padding: '15px' }}>
<h3>
<h3 style={{ marginBottom: '0.5rem' }}>
<Text size="text-l">Subscribe to the newsletter</Text>
</h3>
{isFormSubmitted ? (
Expand All @@ -181,12 +185,15 @@ const NewsPage: NextPageWithLayout = () => {
<>
<Form onSubmit={handleSubmit(onSubmit)}>
{errors.email && <span style={{ color: 'red' }}> This field is required!</span>}
{responseError && <span style={{ color: 'red' }}>{responseError}</span>}
<SubscribeForm gap="none">
<input
{...register('email', { required: true })}
placeholder="dev@youremail.com"
type="email"
className=""
onChange={() => {
setResponseError(null);
}}
/>
<Button
label="Subscribe"
Expand Down Expand Up @@ -235,8 +242,8 @@ const NewsPage: NextPageWithLayout = () => {
<hr />
</Fixed>
</Flex>
<ScrollToTop />
</Grid>
<ScrollToTop />
</Section>
);
};
Expand Down
Loading