Last active
August 10, 2021 18:03
-
-
Save ernestofreyreg/de083762abbc864e5c8b1181377f83b5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import * as React from 'react' | |
import { NextPage } from 'next' | |
import { Container } from '@material-ui/core' | |
import { SubscribeHero } from 'components/SubscribeHero/SubscribeHero' | |
import { secureLoader, useAPIPost } from 'lib/api' | |
import useSWR from 'swr' | |
import { SubscriptionList, EmailSubscription } from 'components/SubscriptionList' | |
const URL = '/api/subscriptions' | |
const IndexPage: NextPage = () => { | |
const subscribe = useAPIPost<void, { email: string }>(URL) | |
const { data, revalidate } = useSWR<EmailSubscription[]>(URL, secureLoader()) | |
React.useEffect(() => { | |
if (subscribe.posted) { | |
revalidate() | |
subscribe.reset() | |
} | |
}, [subscribe.posted]) | |
return ( | |
<Container maxWidth='lg'> | |
<SubscribeHero onSubmit={email => subscribe.post({ email })} hasError={!!subscribe.error} /> | |
<SubscriptionList subscriptions={data || []} /> | |
</Container> | |
) | |
} | |
export default IndexPage |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment