Skip to content

Commit

Permalink
Fix newsletter form
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolalsvk committed Jan 16, 2024
1 parent b880c4a commit 86b15ba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
GATSBY_FUNCTION_HOST=
GATSBY_FIREBASE_CONFIG=

GATSBY_CONVERTKIT_PUBLIC_API_KEY=
20 changes: 13 additions & 7 deletions src/components/subscribe-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,29 +84,35 @@ const SubscribeForm = () => {
const [email, setEmail] = useState("")
const [name, setName] = useState("")

const FORM_URL = `https://app.convertkit.com/forms/1275610/subscriptions`
const FORM_URL = `https://api.convertkit.com/v3/forms/1275610/subscribe`

const handleSubmit = async (event: React.FormEvent) => {
event.preventDefault()

const data = new FormData(event.target as HTMLFormElement)
const payload = JSON.stringify({
email,
first_name: name,
api_key: process.env.GATSBY_CONVERTKIT_PUBLIC_API_KEY,
})

try {
const response = await fetch(FORM_URL, {
method: "post",
body: data,
method: "POST",
body: payload,
headers: {
accept: "application/json",
Accept: "application/json; charset=utf-8",
"Content-Type": "application/json",
},
})

setEmail("")
const json = await response.json()

if (json.status === "success") {
if (json?.subscription?.id) {
setStatus("SUCCESS")
return
}

setStatus("ERROR")
} catch (err) {
setStatus("ERROR")
console.log(err)
Expand Down

0 comments on commit 86b15ba

Please sign in to comment.