diff --git a/src/collections/Events.ts b/src/collections/Events.ts index 9f09096..4cf539e 100644 --- a/src/collections/Events.ts +++ b/src/collections/Events.ts @@ -6,6 +6,28 @@ const Events: CollectionConfig = { useAsTitle: 'name' }, 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', @@ -26,42 +48,60 @@ const Events: CollectionConfig = { type: 'row', fields: [ { - name: 'startTime', - label: 'Start Time', - type: 'date', + 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%', - date: { - displayFormat: 'MMM d, yyy HH:mm', - timeFormat: 'HH:mm', - timeIntervals: 15 - } + description: 'Please fill out in the form dd/mm/yyyy HH:mm' } }, { - name: 'endTime', - label: 'End Time', - type: 'date', + name: 'end', + label: 'End', + type: 'text', validate: (val, { siblingData }) => { if (!val) return true; - const end = new Date(val).getTime(); - const start = new Date(siblingData.startTime).getTime(); + const regex = /^\d{2}\/\d{2}\/\d{4} \d{2}:\d{2}$/; - if (end >= start) - return true; + if (!regex.test(val)) + return 'Must be in the form dd/mm/yyyy HH:mm'; - return 'End date must be greater than or equal to the start date'; + 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%', - date: { - displayFormat: 'MMM d, yyy HH:mm', - timeFormat: 'HH:mm', - timeIntervals: 15 - } + description: 'Please fill out in the form dd/mm/yyyy HH:mm' } } ] diff --git a/src/payload-types.ts b/src/payload-types.ts index a20d50d..40cde95 100644 --- a/src/payload-types.ts +++ b/src/payload-types.ts @@ -39,6 +39,6 @@ export interface Event { value: string | EventType; relationTo: 'event-types'; }; - startTime: string; - endTime?: string; + start: string; + end?: string; }