import type { Field } from 'payload' import type { BlockOptions } from '../../types.js' import { adminText, defineLibraryBlock, variantOption } from '../../define.js' import { buildItemsField, buildMediaField } from '../../fields.js' export const TESTIMONIALS_VARIANTS = [variantOption('grid', 'Grid', 'Siatka'), variantOption('single', 'One at a time', 'Pojedyncza opinia')] as const export type TestimonialsOptions = { /** * A collection of testimonials to pick from, e.g. 'testimonials' — adds "From the list" as a source. * Its documents need `quote` and `heading` (author); `text`, `media`, `rating` are optional * (or map them with `mapTestimonial` in createBlockRegistry). Default none: inline only. */ collection?: string } & BlockOptions const ITEM_FIELDS: Field[] = [ { name: 'quote', type: 'textarea', admin: { rows: 4 }, label: adminText('Quote', 'Cytat'), localized: true, required: true }, { type: 'row', fields: [ { name: 'heading', type: 'text', admin: { width: '50%' }, label: adminText('Author', 'Autor'), localized: true, required: true }, { name: 'text', type: 'text', admin: { width: '50%' }, label: adminText('Role or company', 'Stanowisko lub firma'), localized: true }, ], }, buildMediaField({ label: adminText('Photo', 'Zdjęcie') }), { name: 'rating', type: 'number', admin: { step: 1 }, label: adminText('Rating (1–5)', 'Ocena (1–5)'), max: 5, min: 1 }, ] /** Testimonials — quotes typed into the block, or picked from a testimonials collection. */ export const buildTestimonialsBlock = defineLibraryBlock({ slug: 'testimonials', fields: ({ collection, maxItems = 12 }) => { const items = buildItemsField({ fields: ITEM_FIELDS, labels: { plural: adminText('Testimonials', 'Opinie'), singular: adminText('Testimonial', 'Opinia') }, maxRows: maxItems, }) if (!collection) {return [items]} const fromList = (_: unknown, s: { source?: string } | undefined) => s?.source === 'collection' return [ { name: 'source', type: 'radio', defaultValue: 'inline', label: adminText('Source', 'Źródło'), options: [ { label: adminText('Typed here', 'Wpisane tutaj'), value: 'inline' }, { label: adminText('From the list', 'Z listy opinii'), value: 'collection' }, ], }, { ...items, admin: { ...items.admin, condition: (d: unknown, s: { source?: string } | undefined) => !fromList(d, s) } }, { name: 'entries', type: 'relationship', admin: { condition: fromList }, hasMany: true, label: adminText('Testimonials', 'Opinie'), maxRows: maxItems, relationTo: collection, }, ] }, interfaceName: 'TestimonialsBlock', labels: { plural: adminText('Testimonials', 'Opinie'), singular: adminText('Testimonials', 'Opinie') }, links: { maxRows: 1 }, variants: TESTIMONIALS_VARIANTS, })