mirror of
https://github.com/r-freeman/portfolio.git
synced 2025-04-24 12:04:36 +00:00
All checks were successful
Build And Publish / BuildAndPublish (push) Successful in 3m11s
32 lines
760 B
TypeScript
32 lines
760 B
TypeScript
'use server'
|
|
|
|
import {z} from 'zod'
|
|
import {addSubscriber} from '@/lib/listmonk'
|
|
|
|
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}
|
|
}
|
|
|
|
return {message: 'Subscribed'}
|
|
} |