Switch newsletter form to SvelteKit Form Actions

- remove api endpoint and use latest SK form actions
- create reusable server page function for form handling
- handling the errors in the server function
This commit is contained in:
2022-10-16 00:11:30 +02:00
parent b3616f9cff
commit 993dc90739
9 changed files with 142 additions and 75 deletions

View File

@@ -1,8 +1,13 @@
import { error } from '@sveltejs/kit'
import type { PageServerLoad } from './$types'
import type { Actions, PageServerLoad } from './$types'
import { fetchAPI } from '$utils/api'
import { getRandomItems } from '$utils/functions'
import subscribe from '$utils/forms/subscribe'
/**
* Page Data
*/
export const load: PageServerLoad = async ({ setHeaders }) => {
try {
// Get total of published photos
@@ -52,4 +57,13 @@ export const load: PageServerLoad = async ({ setHeaders }) => {
} catch (err) {
throw error(500, err.message)
}
}
/**
* Form Data
*/
export const actions: Actions = {
// Form newsletter subscription
subscribe,
}

View File

@@ -1,9 +1,13 @@
import { error } from '@sveltejs/kit'
import type { PageServerLoad } from './$types'
import type { Actions, PageServerLoad } from './$types'
import { PUBLIC_LIST_AMOUNT } from '$env/static/public'
import { fetchAPI, photoFields } from '$utils/api'
import subscribe from '$utils/forms/subscribe'
/**
* Page Data
*/
export const load: PageServerLoad = async ({ params, setHeaders }) => {
try {
const { location: slug } = params
@@ -88,4 +92,13 @@ export const load: PageServerLoad = async ({ params, setHeaders }) => {
} catch (err) {
throw error(500, err.message)
}
}
/**
* Form Data
*/
export const actions: Actions = {
// Form newsletter subscription
subscribe,
}

View File

@@ -1,30 +0,0 @@
import { error } from '@sveltejs/kit'
import type { RequestHandler } from './$types'
import { NEWSLETTER_API_TOKEN, NEWSLETTER_LIST_ID } from '$env/static/private'
export const POST: RequestHandler = async ({ request }) => {
try {
const body = await request.text()
if (body) {
const req = await fetch(`https://emailoctopus.com/api/1.6/lists/${NEWSLETTER_LIST_ID}/contacts`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
api_key: NEWSLETTER_API_TOKEN,
email_address: body,
})
})
const res = await req.json()
if (res) {
return new Response(JSON.stringify({
code: res.error ? res.error.code : res.status
}))
}
}
} catch (err) {
throw error(403, err.message)
}
}

View File

@@ -0,0 +1,8 @@
import type { Actions } from './$types'
import subscribe from '$utils/forms/subscribe'
export const actions: Actions = {
// Form newsletter subscription
subscribe,
}

View File

@@ -1,9 +1,13 @@
import { error } from '@sveltejs/kit'
import type { PageServerLoad } from './$types'
import type { Actions, PageServerLoad } from './$types'
import { fetchAPI } from '$utils/api'
import { PUBLIC_FILTERS_DEFAULT_COUNTRY, PUBLIC_FILTERS_DEFAULT_SORT, PUBLIC_GRID_AMOUNT } from '$env/static/public'
import subscribe from '$utils/forms/subscribe'
/**
* Page Data
*/
export const load: PageServerLoad = async ({ url, setHeaders }) => {
try {
// Query parameters
@@ -85,4 +89,13 @@ export const load: PageServerLoad = async ({ url, setHeaders }) => {
} catch (err) {
throw error(500, err.message)
}
}
/**
* Form Data
*/
export const actions: Actions = {
// Form newsletter subscription
subscribe,
}

View File

@@ -1,7 +1,12 @@
import { error } from '@sveltejs/kit'
import type { PageServerLoad } from './$types'
import type { Actions, PageServerLoad } from './$types'
import { fetchAPI } from '$utils/api'
import subscribe from '$utils/forms/subscribe'
/**
* Page Data
*/
export const load: PageServerLoad = async ({ setHeaders }) => {
try {
const res = await fetchAPI(`query {
@@ -35,4 +40,13 @@ export const load: PageServerLoad = async ({ setHeaders }) => {
} catch (err) {
throw error(500, err.message)
}
}
/**
* Form Data
*/
export const actions: Actions = {
// Form newsletter subscription
subscribe,
}