|
| 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