portfolio/app/actions/subscribe.ts
Ryan Freeman af927a4331
All checks were successful
Build And Publish / BuildAndPublish (push) Successful in 3m10s
Add new subscriber notification
2025-04-22 21:35:33 +01:00

44 lines
1.1 KiB
TypeScript

'use server'
import {z} from 'zod'
import {addSubscriber} from '@/lib/listmonk'
import {sendNotification} from '@/lib/ntfy'
const notificationBody = (email: FormDataEntryValue | null) => {
return {
topic: 'portfolio',
message: `${email} has subscribed to your newsletter.`,
title: 'New subscriber',
tags: ['rocket']
}
}
export async function subscribe(prevState: { message: string }, formData: FormData) {
const schema = z.object({
email: z.string().email()
})
const {email} = {
email: formData.get('email')
}
const parse = schema.safeParse({email})
if (!parse.success) {
return {message: 'Error, your email address is invalid.'}
}
try {
await addSubscriber(email)
} catch (error) {
let errorMessage
if (error instanceof Error) errorMessage = error.message
else errorMessage = String(error)
console.error(error)
return {message: errorMessage}
}
await sendNotification(notificationBody(email))
return {message: 'Subscribed'}
}