Files
ipal-kit/src/modules/notifications/getNotificationTexts.ts
T

53 lines
1.9 KiB
TypeScript

import type { BasePayload } from 'payload'
import type { NotificationsData, NotificationTexts } from './types.js'
import { resolveLightboxTexts } from '../lightbox/texts.js'
import { getGlobal } from '../payload/index.js'
import { notificationFallback } from './defaults.js'
type GetNotificationTextsArgs = {
/** Active locale — selects the language variant of each text. */
locale?: string
payload: BasePayload
}
/**
* Resolves notification texts from the Notifications global, falling back per
* field to the defaults in the page's language (Polish for 'pl', else English).
* Mirrors getConsentTexts: one read, per-field fallback, locale-aware. The frontend maps a submitForm result code to the
* matching text and styles it however it likes (toast, inline, banner).
*/
export async function getNotificationTexts({
locale,
payload,
}: GetNotificationTextsArgs): Promise<NotificationTexts> {
const g = await getGlobal<NotificationsData>(payload, 'notifications', { locale })
const f = g.form ?? {}
const fb = notificationFallback(locale).form
return {
form: {
back: f.back || fb.back,
consent: f.consent || fb.consent,
error: f.error || fb.error,
fileCount: f.fileCount || fb.fileCount,
fileNotSent: f.fileNotSent || fb.fileNotSent,
fileOptional: f.fileOptional || fb.fileOptional,
fileRefused: f.fileRefused || fb.fileRefused,
fileRemove: f.fileRemove || fb.fileRemove,
fileTooLarge: f.fileTooLarge || fb.fileTooLarge,
fileType: f.fileType || fb.fileType,
next: f.next || fb.next,
notFound: f.notFound || fb.notFound,
rateLimited: f.rateLimited || fb.rateLimited,
stepOf: f.stepOf || fb.stepOf,
success: f.success || fb.success,
turnstile: f.turnstile || fb.turnstile,
validation: f.validation || fb.validation,
},
lightbox: resolveLightboxTexts(g.lightbox, locale),
}
}