Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / applications / calendar / src / app / components / eventModal / eventForm / propertiesToFrequencyModel.tsx
bloba4db531b6139d2e916eb66d1312e91635ef44153
1 import { DAILY_TYPE, FREQUENCY, WEEKLY_TYPE, YEARLY_TYPE } from '@proton/shared/lib/calendar/constants';
2 import { getIsRruleCustom, getIsRruleSimple } from '@proton/shared/lib/calendar/recurrence/rrule';
3 import {
4     getEndType,
5     getMonthType,
6     getUntilDate,
7     getWeeklyDays,
8 } from '@proton/shared/lib/calendar/recurrence/rruleProperties';
9 import { fromUTCDate, toLocalDate } from '@proton/shared/lib/date/timezone';
10 import type { DateTimeModel, FrequencyModel } from '@proton/shared/lib/interfaces/calendar';
11 import type { VcalRruleFreqValue, VcalRruleProperty } from '@proton/shared/lib/interfaces/calendar/VcalModel';
12 import unique from '@proton/utils/unique';
14 const getSafeWeeklyDays = (startDate: Date, byday?: string | string[]) => {
15     const DEFAULT = [startDate.getDay()];
16     const days = getWeeklyDays(byday);
17     return unique([...DEFAULT].concat(days));
20 const getSafeFrequency = (freq: VcalRruleFreqValue): FREQUENCY | undefined => {
21     if (!freq) {
22         return;
23     }
24     return Object.values(FREQUENCY).find((value) => value.toLowerCase() === freq.toLowerCase());
27 const getType = (isSimple: boolean, isCustom: boolean, freq: VcalRruleFreqValue) => {
28     if (!isSimple && !isCustom) {
29         return FREQUENCY.OTHER;
30     }
31     if (isCustom) {
32         return FREQUENCY.CUSTOM;
33     }
34     return getSafeFrequency(freq) || FREQUENCY.ONCE;
37 const getFrequency = (freq: VcalRruleFreqValue) => {
38     return getSafeFrequency(freq) || FREQUENCY.WEEKLY;
41 /**
42  * Given a parsed recurrence rule in standard format,
43  * parse it into the object that goes in the Event model
44  */
45 export const propertiesToFrequencyModel = (
46     rrule: VcalRruleProperty | undefined,
47     { date: startDate, tzid: startTzid }: DateTimeModel
48 ): FrequencyModel => {
49     const rruleValue = rrule?.value;
50     const { freq, count, interval, until, bysetpos, byday } = rruleValue || {};
51     const isSimple = getIsRruleSimple(rruleValue);
52     const isCustom = !isSimple ? getIsRruleCustom(rruleValue) : false;
53     const type = !rrule ? FREQUENCY.ONCE : getType(isSimple, isCustom, freq);
54     const frequency = getFrequency(freq);
55     const endType = getEndType(count, until);
56     const monthType = getMonthType(byday, bysetpos);
57     const utcUntilDate = getUntilDate(until, startTzid);
58     const untilDate = utcUntilDate ? toLocalDate(fromUTCDate(utcUntilDate)) : undefined;
59     const weeklyDays = getSafeWeeklyDays(startDate, byday);
61     return {
62         type,
63         frequency,
64         interval: interval || 1, // INTERVAL=1 is ignored when parsing a recurring rule
65         daily: {
66             type: DAILY_TYPE.ALL_DAYS,
67         },
68         weekly: {
69             type: WEEKLY_TYPE.ON_DAYS,
70             days: weeklyDays,
71         },
72         monthly: {
73             type: monthType,
74         },
75         yearly: {
76             type: YEARLY_TYPE.BY_MONTH_ON_MONTH_DAY,
77         },
78         ends: {
79             type: endType,
80             count: count || 2,
81             until: untilDate,
82         },
83         vcalRruleValue: rruleValue,
84     };