✨ Implement newsletter subscription form using API
Switch from SendInBlue to EmailOctopus for newsletter. Now uses their API through the form to handle subscription.
This commit is contained in:
@@ -3,52 +3,101 @@
|
|||||||
</style>
|
</style>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { getContext } from 'svelte'
|
import { fly } from 'svelte/transition'
|
||||||
|
import { quartOut } from 'svelte/easing'
|
||||||
|
import { sendEvent } from '$utils/analytics'
|
||||||
// Components
|
// Components
|
||||||
import IconArrow from '$components/atoms/IconArrow.svelte'
|
import IconArrow from '$components/atoms/IconArrow.svelte'
|
||||||
import ButtonCircle from '$components/atoms/ButtonCircle.svelte'
|
import ButtonCircle from '$components/atoms/ButtonCircle.svelte'
|
||||||
|
|
||||||
export let past: boolean = false
|
export let past: boolean = false
|
||||||
|
|
||||||
const { settings: { newsletter_url }}: any = getContext('global')
|
|
||||||
|
|
||||||
let inputInFocus = false
|
let inputInFocus = false
|
||||||
|
let formStatus: string = null
|
||||||
|
let formMessageTimeout: ReturnType<typeof setTimeout> | number
|
||||||
|
const formMessages = {
|
||||||
|
PENDING: `Almost there! Please confirm your email address through the email you'll receive soon.`,
|
||||||
|
MEMBER_EXISTS_WITH_EMAIL_ADDRESS: `Uh oh! This email address is already subscribed to the newsletter.`,
|
||||||
|
INVALID_EMAIL: `Woops. This email doesn't seem to be valid.`,
|
||||||
|
}
|
||||||
|
|
||||||
// Toggle input focus
|
// Toggle input focus
|
||||||
const toggleFocus = () => inputInFocus = !inputInFocus
|
const toggleFocus = () => inputInFocus = !inputInFocus
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Subscription form handling
|
||||||
|
*/
|
||||||
|
const formSubmission = async ({ target }) => {
|
||||||
|
const formData = new FormData(target)
|
||||||
|
const formValues = Object.fromEntries(formData)
|
||||||
|
const { email } = formValues
|
||||||
|
|
||||||
|
if (email && email.match('/^([\w.-])+@([\w.-])+\.([a-zA-Z])+/')) {
|
||||||
|
const req = await fetch('/api/newsletter', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'text/plain' },
|
||||||
|
body: email
|
||||||
|
})
|
||||||
|
|
||||||
|
const res = await req.json()
|
||||||
|
formStatus = res.code
|
||||||
|
|
||||||
|
if (res.code === 'PENDING') {
|
||||||
|
sendEvent({ action: 'newsletterSubscribe' })
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
formStatus = 'INVALID_EMAIL'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$: if (formStatus !== 'PENDING') {
|
||||||
|
clearTimeout(formMessageTimeout)
|
||||||
|
formMessageTimeout = setTimeout(() => formStatus = null, 3000)
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="newsletter-form">
|
<div class="newsletter-form">
|
||||||
<form method="POST" action={newsletter_url} id="sib-form" target="_blank">
|
{#if formStatus !== 'PENDING'}
|
||||||
<div class="newsletter-form__email" class:is-focused={inputInFocus}>
|
<form method="POST" on:submit|preventDefault={formSubmission}
|
||||||
<input type="email" placeholder="Your email address" name="EMAIL" id="SUB_EMAIL" required
|
out:fly={{ y: -8, easing: quartOut, duration: 600 }}
|
||||||
on:focus={toggleFocus}
|
>
|
||||||
on:blur={toggleFocus}
|
<div class="newsletter-form__email" class:is-focused={inputInFocus}>
|
||||||
>
|
<input type="email" placeholder="Your email address" name="email" id="newsletter_email" required
|
||||||
<ButtonCircle
|
on:focus={toggleFocus}
|
||||||
type="submit" form="sib-form"
|
on:blur={toggleFocus}
|
||||||
color="pink" size="small"
|
>
|
||||||
clone={true}
|
<ButtonCircle
|
||||||
label="Subscribe"
|
type="submit"
|
||||||
>
|
color="pink" size="small"
|
||||||
<IconArrow color="white" />
|
clone={true}
|
||||||
</ButtonCircle>
|
label="Subscribe"
|
||||||
|
>
|
||||||
|
<IconArrow color="white" />
|
||||||
|
</ButtonCircle>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="newsletter-form__bottom">
|
||||||
|
{#if past}
|
||||||
|
<a href="/subscribe" class="past-issues" sveltekit:noscroll sveltekit:prefetch>
|
||||||
|
<svg width="20" height="16" viewBox="0 0 20 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg" aria-label="Newsletter icon">
|
||||||
|
<path fill-rule="evenodd" clip-rule="evenodd" d="M18 2.346H2a.5.5 0 0 0-.5.5v11.102a.5.5 0 0 0 .5.5h16a.5.5 0 0 0 .5-.5V2.846a.5.5 0 0 0-.5-.5ZM2 .846a2 2 0 0 0-2 2v11.102a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2V2.846a2 2 0 0 0-2-2H2Zm13.75 4.25h-2v3h2v-3Zm-2-1a1 1 0 0 0-1 1v3a1 1 0 0 0 1 1h2a1 1 0 0 0 1-1v-3a1 1 0 0 0-1-1h-2ZM3.5 6.5a.5.5 0 0 0 0 1h6a.5.5 0 0 0 0-1h-6Zm.25 3a.5.5 0 0 1 .5-.5h6a.5.5 0 0 1 0 1h-6a.5.5 0 0 1-.5-.5Zm1.25 2a.5.5 0 0 0 0 1h6a.5.5 0 1 0 0-1H5Z" />
|
||||||
|
</svg>
|
||||||
|
<span>See past issues</span>
|
||||||
|
</a>
|
||||||
|
{/if}
|
||||||
|
<p>No spam, we promise!</p>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
{#if formStatus}
|
||||||
|
<div class="newsletter-form__message shadow-small"
|
||||||
|
class:is-error={formStatus !== 'PENDING'}
|
||||||
|
class:is-success={formStatus === 'PENDING'}
|
||||||
|
transition:fly={{ y: 8, easing: quartOut, duration: 600, delay: formStatus !== 'PENDING' ? 100 : 600 }}
|
||||||
|
>
|
||||||
|
<p class="text-xsmall">{formMessages[formStatus]}</p>
|
||||||
</div>
|
</div>
|
||||||
|
{/if}
|
||||||
<input type="text" name="email_address_check" value="" style="display: none;">
|
|
||||||
<input type="hidden" name="locale" value="en">
|
|
||||||
<input type="hidden" name="html_type" value="simple">
|
|
||||||
</form>
|
|
||||||
|
|
||||||
<div class="newsletter-form__bottom">
|
|
||||||
{#if past}
|
|
||||||
<a href="/subscribe" class="past-issues" sveltekit:noscroll sveltekit:prefetch>
|
|
||||||
<svg width="20" height="16" viewBox="0 0 20 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg" aria-label="Newsletter icon">
|
|
||||||
<path fill-rule="evenodd" clip-rule="evenodd" d="M18 2.346H2a.5.5 0 0 0-.5.5v11.102a.5.5 0 0 0 .5.5h16a.5.5 0 0 0 .5-.5V2.846a.5.5 0 0 0-.5-.5ZM2 .846a2 2 0 0 0-2 2v11.102a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2V2.846a2 2 0 0 0-2-2H2Zm13.75 4.25h-2v3h2v-3Zm-2-1a1 1 0 0 0-1 1v3a1 1 0 0 0 1 1h2a1 1 0 0 0 1-1v-3a1 1 0 0 0-1-1h-2ZM3.5 6.5a.5.5 0 0 0 0 1h6a.5.5 0 0 0 0-1h-6Zm.25 3a.5.5 0 0 1 .5-.5h6a.5.5 0 0 1 0 1h-6a.5.5 0 0 1-.5-.5Zm1.25 2a.5.5 0 0 0 0 1h6a.5.5 0 1 0 0-1H5Z" />
|
|
||||||
</svg>
|
|
||||||
<span>See past issues</span>
|
|
||||||
</a>
|
|
||||||
{/if}
|
|
||||||
<p>No spam, we promise!</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
<div class="newsletter newsletter--{theme} shadow-box-dark">
|
<div class="newsletter newsletter--{theme} shadow-box-dark">
|
||||||
<div class="newsletter__wrapper">
|
<div class="newsletter__wrapper">
|
||||||
<h3 class="title-medium">
|
<h3 class="title-medium">
|
||||||
<label for="SUB_EMAIL">{newsletter_subtitle}</label>
|
<label for="newsletter_email">{newsletter_subtitle}</label>
|
||||||
</h3>
|
</h3>
|
||||||
<p class="text-small">{newsletter_text}</p>
|
<p class="text-small">{newsletter_text}</p>
|
||||||
|
|
||||||
|
|||||||
@@ -112,7 +112,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="subscribe">
|
<div class="subscribe">
|
||||||
<label class="subscribe__text" for="SUB_EMAIL">Subscribe to be notified when new posters become available</label>
|
<label class="subscribe__text" for="newsletter_email">Subscribe to be notified when new posters become available</label>
|
||||||
<EmailForm />
|
<EmailForm />
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|||||||
44
src/routes/api/newsletter.ts
Normal file
44
src/routes/api/newsletter.ts
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
import type { RequestEvent, RequestHandlerOutput } from '@sveltejs/kit'
|
||||||
|
|
||||||
|
|
||||||
|
// Block GET requests
|
||||||
|
export async function get({}: RequestEvent): Promise<RequestHandlerOutput> {
|
||||||
|
return {
|
||||||
|
status: 403,
|
||||||
|
body: 'nope!'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* POST request
|
||||||
|
*/
|
||||||
|
export async function post ({ request }: RequestEvent): Promise<RequestHandlerOutput> {
|
||||||
|
const body = await request.text()
|
||||||
|
|
||||||
|
if (body) {
|
||||||
|
const req = await fetch(`https://emailoctopus.com/api/1.6/lists/${import.meta.env.VITE_NEWSLETTER_LIST_ID}/contacts`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({
|
||||||
|
api_key: import.meta.env.VITE_NEWSLETTER_API_TOKEN,
|
||||||
|
email_address: body,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
const res = await req.json()
|
||||||
|
|
||||||
|
if (res && res.email_address && res.status === 'PENDING') {
|
||||||
|
return {
|
||||||
|
status: 200,
|
||||||
|
body: {
|
||||||
|
code: 'PENDING'
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
status: 403,
|
||||||
|
body: res.error,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -127,6 +127,18 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// XSmall
|
||||||
|
.text-xsmall {
|
||||||
|
font-size: rem(16px);
|
||||||
|
font-weight: 400;
|
||||||
|
line-height: 1.4;
|
||||||
|
|
||||||
|
@include bp (sm) {
|
||||||
|
font-size: rem(18px);
|
||||||
|
line-height: 1.5;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Information
|
// Information
|
||||||
.text-info {
|
.text-info {
|
||||||
font-size: rem(14px);
|
font-size: rem(14px);
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
// Email Form
|
// Email Form
|
||||||
.newsletter-form {
|
.newsletter-form {
|
||||||
|
position: relative;
|
||||||
|
|
||||||
&__email {
|
&__email {
|
||||||
position: relative;
|
position: relative;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
@@ -23,7 +25,7 @@
|
|||||||
font-size: rem(16px);
|
font-size: rem(16px);
|
||||||
font-family: $font-sans;
|
font-family: $font-sans;
|
||||||
font-weight: 300;
|
font-weight: 300;
|
||||||
color: #333;
|
color: $color-tertiary;
|
||||||
background: none;
|
background: none;
|
||||||
border: none;
|
border: none;
|
||||||
|
|
||||||
@@ -99,4 +101,24 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Message
|
||||||
|
&__message {
|
||||||
|
padding: 24px;
|
||||||
|
text-align: center;
|
||||||
|
border-radius: 8px;
|
||||||
|
|
||||||
|
&.is-error {
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
width: 100%;
|
||||||
|
color: #fff;
|
||||||
|
background: $color-secondary;
|
||||||
|
}
|
||||||
|
&.is-success {
|
||||||
|
color: $color-text;
|
||||||
|
background: $color-tertiary;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -46,6 +46,21 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Form input
|
||||||
|
:global(.newsletter-form__email) {
|
||||||
|
:global(input) {
|
||||||
|
color: $color-text;
|
||||||
|
}
|
||||||
|
:global(input::placeholder) {
|
||||||
|
color: $color-gray;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Form message
|
||||||
|
:global(.newsletter-form__message.is-success) {
|
||||||
|
color: #fff;
|
||||||
|
background: $color-primary-tertiary20;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Variants
|
** Variants
|
||||||
@@ -56,10 +71,6 @@
|
|||||||
|
|
||||||
:global(.newsletter-form__email) {
|
:global(.newsletter-form__email) {
|
||||||
background: $color-tertiary;
|
background: $color-tertiary;
|
||||||
|
|
||||||
:global(input::placeholder) {
|
|
||||||
color: $color-gray;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Light
|
// Light
|
||||||
|
|||||||
@@ -13,11 +13,6 @@
|
|||||||
margin-bottom: 120px;
|
margin-bottom: 120px;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
:global(.newsletter-form__email) {
|
|
||||||
:global(input) {
|
|
||||||
color: #fff;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
:global(.newsletter-form__bottom) {
|
:global(.newsletter-form__bottom) {
|
||||||
:global(p) {
|
:global(p) {
|
||||||
color: rgba($color-tertiary, 0.6);
|
color: rgba($color-tertiary, 0.6);
|
||||||
|
|||||||
Reference in New Issue
Block a user