import { CollectionConfig } from 'payload/types'; const Events: CollectionConfig = { slug: 'events', admin: { useAsTitle: 'name' }, access: { read: () => true, }, timestamps: false, hooks: { afterRead: [ ({ doc }) => { const [ startDate, startTime ] = doc?.start?.split(' ') ?? [ null, null ]; const [ endDate, endTime ] = doc?.end?.split(' ') ?? [ null, null ]; Object.assign(doc, { startTime, startDate }); if (endTime && endDate) { Object.assign(doc, { endTime, endDate }); } return doc; }, ] }, fields: [ { name: 'name', label: 'Name', type: 'text', required: true }, { name: 'type', label: 'Type', type: 'relationship', relationTo: [ 'event-types' ], required: true }, { type: 'row', fields: [ { name: 'start', label: 'Start', type: 'text', required: true, validate: (val) => { const regex = /^\d{2}\/\d{2}\/\d{4} \d{2}:\d{2}$/; if (!regex.test(val)) return 'Must be in the form dd/mm/yyyy HH:mm'; return true; }, admin: { width: '50%', description: 'Please fill out in the form dd/mm/yyyy HH:mm' } }, { name: 'end', label: 'End', type: 'text', validate: (val, { siblingData }) => { if (!val) return true; const regex = /^\d{2}\/\d{2}\/\d{4} \d{2}:\d{2}$/; if (!regex.test(val)) return 'Must be in the form dd/mm/yyyy HH:mm'; const end = val; const start = siblingData.start; const [ startDateObject, endDateObject ] = [ start, end ].map(v => { const d = new Date(); const [ date, time ] = v.split(' '); const [ day, month, year ] = date.split('/'); const [ hours, minutes ] = time.split(':'); d.setFullYear(year, month - 1, day); d.setHours(hours, minutes, 0, 0); return d; }); if (endDateObject < startDateObject) return 'End date must be greater than or equal to the start date'; return true; }, admin: { width: '50%', description: 'Please fill out in the form dd/mm/yyyy HH:mm' } } ] } ] }; export default Events;