Page-filled form fields, links to entries, entry JSON-LD, R2-optional dev
Forms: `values` on RenderForm / BuilderForm / useBuilderForm fills fields the
page knows (chosen product variant, category) — read-only <output>, in the
SSR HTML, sent and validated like any field. prepareBuilderForm (/rsc) is the
server half of RenderForm for client components whose values change in the
browser; publicForm is exported so projects stop copying it.
Links: buildLinkField({ relationTo: ['pages', 'categories', …] }) — a
polymorphic relation; resolveLinkHref builds /{locale}/{archive}/{entry} from
`archives`, which getLinkOptions in createContentHelpers fills from Site
Settings.
SEO: pageJsonLd.entries — the main JSON-LD node per content collection
instead of BlogPosting; buildProductJsonLd emits an Offer only for a real
price from the panel.
Pages: createPageRoute / createNotFoundPage pass isFirst to blocks, so the
first section carries the page's only h1.
Fixes: ipalEnvRules({ storage: true }) requires R2_* in production only
(development: optional, all or none); buildImagesConfig allows the app's own
media on localhost outside production.
Docs, CHANGELOG, PROGRAMISTA §6 (34–37), ipal-form and ipal-upgrade skills.
Tests: builderForm, links, drafts (entries + archives), seo, media, env.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
46d30d4ecd
commit
f809526f1d
@@ -18,7 +18,7 @@ import { useBuilderForm } from './useBuilderForm.js'
|
||||
export type BuilderFormClassNames = Partial<
|
||||
Record<
|
||||
| 'actions' | 'back' | 'checkbox' | 'checkboxLabel' | 'choice' | 'choiceInput' | 'choiceLabel' | 'choices'
|
||||
| 'error' | 'field' | 'fieldFull' | 'fieldHalf' | 'fields' | 'form' | 'formError' | 'input' | 'label'
|
||||
| 'error' | 'field' | 'fieldFull' | 'fieldHalf' | 'fields' | 'fixed' | 'form' | 'formError' | 'input' | 'label'
|
||||
| 'legend' | 'message' | 'next' | 'progress' | 'required' | 'select' | 'step' | 'stepDescription'
|
||||
| 'stepTitle' | 'submit' | 'success' | 'textarea' | 'turnstile',
|
||||
string
|
||||
@@ -51,6 +51,9 @@ export type BuilderFormProps = {
|
||||
turnstileTheme?: 'auto' | 'dark' | 'light'
|
||||
} & UseBuilderFormOptions
|
||||
|
||||
/** A page-supplied value (always a string from `values`) as text. */
|
||||
const fixedText = (value: unknown) => (typeof value === 'string' ? value : '')
|
||||
|
||||
const cx = (...c: (false | null | string | undefined)[]) => c.filter(Boolean).join(' ') || undefined
|
||||
|
||||
/**
|
||||
@@ -108,6 +111,18 @@ export function BuilderForm({
|
||||
|
||||
const defaultField = (field: BuilderField, index: number): ReactNode => {
|
||||
if (field.blockType === 'message') {return <div className={cn.message}>{messages[index]}</div>}
|
||||
if (f.isFixed(field)) {
|
||||
// Filled by the page (`values`): visible, not editable, sent with the form.
|
||||
const id = f.fieldId(field)
|
||||
return (
|
||||
<>
|
||||
<span className={cn.label} id={`${id}-label`}>{field.label}</span>
|
||||
<output aria-labelledby={`${id}-label`} className={cn.fixed} data-ipal="form-fixed" id={id}>
|
||||
{fixedText(f.values[field.name ?? ''])}
|
||||
</output>
|
||||
</>
|
||||
)
|
||||
}
|
||||
switch (field.blockType) {
|
||||
case 'checkbox': {
|
||||
const props = f.getInputProps(field)
|
||||
|
||||
@@ -38,11 +38,13 @@ export type RenderFormProps = {
|
||||
/** Notifications → form texts in this locale (getNotificationTexts(...).form). */
|
||||
texts: BuilderFormTexts
|
||||
turnstileTheme?: 'auto' | 'dark' | 'light'
|
||||
/** 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. */
|
||||
function publicForm(form: BuilderFormDoc): BuilderFormDoc {
|
||||
export function publicForm(form: BuilderFormDoc): BuilderFormDoc {
|
||||
return {
|
||||
id: form.id,
|
||||
analytics: form.analytics,
|
||||
@@ -54,27 +56,42 @@ function publicForm(form: BuilderFormDoc): BuilderFormDoc {
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
messages: Record<number, ReactNode>
|
||||
redirectHref: null | string
|
||||
}
|
||||
|
||||
/**
|
||||
* A panel form inside a block, in one line — server part of BuilderForm:
|
||||
* 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):
|
||||
*
|
||||
* <RenderForm form={form} action={sendForm} locale={locale} texts={formTexts} classNames={formClasses} />
|
||||
* // server
|
||||
* const prepared = prepareBuilderForm({ form, action: sendForm, locale })
|
||||
* <ProductConfigurator form={prepared} texts={formTexts} />
|
||||
* // client
|
||||
* <BuilderForm {...form} texts={texts} classNames={formClasses} values={{ subject: chosenVariant }} />
|
||||
*
|
||||
* 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).
|
||||
* Returns null for an unpopulated form (an id or nothing).
|
||||
*/
|
||||
export function RenderForm({
|
||||
export function prepareBuilderForm({
|
||||
action,
|
||||
classNames,
|
||||
converters,
|
||||
form,
|
||||
linkOptions,
|
||||
locale,
|
||||
requiredMark,
|
||||
richTextClassName,
|
||||
texts,
|
||||
turnstileTheme,
|
||||
}: RenderFormProps) {
|
||||
}: PrepareBuilderFormArgs): null | PreparedBuilderForm {
|
||||
if (!form || typeof form !== 'object') {return null} // never a placeholder form
|
||||
|
||||
const rich = (data: unknown) =>
|
||||
@@ -93,17 +110,51 @@ export function RenderForm({
|
||||
: safeHref(form.redirect?.url)
|
||||
: null
|
||||
|
||||
return {
|
||||
action: action.bind(null, String(form.id), locale),
|
||||
confirmation: rich(form.confirmationMessage),
|
||||
form: publicForm(form),
|
||||
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,
|
||||
linkOptions,
|
||||
locale,
|
||||
requiredMark,
|
||||
richTextClassName,
|
||||
texts,
|
||||
turnstileTheme,
|
||||
values,
|
||||
}: RenderFormProps) {
|
||||
const prepared = prepareBuilderForm({ action, converters, form, linkOptions, locale, richTextClassName })
|
||||
if (!prepared) {return null}
|
||||
return (
|
||||
<BuilderForm
|
||||
action={action.bind(null, String(form.id), locale)}
|
||||
{...prepared}
|
||||
classNames={classNames}
|
||||
confirmation={rich(form.confirmationMessage)}
|
||||
form={publicForm(form)}
|
||||
messages={messages}
|
||||
redirectHref={redirectHref}
|
||||
requiredMark={requiredMark}
|
||||
texts={texts}
|
||||
turnstileTheme={turnstileTheme}
|
||||
values={values}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -23,6 +23,12 @@ export type UseBuilderFormOptions = {
|
||||
/** Redirect target resolved on the server (RenderForm). Falls back to the form's custom URL. */
|
||||
redirectHref?: null | string
|
||||
texts: BuilderFormTexts
|
||||
/**
|
||||
* Fields the page fills, by name (e.g. `{ subject: 'Ring · white gold · 0.70 ct' }`):
|
||||
* kept in sync when the prop changes, read-only in BuilderForm, sent like any
|
||||
* field. Names not in the form are ignored (the server rejects unknown fields).
|
||||
*/
|
||||
values?: Record<string, string>
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -40,7 +46,7 @@ export type UseBuilderFormOptions = {
|
||||
* <button type="submit">{f.isLast ? form.submitButtonLabel : texts.next}</button>
|
||||
* </form>
|
||||
*/
|
||||
export function useBuilderForm({ action, form, navigate, onSuccess, redirectHref, texts }: UseBuilderFormOptions) {
|
||||
export function useBuilderForm({ action, form, navigate, onSuccess, redirectHref, texts, values: pageValues }: UseBuilderFormOptions) {
|
||||
const fields = useMemo(() => form.fields ?? [], [form.fields])
|
||||
// Fields behind a failed "Show only when" are neither shown, validated nor sent.
|
||||
const visibleInputs = useCallback((values: Record<string, unknown>) => inputFields(visibleFields(fields, values)), [fields])
|
||||
@@ -65,7 +71,11 @@ export function useBuilderForm({ action, form, navigate, onSuccess, redirectHref
|
||||
|
||||
const f = useForm<Record<string, unknown>>({
|
||||
errorFallback: texts.error,
|
||||
initialValues: initialBuilderValues(fields),
|
||||
// Page values are in the first render too — server HTML shows them before hydration.
|
||||
initialValues: {
|
||||
...initialBuilderValues(fields),
|
||||
...Object.fromEntries(Object.entries(pageValues ?? {}).filter(([name]) => fields.some((field) => field.name === name))),
|
||||
},
|
||||
onSuccess: (_result, values) => {
|
||||
formAnalyticsOnSuccess(form.analytics)(_result, values)
|
||||
onSuccess?.(values)
|
||||
@@ -78,7 +88,19 @@ export function useBuilderForm({ action, form, navigate, onSuccess, redirectHref
|
||||
submitAction,
|
||||
validate,
|
||||
})
|
||||
const { setErrors, setTurnstileToken } = f
|
||||
const { setErrors, setField, setTurnstileToken } = f
|
||||
|
||||
// Page-supplied values follow the prop (a configurator changes them in the browser).
|
||||
const fixedNames = useMemo(
|
||||
() => new Set(Object.keys(pageValues ?? {}).filter((name) => fields.some((field) => field.name === name))),
|
||||
[pageValues, fields],
|
||||
)
|
||||
const fixedKey = JSON.stringify(pageValues ?? {})
|
||||
useEffect(() => {
|
||||
for (const name of fixedNames) {setField(name, pageValues?.[name] ?? '')}
|
||||
}, [fixedKey, fixedNames, setField])
|
||||
/** True for a field whose value comes from the page (`values`) — show it, don't let the visitor edit it. */
|
||||
const isFixed = useCallback((field: BuilderField) => Boolean(field.name && fixedNames.has(field.name)), [fixedNames])
|
||||
// Steps follow the answers: a Step whose condition fails drops out, later steps renumber.
|
||||
const steps = useMemo(() => {
|
||||
const shown = new Set(visibleFields(fields, f.values))
|
||||
@@ -258,6 +280,7 @@ export function useBuilderForm({ action, form, navigate, onSuccess, redirectHref
|
||||
getTextareaProps,
|
||||
goTo,
|
||||
isFirst,
|
||||
isFixed,
|
||||
isLast,
|
||||
next,
|
||||
/** Message the server returned on success (Notifications → success). */
|
||||
|
||||
Reference in New Issue
Block a user