1 import { isSameDay } from '@proton/shared/lib/date-fns-utc';
2 import type { DateTimeModel, EventModel, NotificationModel } from '@proton/shared/lib/interfaces/calendar';
4 const keys = ['title', 'location', 'description', 'isAllDay', 'frequency'];
6 export const getHasEdited = (keys: string[], model: any, otherModel: any) => {
7 return keys.some((key) => {
8 return model[key] !== otherModel[key];
12 export const getHasEditedTimezone = ({ tzid }: DateTimeModel, { tzid: otherTzid }: DateTimeModel) => {
13 return tzid !== otherTzid;
16 const getHasEditedHourMinutes = (a: Date, b: Date) =>
17 a.getHours() !== b.getHours() || a.getMinutes() !== b.getMinutes();
19 export const getHasEditedDateTime = (
20 { time, date }: DateTimeModel,
21 { time: otherTime, date: otherDate }: DateTimeModel
23 return getHasEditedHourMinutes(time, otherTime) || !isSameDay(date, otherDate);
26 const getHasEditedNotification = (notification: NotificationModel, otherNotification: NotificationModel) => {
28 getHasEdited(['type', 'unit', 'when', 'value'], notification, otherNotification) ||
29 (notification.at && otherNotification.at && getHasEditedHourMinutes(notification.at, otherNotification.at))
33 export const getHasEditedNotifications = (
34 notifications: NotificationModel[],
35 otherNotifications: NotificationModel[]
38 notifications.length !== otherNotifications.length ||
39 notifications.some((notification, i) => getHasEditedNotification(notification, otherNotifications[i]))
43 const getNotifications = ({ isAllDay, partDayNotifications, fullDayNotifications }: EventModel) => {
44 return isAllDay ? fullDayNotifications : partDayNotifications;
47 export const getHasDoneChanges = (model: EventModel, otherModel: EventModel, isEditMode: boolean) => {
49 getHasEdited(keys, model, otherModel) ||
50 getHasEditedNotifications(getNotifications(model), getNotifications(otherModel)) ||
51 getHasEditedTimezone(model.start, otherModel.start) ||
52 getHasEditedTimezone(model.end, otherModel.end) ||
54 (getHasEditedDateTime(model.start, otherModel.start) || getHasEditedDateTime(model.end, otherModel.end)))