178 lines
6.9 KiB
TypeScript
178 lines
6.9 KiB
TypeScript
import type { SerializedEditorState } from '@payloadcms/richtext-lexical/lexical'
|
|
import type { JSXConvertersFunction } from '@payloadcms/richtext-lexical/react'
|
|
import type { ComponentType, ReactNode } from 'react'
|
|
|
|
import type { ResolveLinkHrefOptions } from '../links/resolveLinkHref.js'
|
|
import type { TurnstileTheme } from '../turnstile/Turnstile.js'
|
|
import type { BuilderFormClassNames, BuilderFormProps } from './BuilderForm.js'
|
|
import type { BuilderField, BuilderFormDoc, BuilderFormTexts } from './builderTypes.js'
|
|
import type { SubmitActionResult } from './createSubmitAction.js'
|
|
|
|
import { resolveLinkHref, safeHref } from '../links/resolveLinkHref.js'
|
|
import { RenderRichText } from '../richtext/RenderRichText.js'
|
|
import { LazyBuilderForm } from './LazyBuilderForm.js'
|
|
|
|
/** The action from createSubmitAction — `sendForm(formId, locale, values, token?)`. */
|
|
export type SendFormAction = (
|
|
formId: string,
|
|
locale: string,
|
|
values: Record<string, unknown>,
|
|
turnstileToken?: string,
|
|
) => Promise<SubmitActionResult>
|
|
|
|
export type RenderFormProps = {
|
|
/** The project's `sendForm` server action (createSubmitAction). */
|
|
action: SendFormAction
|
|
classNames?: BuilderFormClassNames
|
|
/** Rich text styling for message blocks and the confirmation (same as RenderRichText). */
|
|
converters?: JSXConvertersFunction
|
|
/**
|
|
* The client form component. Default: the lazy one (1.18.0) — its code loads only on pages with a form, the HTML
|
|
* is complete either way. `Form={BuilderForm}` from `@intecion/ipal-kit/client/eager` = in every page's bundle.
|
|
*/
|
|
Form?: ComponentType<BuilderFormProps>
|
|
/** The `form` relationship of the block, populated (page query depth ≥ 1). An id or null renders nothing. */
|
|
form: BuilderFormDoc | null | number | string | undefined
|
|
/**
|
|
* For redirect forms pointing at a page: `{ config: i18nConfig, homeSlug }`
|
|
* (locale is added). Without it only a custom URL redirect works.
|
|
*/
|
|
linkOptions?: Omit<ResolveLinkHrefOptions, 'locale'>
|
|
locale: string
|
|
requiredMark?: ReactNode
|
|
richTextClassName?: string
|
|
/** Notifications → form texts in this locale (getNotificationTexts(...).form). */
|
|
texts: BuilderFormTexts
|
|
/** Widget theme. Default 'page' — follows the site's light/dark theme. */
|
|
/** Space kept for the Turnstile widget before it is drawn (default 73 px; 0 for an invisible widget). */
|
|
turnstileReserveHeight?: number
|
|
turnstileTheme?: TurnstileTheme
|
|
/** Values of fields the page already knows, by field name — read-only for the visitor, sent with the form. */
|
|
values?: Record<string, string>
|
|
}
|
|
|
|
/** Only what the browser needs. The Local API reads with overrideAccess, so the
|
|
* document still carries Emails (recipient addresses) — those never leave the server. */
|
|
export function publicForm(form: BuilderFormDoc): BuilderFormDoc {
|
|
return {
|
|
id: form.id,
|
|
analytics: form.analytics,
|
|
confirmationType: form.confirmationType,
|
|
fields: (form.fields ?? []).map(({ message: _message, ...field }: BuilderField) => field),
|
|
fileSubmitUrl: form.fileSubmitUrl,
|
|
redirect: form.redirect?.type === 'custom' || form.redirect?.url ? { url: safeHref(form.redirect?.url) } : null,
|
|
submitButtonLabel: form.submitButtonLabel,
|
|
title: form.title,
|
|
}
|
|
}
|
|
|
|
export type PrepareBuilderFormArgs = Pick<
|
|
RenderFormProps,
|
|
'action' | 'converters' | 'form' | 'linkOptions' | 'locale' | 'richTextClassName'
|
|
>
|
|
|
|
/** What the browser gets for a panel form — spread it into <BuilderForm> (plus texts, classNames, values). */
|
|
export type PreparedBuilderForm = {
|
|
action: (values: Record<string, unknown>, turnstileToken?: string) => Promise<SubmitActionResult>
|
|
confirmation: ReactNode
|
|
form: BuilderFormDoc
|
|
/** The page language — sent with a submission that carries files. */
|
|
locale: string
|
|
messages: Record<number, ReactNode>
|
|
redirectHref: null | string
|
|
}
|
|
|
|
/**
|
|
* The server half of RenderForm without the markup: the public part of the form,
|
|
* rendered message blocks and confirmation, the action bound to form + locale,
|
|
* the redirect target. For a client component that renders <BuilderForm> itself —
|
|
* e.g. with `values` that change in the browser (a product configurator):
|
|
*
|
|
* // server
|
|
* const prepared = prepareBuilderForm({ form, action: sendForm, locale })
|
|
* <ProductConfigurator form={prepared} texts={formTexts} />
|
|
* // client
|
|
* <BuilderForm {...form} texts={texts} classNames={formClasses} values={{ subject: chosenVariant }} />
|
|
*
|
|
* Returns null for an unpopulated form (an id or nothing).
|
|
*/
|
|
export function prepareBuilderForm({
|
|
action,
|
|
converters,
|
|
form,
|
|
linkOptions,
|
|
locale,
|
|
richTextClassName,
|
|
}: PrepareBuilderFormArgs): null | PreparedBuilderForm {
|
|
if (!form || typeof form !== 'object') {return null} // never a placeholder form
|
|
|
|
const rich = (data: unknown) =>
|
|
data ? <RenderRichText className={richTextClassName} converters={converters} data={data as SerializedEditorState} /> : null
|
|
|
|
const messages: Record<number, ReactNode> = {}
|
|
form.fields?.forEach((field, i) => {
|
|
if (field.blockType === 'message' && field.message) {messages[i] = rich(field.message)}
|
|
})
|
|
|
|
const page = form.redirect?.type === 'reference' ? (form.redirect.reference as { value?: unknown } | null)?.value : undefined
|
|
const redirectHref =
|
|
form.confirmationType === 'redirect'
|
|
? page && linkOptions
|
|
? resolveLinkHref({ type: 'internal', page }, { ...linkOptions, locale })
|
|
: safeHref(form.redirect?.url)
|
|
: null
|
|
|
|
return {
|
|
action: action.bind(null, String(form.id), locale),
|
|
confirmation: rich(form.confirmationMessage),
|
|
form: publicForm(form),
|
|
locale,
|
|
messages,
|
|
redirectHref,
|
|
}
|
|
}
|
|
|
|
/**
|
|
* A panel form inside a block, in one line — server part of BuilderForm:
|
|
*
|
|
* <RenderForm form={form} action={sendForm} locale={locale} texts={formTexts} classNames={formClasses} />
|
|
*
|
|
* Renders `message` blocks and the confirmation with RenderRichText, binds the
|
|
* action to the form and locale, resolves the redirect target, and passes the
|
|
* client only the public part of the form (never the Emails tab).
|
|
*
|
|
* `values` fill fields the page already knows (`{ subject: 'Brooches' }`): shown
|
|
* read-only and sent with the submission. Values that change in the browser:
|
|
* prepareBuilderForm + <BuilderForm values>.
|
|
*/
|
|
export function RenderForm({
|
|
action,
|
|
classNames,
|
|
converters,
|
|
form,
|
|
Form = LazyBuilderForm,
|
|
linkOptions,
|
|
locale,
|
|
requiredMark,
|
|
richTextClassName,
|
|
texts,
|
|
turnstileReserveHeight,
|
|
turnstileTheme,
|
|
values,
|
|
}: RenderFormProps) {
|
|
const prepared = prepareBuilderForm({ action, converters, form, linkOptions, locale, richTextClassName })
|
|
if (!prepared) {return null}
|
|
// the form's code is fetched only on pages that have a form (the HTML stays complete): LazyBuilderForm.tsx
|
|
return (
|
|
<Form
|
|
{...prepared}
|
|
classNames={classNames}
|
|
requiredMark={requiredMark}
|
|
texts={texts}
|
|
turnstileReserveHeight={turnstileReserveHeight}
|
|
turnstileTheme={turnstileTheme}
|
|
values={values}
|
|
/>
|
|
)
|
|
}
|