41 lines
1.3 KiB
TypeScript
41 lines
1.3 KiB
TypeScript
import type { BasePayload } from 'payload'
|
|
|
|
import type { NotificationsData, NotificationTexts } from './types.js'
|
|
|
|
import { getGlobal } from '../payload/index.js'
|
|
import { NOTIFICATION_FALLBACK } 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 to
|
|
* English defaults per field. 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 = NOTIFICATION_FALLBACK.form
|
|
|
|
return {
|
|
form: {
|
|
consent: f.consent || fb.consent,
|
|
error: f.error || fb.error,
|
|
notFound: f.notFound || fb.notFound,
|
|
rateLimited: f.rateLimited || fb.rateLimited,
|
|
success: f.success || fb.success,
|
|
turnstile: f.turnstile || fb.turnstile,
|
|
validation: f.validation || fb.validation,
|
|
},
|
|
}
|
|
}
|