Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / applications / calendar / src / app / components / eventModal / eventForm / getHasEdited.ts
blobca47b8d0d6201283cb2d423e93fb79fa694b7fc7
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];
9     });
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
22 ) => {
23     return getHasEditedHourMinutes(time, otherTime) || !isSameDay(date, otherDate);
26 const getHasEditedNotification = (notification: NotificationModel, otherNotification: NotificationModel) => {
27     return (
28         getHasEdited(['type', 'unit', 'when', 'value'], notification, otherNotification) ||
29         (notification.at && otherNotification.at && getHasEditedHourMinutes(notification.at, otherNotification.at))
30     );
33 export const getHasEditedNotifications = (
34     notifications: NotificationModel[],
35     otherNotifications: NotificationModel[]
36 ) => {
37     return (
38         notifications.length !== otherNotifications.length ||
39         notifications.some((notification, i) => getHasEditedNotification(notification, otherNotifications[i]))
40     );
43 const getNotifications = ({ isAllDay, partDayNotifications, fullDayNotifications }: EventModel) => {
44     return isAllDay ? fullDayNotifications : partDayNotifications;
47 export const getHasDoneChanges = (model: EventModel, otherModel: EventModel, isEditMode: boolean) => {
48     return (
49         getHasEdited(keys, model, otherModel) ||
50         getHasEditedNotifications(getNotifications(model), getNotifications(otherModel)) ||
51         getHasEditedTimezone(model.start, otherModel.start) ||
52         getHasEditedTimezone(model.end, otherModel.end) ||
53         (isEditMode &&
54             (getHasEditedDateTime(model.start, otherModel.start) || getHasEditedDateTime(model.end, otherModel.end)))
55     );