Skip to content

Commit 715f399

Browse files
authored
Merge pull request #182 from akd-io/feature/181-set-up-newsletter
Set up newsletter
2 parents c7d2d82 + f23ba7c commit 715f399

File tree

6 files changed

+116
-8
lines changed

6 files changed

+116
-8
lines changed

website/.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
NEXT_PUBLIC_CONVERTKIT_API_KEY=GHAdhoS_vF2IDp-9z0YdqA
2+
NEXT_PUBLIC_CONVERTKIT_FORM_ID=5191913

website/components/Section.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import { Box, Flex } from "@chakra-ui/react"
1+
import { Flex } from "@chakra-ui/react"
22
import { ComponentProps, FC } from "react"
33

44
type SectionProps = ComponentProps<typeof Flex> & {
5-
innerProps?: ComponentProps<typeof Box>
5+
innerProps?: ComponentProps<typeof Flex>
66
}
7-
export const Section: FC<SectionProps> = ({ boxProps, children, ...props }) => {
7+
export const Section: FC<SectionProps> = ({
8+
innerProps,
9+
children,
10+
...props
11+
}) => {
812
return (
913
<Flex
1014
direction="column"
@@ -19,7 +23,7 @@ export const Section: FC<SectionProps> = ({ boxProps, children, ...props }) => {
1923
width="100%"
2024
maxWidth="800"
2125
alignItems="center"
22-
{...boxProps}
26+
{...innerProps}
2327
>
2428
{children}
2529
</Flex>

website/templates/LandingPage/LandingPageTemplate.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { Box, Flex } from "@chakra-ui/react"
2-
import { css, Global } from "@emotion/react"
2+
import { Global, css } from "@emotion/react"
33
import { ComponentProps, FC } from "react"
44
import { Section } from "../../components/Section"
5-
import { Description } from "./components/DescriptionSection"
5+
import { Description } from "./components/Description"
66
import { Footer } from "./components/Footer"
77
import { HeaderSection } from "./components/Header"
8+
import { Newsletter } from "./components/NewsletterSection"
89
import { TechnologiesForm } from "./components/TechnologiesForm"
910

1011
const globalStyles = css`
@@ -46,6 +47,9 @@ const LandingPageTemplate = () => {
4647
</Card>
4748
</Section>
4849
</Flex>
50+
<Section paddingTop="20px">
51+
<Newsletter />
52+
</Section>
4953
<Footer />
5054
</Flex>
5155
</>

website/templates/LandingPage/components/DescriptionSection.tsx renamed to website/templates/LandingPage/components/Description.tsx

File renamed without changes.

website/templates/LandingPage/components/Footer.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ export const Footer: React.FC = () => {
99
<Section
1010
as="footer"
1111
innerProps={{
12-
alignItems: "left",
13-
gap: "32px",
12+
gap: "24px",
1413
}}
1514
>
1615
<SocialIcons />
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { Button, Flex, Heading, Input, Text } from "@chakra-ui/react"
2+
import { FC, useState } from "react"
3+
import { SubmitHandler, useForm } from "react-hook-form"
4+
5+
const NEXT_PUBLIC_CONVERTKIT_API_KEY =
6+
process.env.NEXT_PUBLIC_CONVERTKIT_API_KEY
7+
if (!NEXT_PUBLIC_CONVERTKIT_API_KEY) {
8+
throw new Error("Missing NEXT_PUBLIC_CONVERTKIT_API_KEY environment variable")
9+
}
10+
const NEXT_PUBLIC_CONVERTKIT_FORM_ID =
11+
process.env.NEXT_PUBLIC_CONVERTKIT_FORM_ID
12+
if (!NEXT_PUBLIC_CONVERTKIT_FORM_ID) {
13+
throw new Error("Missing NEXT_PUBLIC_CONVERTKIT_FORM_ID environment variable")
14+
}
15+
16+
type NewsletterFormData = {
17+
email: string
18+
}
19+
20+
export const Newsletter: FC = () => {
21+
const [state, setState] = useState<
22+
"showForm" | "loading" | "submitSuccess" | "submitError"
23+
>("showForm")
24+
25+
const { register, handleSubmit } = useForm<NewsletterFormData>()
26+
27+
const onSubmit: SubmitHandler<NewsletterFormData> = async (data) => {
28+
const { email } = data
29+
setState("loading")
30+
const response = await fetch(
31+
`https://api.convertkit.com/v3/forms/${NEXT_PUBLIC_CONVERTKIT_FORM_ID}/subscribe`,
32+
{
33+
body: JSON.stringify({
34+
api_key: NEXT_PUBLIC_CONVERTKIT_API_KEY,
35+
email,
36+
tags: [
37+
3868813, // Tag ID of "create-next-stack-com"
38+
],
39+
}),
40+
headers: {
41+
"Content-Type": "application/json",
42+
},
43+
method: "POST",
44+
}
45+
)
46+
if (response.status === 200) {
47+
setState("submitSuccess")
48+
} else {
49+
setState("submitError")
50+
}
51+
}
52+
53+
return (
54+
<Flex
55+
width="100%"
56+
direction="column"
57+
alignItems={["left", "center"]}
58+
gap="10px"
59+
>
60+
<Heading>Join the Newsletter</Heading>
61+
<Text>Stay up to date with new feature releases!</Text>
62+
{state === "submitSuccess" && (
63+
<Text padding="16px">Thank you for subscribing to the newsletter!</Text>
64+
)}
65+
{state !== "submitSuccess" ? (
66+
<Flex
67+
as="form"
68+
onSubmit={handleSubmit(onSubmit)}
69+
gap="10px"
70+
direction={["column", "row"]}
71+
padding="12px 0"
72+
>
73+
<Input
74+
{...register("email")}
75+
type="email"
76+
placeholder="Email Address"
77+
background="white"
78+
disabled={state === "loading"}
79+
/>
80+
<Button
81+
type="submit"
82+
variant="solid"
83+
colorScheme="purple"
84+
minWidth=""
85+
isLoading={state === "loading"}
86+
>
87+
Subscribe
88+
</Button>
89+
</Flex>
90+
) : null}
91+
{state === "submitError" && (
92+
<Text>Something went wrong. Please try again later.</Text>
93+
)}
94+
<Text fontSize="0.8em">
95+
{"We won't send you spam. Unsubscribe at any time."}
96+
</Text>
97+
</Flex>
98+
)
99+
}

0 commit comments

Comments
 (0)