-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1365 from flmel/fix/newsletter
fix: subscribe form
- Loading branch information
Showing
4 changed files
with
26 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]); | ||
|
@@ -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: { | ||
|
@@ -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 ( | ||
|
@@ -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 ? ( | ||
|
@@ -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="[email protected]" | ||
type="email" | ||
className="" | ||
onChange={() => { | ||
setResponseError(null); | ||
}} | ||
/> | ||
<Button | ||
label="Subscribe" | ||
|
@@ -235,8 +242,8 @@ const NewsPage: NextPageWithLayout = () => { | |
<hr /> | ||
</Fixed> | ||
</Flex> | ||
<ScrollToTop /> | ||
</Grid> | ||
<ScrollToTop /> | ||
</Section> | ||
); | ||
}; | ||
|