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' useAsTitle: 'name'
}, },
timestamps: false, 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: [ fields: [
{ {
name: 'name', name: 'name',
@@ -26,42 +48,60 @@ const Events: CollectionConfig = {
type: 'row', type: 'row',
fields: [ fields: [
{ {
name: 'startTime', name: 'start',
label: 'Start Time', label: 'Start',
type: 'date', type: 'text',
required: true, 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: { admin: {
width: '50%', width: '50%',
date: { description: 'Please fill out in the form dd/mm/yyyy HH:mm'
displayFormat: 'MMM d, yyy HH:mm',
timeFormat: 'HH:mm',
timeIntervals: 15
}
} }
}, },
{ {
name: 'endTime', name: 'end',
label: 'End Time', label: 'End',
type: 'date', type: 'text',
validate: (val, { siblingData }) => { validate: (val, { siblingData }) => {
if (!val) if (!val)
return true; return true;
const end = new Date(val).getTime(); const regex = /^\d{2}\/\d{2}\/\d{4} \d{2}:\d{2}$/;
const start = new Date(siblingData.startTime).getTime();
if (end >= start) if (!regex.test(val))
return true; 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 'End date must be greater than or equal to the start date';
return true;
}, },
admin: { admin: {
width: '50%', width: '50%',
date: { description: 'Please fill out in the form dd/mm/yyyy HH:mm'
displayFormat: 'MMM d, yyy HH:mm',
timeFormat: 'HH:mm',
timeIntervals: 15
}
} }
} }
] ]

View File

@@ -39,6 +39,6 @@ export interface Event {
value: string | EventType; value: string | EventType;
relationTo: 'event-types'; relationTo: 'event-types';
}; };
startTime: string; start: string;
endTime?: string; end?: string;
} }