23 lines
1.2 KiB
TypeScript
23 lines
1.2 KiB
TypeScript
// src/app/(frontend)/[locale]/actions.ts — ONE server action for every builder form.
|
|
'use server'
|
|
import { headers } from 'next/headers'
|
|
import { submitForm } from '@intecion/ipal-kit/server'
|
|
import { isHoneypotTripped, HONEYPOT_FIELD, getNotificationTexts, resolveFormMessage } from '@intecion/ipal-kit'
|
|
import { getCachedPayload } from '@/lib/content'
|
|
|
|
export async function sendForm(
|
|
formId: string,
|
|
locale: string,
|
|
values: Record<string, unknown>,
|
|
turnstileToken?: string,
|
|
): Promise<{ success: boolean; message: string }> {
|
|
const payload = await getCachedPayload()
|
|
const texts = (await getNotificationTexts({ payload, locale })).form
|
|
if (isHoneypotTripped(values)) return { success: true, message: texts.success } // bot: fake success, nothing stored
|
|
const { [HONEYPOT_FIELD]: _hp, ...data } = values
|
|
const h = await headers() // fine in a server action — never in the page tree (breaks SSG)
|
|
const ip = h.get('cf-connecting-ip') ?? h.get('x-forwarded-for')?.split(',')[0]?.trim() ?? undefined
|
|
const result = await submitForm({ payload, formId, data, turnstileToken, ip })
|
|
return { success: result.success, message: resolveFormMessage(result, texts) }
|
|
}
|