1 import { addDays, isSameDay } from '@proton/shared/lib/date-fns-utc';
2 import type { EventModel } from '@proton/shared/lib/interfaces/calendar';
4 import { getInitialDateTimeModel } from './state';
5 import { getTimeInUtc } from './time';
7 const isZeroHoursMinutes = (date: Date) => date.getHours() === 0 && date.getMinutes() === 0;
9 export const getAllDayCheck = ({
15 }): Partial<EventModel> => {
16 const hasDefaultNotifications =
17 oldModel[isAllDay ? 'hasFullDayDefaultNotifications' : 'hasPartDayDefaultNotifications'];
19 if (!isAllDay && oldModel.isAllDay) {
20 // If switching to a part day event and there is no time, assume it has never been set.
21 if (isZeroHoursMinutes(oldModel.start.time) && isZeroHoursMinutes(oldModel.end.time)) {
23 start: { date: startDate, time: startTime },
24 end: { date: endDate, time: endTime },
25 } = getInitialDateTimeModel(oldModel.initialDate, oldModel.defaultEventDuration, '');
27 const newStartDate = oldModel.start.date;
28 // If the default time did not yield the same date, assume it starts in one day and spans into the next (max 1 day)
29 const newEndDate = !isSameDay(startDate, endDate) ? addDays(oldModel.end.date, 1) : oldModel.end.date;
33 start: { tzid: oldModel.initialTzid, date: newStartDate, time: startTime },
34 end: { tzid: oldModel.initialTzid, date: newEndDate, time: endTime },
35 hasDefaultNotifications,
38 // If switching to a part day event and there is time, make sure the end time is not earlier than the start time
39 const startUtcDate = getTimeInUtc(oldModel.start, isAllDay);
40 const endUtcDate = getTimeInUtc(oldModel.end, isAllDay);
41 if (+endUtcDate < +startUtcDate) {
44 end: { tzid: oldModel.initialTzid, date: oldModel.start.date, time: oldModel.start.time },
45 hasDefaultNotifications,
52 hasDefaultNotifications,