-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Buttondown newsletter subscription, setup with server actions 😎
- Loading branch information
1 parent
830d46d
commit 5ae09c8
Showing
4 changed files
with
125 additions
and
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
dist/ | ||
node_modules/ | ||
*.db | ||
.env | ||
|
||
# ts-gql | ||
__generated__ | ||
|
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
BUTTONDOWN_API_KEY= |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use server' | ||
|
||
// ------------------------------ | ||
// Buttondown subscription | ||
// ------------------------------ | ||
export async function subscribeToButtondown (pathname: string, formData: FormData) { | ||
try { | ||
const data = { | ||
email: formData.get('email'), | ||
tags: [ | ||
...formData.getAll('tags'), | ||
`keystone website${pathname !== '/' ? `: ${pathname}` : ' homepage'}`, | ||
], | ||
} | ||
|
||
const buttondownResponse = await fetch('https://api.buttondown.email/v1/subscribers', { | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Token ${process.env.BUTTONDOWN_API_KEY}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
email_address: data.email, | ||
tags: data.tags, | ||
}), | ||
}) | ||
|
||
if (!buttondownResponse.ok) { | ||
const error = await buttondownResponse.json() | ||
return { | ||
// 409 status Conflict has no detail message | ||
error: error?.detail || 'Sorry, an error has occurred — please try again later.', | ||
} | ||
} | ||
|
||
return { success: true } | ||
} catch (error) { | ||
console.error('An error occurred: ', error) | ||
return { | ||
error: 'Sorry, an error has occurred — please try again later.', | ||
} | ||
} | ||
} |
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