Rewrite events collection to use Text fields instead of Date

This commit is contained in:
2022-07-24 13:41:10 +00:00
parent 53dfc26b88
commit 6b6eacf45d
2 changed files with 63 additions and 23 deletions

View File

@@ -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'
}
}
]