-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d708773
commit cbaa548
Showing
5 changed files
with
168 additions
and
5 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
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,76 @@ | ||
/* eslint-disable no-console */ | ||
|
||
export async function POST(request: Request): Promise<Response> { | ||
const publicationId = process.env.BEEHIIV_PUBLICATION_ID | ||
if (!publicationId) { | ||
console.error('Must have BEEHIIV_PUBLICATION_ID defined') | ||
return Response.json( | ||
{ | ||
message: 'Oops! Something went wrong' | ||
}, | ||
{ | ||
status: 500 | ||
} | ||
) | ||
} | ||
const apiKey = process.env.BEEHIIV_API_KEY | ||
if (!apiKey) { | ||
console.error('Must have BEEHIIV_API_KEY defined') | ||
return Response.json( | ||
{ | ||
message: 'Oops! Something went wrong' | ||
}, | ||
{ | ||
status: 500 | ||
} | ||
) | ||
} | ||
|
||
const { email, referringUrl } = await request.json() | ||
if (!email) { | ||
return Response.json( | ||
{ | ||
message: 'Please submit a valid email' | ||
}, | ||
{ | ||
status: 400 | ||
} | ||
) | ||
} | ||
const url = new URL(referringUrl) | ||
// https://developers.beehiiv.com/docs/v2/1a77a563675ee-create | ||
const response = await fetch( | ||
`https://api.beehiiv.com/v2/publications/${publicationId}/subscriptions`, | ||
{ | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Bearer ${apiKey}`, | ||
'Content-Type': 'application/json', | ||
Accept: 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
email, | ||
reactivate_existing: true, | ||
send_welcome_email: true, | ||
utm_source: url.searchParams.get('ref') || url.host, | ||
referring_site: referringUrl, | ||
utm_medium: url.host | ||
}) | ||
} | ||
) | ||
if (!response.ok) { | ||
console.error(await response.json()) | ||
return Response.json( | ||
{ | ||
message: 'Oops! Something went wrong.' | ||
}, | ||
{ | ||
status: 500 | ||
} | ||
) | ||
} else { | ||
return Response.json({ | ||
message: "You've been subscribed!" | ||
}) | ||
} | ||
} |
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,70 @@ | ||
'use client' | ||
|
||
import { useState } from 'react' | ||
|
||
interface FormState { | ||
ok: boolean | ||
message: string | ||
} | ||
|
||
const initialState: FormState = { | ||
ok: true, | ||
message: '' | ||
} | ||
|
||
export function SubscribeForm(): JSX.Element { | ||
const [formState, setFormState] = useState(initialState) | ||
return ( | ||
<form | ||
className="card mx-auto flex flex-col gap-4 bg-surface-base-elevation-100 text-on-surface-base" | ||
onSubmit={async e => { | ||
e.preventDefault() | ||
const email = e.currentTarget.email.value | ||
const res = await fetch('/api/subscribe', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ email, referringUrl: window.location.href }) | ||
}) | ||
const { message } = await res.json() | ||
setFormState({ | ||
ok: res.ok, | ||
message | ||
}) | ||
}} | ||
> | ||
<h2 className="text-xl"> | ||
Subscribe to receive more like this in your inbox. | ||
</h2> | ||
<p className="text-on-surface-base-muted"> | ||
No spam <span className="italic">ever.</span> | ||
</p> | ||
<div className="mx-auto flex gap-4"> | ||
<label htmlFor="email" className="sr-only"> | ||
</label> | ||
<input | ||
required | ||
type="text" | ||
name="email" | ||
className="overflow-hidden rounded-md border border-borderColor bg-surface-base px-3 py-2 text-left text-sm shadow-sm focus:border-borderColor-focus focus:outline-none focus:ring-1 focus:ring-ringColor-focus" | ||
placeholder="Enter your email" | ||
/> | ||
<button className="rounded-md bg-accent-400 px-3 py-2 text-sm text-on-surface-accent shadow-sm hover:bg-surface-accent-hover focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-surface-accent"> | ||
Subscribe | ||
</button> | ||
</div> | ||
{formState.message && ( | ||
<p | ||
aria-live="polite" | ||
className={`text-sm ${ | ||
formState.ok === true ? 'text-success' : 'text-danger' | ||
}`} | ||
> | ||
{formState.message} | ||
</p> | ||
)} | ||
</form> | ||
) | ||
} |
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
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