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/newsletter page #1363

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
0ec01ba
Merge pull request #1270 from near/develop
charleslavon Jul 28, 2024
aff14b9
Merge pull request #1279 from near/develop
shelegdmitriy Aug 6, 2024
39c265c
Merge pull request #1284 from near/develop
charleslavon Aug 7, 2024
fbd691a
Merge pull request #1291 from near/develop
gagdiez Aug 14, 2024
040acae
Merge pull request #1294 from near/develop
mpeterdev Aug 15, 2024
5a34aee
Merge pull request #1299 from near/develop
gagdiez Aug 22, 2024
0f39f92
Merge pull request #1300 from near/develop
calebjacob Aug 29, 2024
9059f9a
Merge pull request #1305 from near/develop
calebjacob Sep 3, 2024
804491f
Merge pull request #1308 from near/develop
gagdiez Sep 11, 2024
50f913b
Merge pull request #1311 from near/develop
gagdiez Oct 4, 2024
be074d5
Merge pull request #1315 from near/develop
gagdiez Oct 8, 2024
fdb169a
Merge pull request #1317 from near/develop
gagdiez Oct 9, 2024
1048456
Merge pull request #1319 from near/develop
gagdiez Oct 9, 2024
6ce1729
Merge pull request #1323 from near/develop
gagdiez Oct 10, 2024
7e85300
Merge pull request #1325 from near/develop
gagdiez Oct 14, 2024
61df5dd
Merge pull request #1327 from near/develop
gagdiez Oct 16, 2024
40c4780
Merge pull request #1339 from near/develop
gagdiez Nov 8, 2024
69769e1
Merge pull request #1346 from near/develop
gagdiez Nov 22, 2024
acfbce2
Merge pull request #1350 from near/develop
gagdiez Jan 5, 2025
1bb6880
Merge pull request #1358 from near/develop
gagdiez Jan 5, 2025
25521a0
Merge pull request #1360 from near/develop
gagdiez Jan 5, 2025
d6937c0
fix: subscribe form
flmel Jan 8, 2025
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.',
});
}
}
}
}
20 changes: 11 additions & 9 deletions src/pages/newsletter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ const fadeIn = keyframes`
}
`;

const IssueCover = styled.img`
border-radius: 6px;
`;

const GreenBox = styled.div`
background: #00ec97;
border-radius: 6px;
Expand Down Expand Up @@ -111,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 @@ -127,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 @@ -138,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 @@ -169,9 +169,8 @@ const NewsPage: NextPageWithLayout = () => {

<Flex style={{ flexDirection: 'column' }}>
<Fixed>
<IssueCover src={`newsletter/${issueId}.jpg`} alt="" />
<GreenBox style={{ padding: '15px' }}>
<h3>
<h3 style={{ marginBottom: '0.5rem' }}>
<Text size="text-l">Subscribe to the newsletter</Text>
</h3>
{isFormSubmitted ? (
Expand All @@ -186,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 @@ -239,9 +241,9 @@ const NewsPage: NextPageWithLayout = () => {
</LinksList>
<hr />
</Fixed>
<ScrollToTop />
</Flex>
</Grid>
<ScrollToTop />
</Section>
);
};
Expand Down
Loading