wwwfiberdirekt/src/globals/PopupAnnouncement/config.ts

183 lines
4.7 KiB
TypeScript

import type { GlobalConfig } from 'payload'
import { adminOnly } from '../../access/adminOnly'
import { revalidatePopup } from './hooks/revalidatePopup'
export const PopupAnnouncement: GlobalConfig = {
slug: 'popup-announcement',
label: 'Popup-meddelande',
admin: {
group: 'Globala inställningar',
description: 'Ett popup-fönster som visas för besökare. Kan begränsas till specifika sidor.',
},
access: {
read: () => true,
update: adminOnly,
},
hooks: {
afterChange: [revalidatePopup],
},
fields: [
{
name: 'enabled',
type: 'checkbox',
label: 'Aktiverad',
defaultValue: false,
},
{
name: 'heading',
type: 'text',
label: 'Rubrik',
required: true,
defaultValue: 'Nyhet',
admin: {
condition: (data) => Boolean(data?.enabled),
},
},
{
name: 'subheading',
type: 'text',
label: 'Underrubrik (valfri)',
admin: {
condition: (data) => Boolean(data?.enabled),
},
},
{
name: 'body',
type: 'textarea',
label: 'Beskrivning',
admin: {
condition: (data) => Boolean(data?.enabled),
},
},
{
name: 'ctaText',
type: 'text',
label: 'CTA-knapptext',
defaultValue: 'Läs mer',
admin: {
condition: (data) => Boolean(data?.enabled),
},
},
// ── CTA Link — internal page picker or custom URL ──────────────────────
{
name: 'ctaLink',
type: 'group',
label: 'CTA-länk',
admin: {
condition: (data) => Boolean(data?.enabled),
hideGutter: true,
},
fields: [
{
type: 'row',
fields: [
{
name: 'type',
type: 'radio',
label: 'Länktyp',
defaultValue: 'custom',
options: [
{ label: 'Intern sida', value: 'reference' },
{ label: 'Egen URL', value: 'custom' },
],
admin: { layout: 'horizontal', width: '50%' },
},
{
name: 'newTab',
type: 'checkbox',
label: 'Öppna i ny flik',
admin: { width: '50%', style: { alignSelf: 'flex-end' } },
},
],
},
{
name: 'reference',
type: 'relationship',
label: 'Sida',
relationTo: ['pages', 'posts'] as const,
admin: {
condition: (_, siblingData) => siblingData?.type === 'reference',
},
},
{
name: 'url',
type: 'text',
label: 'URL',
admin: {
condition: (_, siblingData) => siblingData?.type === 'custom',
description: 'T.ex. /bredband eller https://example.com',
},
},
],
},
{
name: 'image',
type: 'upload',
relationTo: 'media' as const,
label: 'Bild (valfri, visas till höger)',
admin: {
condition: (data) => Boolean(data?.enabled),
},
},
{
name: 'badgeText',
type: 'text',
label: 'Badge-text (valfri)',
defaultValue: 'NYHET',
admin: {
description: 'T.ex. "NYHET", "ERBJUDANDE", "VIKTIG INFO"',
condition: (data) => Boolean(data?.enabled),
},
},
{
name: 'theme',
type: 'select',
label: 'Tema',
defaultValue: 'light',
options: [
{ label: 'Ljust (vit bakgrund)', value: 'light' },
{ label: 'Mörkt (navy bakgrund)', value: 'dark' },
],
admin: {
condition: (data) => Boolean(data?.enabled),
},
},
{
name: 'showOnPages',
type: 'select',
label: 'Visa på',
defaultValue: 'all',
options: [
{ label: 'Alla sidor', value: 'all' },
{ label: 'Enbart startsidan', value: 'home' },
{ label: 'Specifika sidor', value: 'specific' },
],
admin: {
condition: (data) => Boolean(data?.enabled),
},
},
// ── Specific pages — relationship instead of fragile comma-separated text ──
{
name: 'specificPages',
type: 'relationship',
label: 'Välj sidor',
relationTo: 'pages' as const,
hasMany: true,
admin: {
description: 'Välj exakt vilka sidor popupen ska visas på.',
condition: (data) => data?.showOnPages === 'specific',
},
},
{
name: 'dismissDays',
type: 'number',
label: 'Dölj i antal dagar efter stängning',
defaultValue: 7,
admin: {
description: 'Hur många dagar popupen ska döljas efter att besökaren stänger den.',
condition: (data) => Boolean(data?.enabled),
},
},
],
}