Skip to content

Commit

Permalink
fix: event recurrence update fn (#358)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomosterlund committed Apr 16, 2024
1 parent 8578f40 commit b229acb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
2 changes: 1 addition & 1 deletion development/calendar/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const calendar = createCalendar({
// locale: 'ja-JP',
// locale: 'en-US',
// locale: 'zh-CN',
locale: 'ky-KG',
locale: 'de-DE',
views: [viewMonthGrid, viewWeek, viewDay, viewMonthAgenda],
// defaultView: viewWeek.name,
// minDate: '2024-01-01',
Expand Down
35 changes: 34 additions & 1 deletion packages/event-recurrence/src/__test__/events-facade.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ describe('Events facade for recurrence plugin', () => {
})

describe('Updating events', () => {
it('should update an event with rrule', () => {
it('should remove an rrule', () => {
const $app = __createAppWithViews__()
const plugin = createEventUpdaterPlugin()
plugin.init!($app)
Expand All @@ -160,6 +160,39 @@ describe('Events facade for recurrence plugin', () => {
plugin.update({ id: '1', start: '2021-01-01', end: '2021-01-01' })
expect($app.calendarEvents.list.value.length).toBe(1)
})

it('should update an event with an rrule', () => {
const $app = __createAppWithViews__({
events: [
{
id: '657436747',
title: 'tjena tjena',
start: '2024-02-04',
end: '2024-02-04',
},
],
})
const plugin = createEventUpdaterPlugin()
plugin.init!($app)
expect($app.calendarEvents.list.value.length).toBe(1)

const event = {
id: '1',
start: '2021-01-01',
end: '2021-01-01',
rrule: 'FREQ=WEEKLY;COUNT=3',
}
plugin.add(event)
expect($app.calendarEvents.list.value.length).toBe(4)

plugin.update({
id: '1',
start: '2021-01-01',
end: '2021-01-01',
rrule: 'FREQ=WEEKLY;COUNT=3',
})
expect($app.calendarEvents.list.value.length).toBe(4)
})
})

describe('Setting the whole list of events', () => {
Expand Down
20 changes: 5 additions & 15 deletions packages/event-recurrence/src/util/stateful/events-facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,20 @@ export class EventsFacadeImpl implements EventsFacade {
}

update(event: CalendarEventExternal): void {
this.removeCopiesForEvent(event)
const eventIndex = (
this.$app.calendarEvents.list.value as AugmentedEvent[]
).findIndex((e) => e.id === event.id && !e.isCopy)
const copiedEvents = [...this.$app.calendarEvents.list.value]
this.removeOriginalAndCopiesForId(event.id)
const updatedEvent = externalEventToInternal(event, this.$app.config)
copiedEvents.splice(eventIndex, 1, updatedEvent)
const copiedEvents = [...this.$app.calendarEvents.list.value, updatedEvent]
const rrule = (updatedEvent as AugmentedEvent)._getForeignProperties().rrule
if (rrule) {
copiedEvents.push(
...createRecurrencesForEvent(
this.$app,
event as AugmentedEvent,
rrule as string
)
...createRecurrencesForEvent(this.$app, updatedEvent, rrule as string)
)
}
this.$app.calendarEvents.list.value = copiedEvents
}

private removeCopiesForEvent(event: CalendarEventExternal) {
private removeOriginalAndCopiesForId(eventId: EventId) {
this.$app.calendarEvents.list.value =
this.$app.calendarEvents.list.value.filter(
(e) => e.id !== event.id && !e.isCopy
)
this.$app.calendarEvents.list.value.filter((e) => e.id !== eventId)
}
}

0 comments on commit b229acb

Please sign in to comment.