// global import { GlobalConfig } from 'payload' const OffersConfig: GlobalConfig = { slug: 'offers-config', label: { en: 'Offers Config', }, fields: [ { type: 'array', name: 'offers', label: { en: 'Offers', }, access: { create: () => false, }, admin: { description: { en: 'Drag and drop to reorder the offers', }, components: { RowLabel: { path: 'src/components/RowLabel.ts', }, }, }, fields: [ { type: 'row', admin: { readOnly: true, }, fields: [ { name: 'title', label: 'Offre', type: 'text', }, { name: 'promotions', type: 'text', }, ] } ], virtual: true, validate: () => true, maxRows: 1, } ], hooks: { afterRead: [ async ({ doc, req }) => { const offers = await req.payload.find({collection: 'offers', sort: 'priority'}); doc.offers = offers.docs return doc }, ], beforeChange: [ async ({ data, req }) => { for (let i = 0; i < data.offers.length; i++) { const offer = data.offers[i]; await req.payload.update({ collection: 'offers', id: offer.id, data: { priority: i + 1 }, }); } return data; }, ], } } export default OffersConfig