Files
ipal-kit/docs/notifications.md
T

5.0 KiB

notifications

Notification texts (action results) configured in the admin panel, per language, with a fallback. Today it covers form result messages (submitForm), with room for future contexts. The Notifications global is always built.

Principle

The plugin returns a result CODE (submitForm returns reason), not text. This module maps code → text from the panel (localized), with an English fallback per field. The frontend gets a ready string and styles it as it likes (toast, inline, banner). This way no message is hardcoded — everything goes through the panel.

Config

No options — the Notifications global (slug notifications, panel group Settings) is always built. The editor manages the texts in the panel (Notifications tab), grouped per context. Group form: success, error, rateLimited, turnstile, validation, consent, notFound and the multi-step form buttons: next ("Next"), back ("Back"), stepOf ("Step {current} of {total}"). Every empty field → the built-in default in the page language (notificationFallback(locale): NOTIFICATION_FALLBACK_PL for pl, else NOTIFICATION_FALLBACK — EN; all exported from @intecion/ipal-kit). In validation, {field} is the field's label in the page language. For forms: getFormTexts(locale) from lib/content = the form group in the given language.

Group lightbox (only with ipalKit({ lightbox: true }) — adds columns, run a migration on Postgres): open ("Enlarge image"), dialog ("Image viewer"), previous, next, close, counter ("{current} / {total}"). getNotificationTexts(...).lightbox always returns the full set (fallback LIGHTBOX_FALLBACK), and getSiteShell(locale).lightboxTexts carries it to <SiteProviders lightbox>. Details: lightbox.md.

Helper — getNotificationTexts

Reads the texts from the global per language, with a fallback per field. The counterpart of getConsentTexts:

import { getNotificationTexts } from '@intecion/ipal-kit'

const notifications = await getNotificationTexts({ payload, locale })   // in lib/content: getFormTexts(locale) = .form
// notifications.form.error, notifications.form.success, ..., notifications.lightbox.close

Mapping the result — resolveFormMessage

The bridge between submitForm and the UI: takes the result and the texts, returns one message. Interpolates {field} in the validation text. NEVER shows a raw exception (error → a generic text, not the backend error message).

import { resolveFormMessage } from '@intecion/ipal-kit'

const result = await submitFormAction(...)
if (!result.success) {
  setError(resolveFormMessage(result, notifications.form))
}

This replaces a hardcoded Error: ${result.reason} — now a friendly text from the panel, per language.

{field} interpolation

The validation text may contain {field} — it is replaced with the technical name of the field with the error (name, not the label), only the first {field} occurrence; no name → empty string:

Panel:  "Sprawdź pole {field} i spróbuj ponownie."
Result: "Sprawdź pole email i spróbuj ponownie."

Adding new contexts

The form group is the first context. Further ones (newsletter, system) are added the same way — a new group in globals/Notifications/fields.ts + a field in the types + a fallback. getNotificationTexts resolves whatever exists.

Access

The global has read: () => true — the texts are public (shown to end users), so the frontend reads them without a session. Write: editor+ (adminOrEditor). Unlike SiteIntegrations (read: isAdmin — secrets live there).

Turn off, override, extend

  • Turn off: none — the notifications global is currently always added (also without the forms option). The lightbox group is opt-in (ipalKit({ lightbox: true })); lightboxNotificationFields puts it in your own global instead.
  • Override texts: in the panel, per language; empty field → notificationFallback(locale) (Polish on pl pages, English otherwise). The fallbacks cannot be swapped with a plugin option — apply your own default texts in the project on the result of getNotificationTexts (e.g. texts.form.error ||= '…').
  • Override access / hooks / admin: ipalKit({ globalOverrides: { notifications: … } }) — an object merges access, admin and hooks key by key over the plugin's (e.g. let any logged-in user edit: { notifications: { access: { update: authenticated } } }), a function gets the global and returns the one to use.
  • Override mapping: resolveFormMessage is optional — you can map result.reason with your own switch.
  • Extend: the global's fields are fixed in the plugin (no ipalKit option to add groups / fields); for a project-only extra, a globalOverrides.notifications function can append fields, but getNotificationTexts will not read them — a new shared context needs a change in the plugin (globals/Notifications/fields.ts, types, fallback).