Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ export const selectors = {
(state: State) => state.nowUpdatedEveryMinute,
(adapter, now, date: SchedulerValidDate) => adapter.isSameDay(date, now),
),
/**
* Builds the presets the user can choose from when creating or editing a recurring event.
*/
recurrencePresets: createSelectorMemoized(
(state: State) => state.adapter,
(adapter, date: SchedulerValidDate): Record<RecurrencePresetKey, RRuleSpec> => {
Expand Down Expand Up @@ -141,4 +144,76 @@ export const selectors = {
};
},
),
/**
* Determines which preset (if any) the given rule corresponds to.
* If the rule does not correspond to any preset, 'custom' is returned.
* If no rule is provided, null is returned.
*/
defaultRecurrencePresetKey: createSelectorMemoized(
(state: State) => state.adapter,
(
adapter,
rule: CalendarEvent['rrule'] | undefined,
occurrenceStart: SchedulerValidDate,
): RecurrencePresetKey | 'custom' | null => {
if (!rule) {
return null;
}

const interval = rule.interval ?? 1;
const neverEnds = !rule.count && !rule.until;
const hasSelectors = !!(
rule.byDay?.length ||
rule.byMonthDay?.length ||
rule.byMonth?.length
);
const { numToByDay: numToCode } = getByDayMaps(adapter);

switch (rule.freq) {
case 'DAILY': {
// Preset "Daily" => FREQ=DAILY;INTERVAL=1; no COUNT/UNTIL;
return interval === 1 && neverEnds && !hasSelectors ? 'daily' : 'custom';
}

case 'WEEKLY': {
// Preset "Weekly" => FREQ=WEEKLY;INTERVAL=1;BYDAY=<weekday-of-start>; no COUNT/UNTIL;
const startDowCode = numToCode[adapter.getDayOfWeek(occurrenceStart)];

const byDay = rule.byDay ?? [];
const matchesDefaultByDay =
byDay.length === 0 || (byDay.length === 1 && byDay[0] === startDowCode);
const isPresetWeekly =
interval === 1 &&
neverEnds &&
matchesDefaultByDay &&
!(rule.byMonthDay?.length || rule.byMonth?.length);

return isPresetWeekly ? 'weekly' : 'custom';
}

case 'MONTHLY': {
// Preset "Monthly" => FREQ=MONTHLY;INTERVAL=1;BYMONTHDAY=<start-day>; no COUNT/UNTIL;
const day = adapter.getDate(occurrenceStart);
const byMonthDay = rule.byMonthDay ?? [];
const matchesDefaultByMonthDay =
byMonthDay.length === 0 || (byMonthDay.length === 1 && byMonthDay[0] === day);
const isPresetMonthly =
interval === 1 &&
neverEnds &&
matchesDefaultByMonthDay &&
!(rule.byDay?.length || rule.byMonth?.length);

return isPresetMonthly ? 'monthly' : 'custom';
}

case 'YEARLY': {
// Preset "Yearly" => FREQ=YEARLY;INTERVAL=1; no COUNT/UNTIL;
return interval === 1 && neverEnds && !hasSelectors ? 'yearly' : 'custom';
}

default:
return 'custom';
}
},
),
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
CalendarResource,
CalendarResourceId,
SchedulerValidDate,
RecurrencePresetKey,
CalendarEventUpdatedProperties,
} from '../../models';
import {
Expand All @@ -24,7 +23,6 @@ import { Adapter } from '../../use-adapter/useAdapter.types';
import {
applyRecurringUpdateFollowing,
applyRecurringUpdateAll,
getByDayMaps,
applyRecurringUpdateOnlyThis,
} from '../recurrence-utils';
import { selectors } from './SchedulerStore.selectors';
Expand Down Expand Up @@ -331,70 +329,4 @@ export class SchedulerStore<
this.set('occurrencePlaceholder', newPlaceholder);
}
};

/**
* Determines which preset (if any) the given rule corresponds to.
* If the rule does not correspond to any preset, 'custom' is returned.
* If no rule is provided, null is returned.
*/
public getRecurrencePresetKeyFromRule = (
rule: CalendarEvent['rrule'] | undefined,
start: SchedulerValidDate,
): RecurrencePresetKey | 'custom' | null => {
if (!rule) {
return null;
}

const { adapter } = this.state;
const interval = rule.interval ?? 1;
const neverEnds = !rule.count && !rule.until;
const hasSelectors = !!(rule.byDay?.length || rule.byMonthDay?.length || rule.byMonth?.length);
const { numToByDay: numToCode } = getByDayMaps(adapter);

switch (rule.freq) {
case 'DAILY': {
// Preset "Daily" => FREQ=DAILY;INTERVAL=1; no COUNT/UNTIL;
return interval === 1 && neverEnds && !hasSelectors ? 'daily' : 'custom';
}

case 'WEEKLY': {
// Preset "Weekly" => FREQ=WEEKLY;INTERVAL=1;BYDAY=<weekday-of-start>; no COUNT/UNTIL;
const startDowCode = numToCode[adapter.getDayOfWeek(start)];

const byDay = rule.byDay ?? [];
const matchesDefaultByDay =
byDay.length === 0 || (byDay.length === 1 && byDay[0] === startDowCode);
const isPresetWeekly =
interval === 1 &&
neverEnds &&
matchesDefaultByDay &&
!(rule.byMonthDay?.length || rule.byMonth?.length);

return isPresetWeekly ? 'weekly' : 'custom';
}

case 'MONTHLY': {
// Preset "Monthly" => FREQ=MONTHLY;INTERVAL=1;BYMONTHDAY=<start-day>; no COUNT/UNTIL;
const day = adapter.getDate(start);
const byMonthDay = rule.byMonthDay ?? [];
const matchesDefaultByMonthDay =
byMonthDay.length === 0 || (byMonthDay.length === 1 && byMonthDay[0] === day);
const isPresetMonthly =
interval === 1 &&
neverEnds &&
matchesDefaultByMonthDay &&
!(rule.byDay?.length || rule.byMonth?.length);

return isPresetMonthly ? 'monthly' : 'custom';
}

case 'YEARLY': {
// Preset "Yearly" => FREQ=YEARLY;INTERVAL=1; no COUNT/UNTIL;
return interval === 1 && neverEnds && !hasSelectors ? 'yearly' : 'custom';
}

default:
return 'custom';
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { getByDayMaps } from '../../recurrence-utils';
import { selectors } from '../SchedulerStore.selectors';
import { SchedulerState as State } from '../SchedulerStore.types';

const DEFAULT_PARAMS = { events: [] };

const baseState = (overrides: Partial<State> = {}) =>
({
adapter,
Expand Down Expand Up @@ -43,47 +41,60 @@ storeClasses.forEach((storeClass) => {
});
});

describe('Method: getRecurrencePresetKeyFromRule', () => {
describe('Selector: defaultRecurrencePresetKey', () => {
const state = baseState();
const store = new storeClass.Value({ ...DEFAULT_PARAMS }, adapter);
const start = adapter.date('2025-08-05T09:00:00'); // Tuesday
const presets = selectors.recurrencePresets(state, start);

it('returns null when rule undefined', () => {
expect(store.getRecurrencePresetKeyFromRule(undefined, start)).to.equal(null);
expect(selectors.defaultRecurrencePresetKey(state, undefined, start)).to.equal(null);
});

it('detects daily, weekly, monthly and yearly presets', () => {
expect(store.getRecurrencePresetKeyFromRule(presets.daily, start)).to.equal('daily');
expect(store.getRecurrencePresetKeyFromRule(presets.weekly, start)).to.equal('weekly');
expect(store.getRecurrencePresetKeyFromRule(presets.monthly, start)).to.equal('monthly');
expect(store.getRecurrencePresetKeyFromRule(presets.yearly, start)).to.equal('yearly');
expect(selectors.defaultRecurrencePresetKey(state, presets.daily, start)).to.equal('daily');
expect(selectors.defaultRecurrencePresetKey(state, presets.weekly, start)).to.equal(
'weekly',
);
expect(selectors.defaultRecurrencePresetKey(state, presets.monthly, start)).to.equal(
'monthly',
);
expect(selectors.defaultRecurrencePresetKey(state, presets.yearly, start)).to.equal(
'yearly',
);
});

it('classifies daily interval>1 or with finite end (count) as custom', () => {
const ruleInterval2: RRuleSpec = { ...presets.daily, interval: 2 };
expect(store.getRecurrencePresetKeyFromRule(ruleInterval2, start)).to.equal('custom');
expect(selectors.defaultRecurrencePresetKey(state, ruleInterval2, start)).to.equal(
'custom',
);

const ruleFiniteCount: RRuleSpec = { ...presets.daily, count: 5 };
expect(store.getRecurrencePresetKeyFromRule(ruleFiniteCount, start)).to.equal('custom');
expect(selectors.defaultRecurrencePresetKey(state, ruleFiniteCount, start)).to.equal(
'custom',
);
});

it('classifies weekly with extra day as custom', () => {
const rule: RRuleSpec = { ...presets.weekly, byDay: ['TU', 'WE'] };
expect(store.getRecurrencePresetKeyFromRule(rule, start)).to.equal('custom');
expect(selectors.defaultRecurrencePresetKey(state, rule, start)).to.equal('custom');
});

it('classifies monthly with different day or with interval>1 as custom', () => {
const ruleDifferentDay: RRuleSpec = { ...presets.monthly, byMonthDay: [26] };
expect(store.getRecurrencePresetKeyFromRule(ruleDifferentDay, start)).to.equal('custom');
expect(selectors.defaultRecurrencePresetKey(state, ruleDifferentDay, start)).to.equal(
'custom',
);

const ruleInterval2: RRuleSpec = { ...presets.monthly, interval: 2 };
expect(store.getRecurrencePresetKeyFromRule(ruleInterval2, start)).to.equal('custom');
expect(selectors.defaultRecurrencePresetKey(state, ruleInterval2, start)).to.equal(
'custom',
);
});

it('classifies yearly interval>1 as custom', () => {
const rule: RRuleSpec = { ...presets.yearly, interval: 2 };
expect(store.getRecurrencePresetKeyFromRule(rule, start)).to.equal('custom');
expect(selectors.defaultRecurrencePresetKey(state, rule, start)).to.equal('custom');
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ export const EventPopover = React.forwardRef(function EventPopover(
const color = useStore(store, selectors.eventColor, occurrence.id);
const rawPlaceholder = useStore(store, selectors.occurrencePlaceholder);
const recurrencePresets = useStore(store, selectors.recurrencePresets, occurrence.start);
const defaultRecurrenceKey = useStore(
store,
selectors.defaultRecurrencePresetKey,
occurrence.rrule,
occurrence.start,
);

// State hooks
const [errors, setErrors] = React.useState<Form.Props['errors']>({});
Expand Down Expand Up @@ -159,11 +165,6 @@ export const EventPopover = React.forwardRef(function EventPopover(
];
}, [resources, translations.labelNoResource]);

const defaultRecurrenceKey = React.useMemo(
() => store.getRecurrencePresetKeyFromRule(occurrence.rrule, occurrence.start),
[store, occurrence.rrule, occurrence.start],
);

function validateRange(
start: SchedulerValidDate,
end: SchedulerValidDate,
Expand Down
Loading