2
0
Эх сурвалжийг харах

fix ics event source does not update on refreshEvents. fixes #6194

Adam Shaw 4 жил өмнө
parent
commit
c522ad1066

+ 2 - 1
CHANGELOG.md

@@ -1,9 +1,10 @@
 
-v5.5.2
+v5.6.0
 ------
 
 - feature: icalendar events receive URL (#6173)
 - feature: icalendar events receive location, organizer, description in extendedProps (#6097)
+- fix: icalendar event source does not update on refreshEvents (#6194)
 - fix: business hours per resource do not fill row height with expandRows (#6134)
 - fix: icalendar recurring events ignoring count rule (#6190)
 - fix: icalendar recurring timed-events with wrong times (#6139, #6106)

+ 31 - 1
packages/__tests__/src/icalendar/month-view.ts

@@ -144,12 +144,42 @@ describe('addICalEventSource with month view', () => {
     )
   })
 
+  it('calling refetchEvents request ical feed again', (done) => {
+    const feedUrl = '/mock.ics'
+    let fetchCnt = 0
+
+    XHRMock.get(feedUrl, (req, res) => {
+      fetchCnt++
+      return res.status(200)
+        .header('content-type', ICAL_MIME_TYPE)
+        .body(oneHourMeeting)
+    })
+
+    const calendar = initCalendar({
+      events: {
+        url: feedUrl,
+        format: 'ics',
+      }
+    })
+
+    setTimeout(() => {
+      expect(fetchCnt).toBe(1)
+      expect(calendar.getEvents().length).toBe(1)
+      calendar.refetchEvents()
+
+      setTimeout(() => {
+        expect(fetchCnt).toBe(2)
+        expect(calendar.getEvents().length).toBe(1)
+        done()
+      }, 100)
+    }, 100)
+  })
+
   function loadICalendarWith(rawICal: string, assertions: () => void, calendarSetup?: (source: EventSourceInput) => void) {
     const feedUrl = '/mock.ics'
 
     XHRMock.get(feedUrl, (req, res) => {
       expect(req.url().query).toEqual({})
-
       return res.status(200)
         .header('content-type', ICAL_MIME_TYPE)
         .body(rawICal)

+ 1 - 1
packages/common/src/CalendarApi.tsx

@@ -498,7 +498,7 @@ export class CalendarApi {
   }
 
   refetchEvents() {
-    this.dispatch({ type: 'FETCH_EVENT_SOURCES' })
+    this.dispatch({ type: 'FETCH_EVENT_SOURCES', isRefetch: true })
   }
 
   // Scroll

+ 1 - 0
packages/common/src/api/EventSourceApi.ts

@@ -22,6 +22,7 @@ export class EventSourceApi {
     this.context.dispatch({
       type: 'FETCH_EVENT_SOURCES',
       sourceIds: [this.internalEventSource.sourceId],
+      isRefetch: true
     })
   }
 

+ 1 - 1
packages/common/src/reducers/Action.ts

@@ -31,7 +31,7 @@ export type Action =
   { type: 'REMOVE_EVENT_SOURCE', sourceId: string } |
   { type: 'REMOVE_ALL_EVENT_SOURCES' } |
 
-  { type: 'FETCH_EVENT_SOURCES', sourceIds?: string[] } | // if no sourceIds, fetch all
+  { type: 'FETCH_EVENT_SOURCES', sourceIds?: string[], isRefetch?: boolean } | // if no sourceIds, fetch all
 
   { type: 'RECEIVE_EVENTS', sourceId: string, fetchId: string, fetchRange: DateRange | null, rawEvents: EventInput[] } |
   {

+ 7 - 2
packages/common/src/reducers/eventSources.ts

@@ -50,6 +50,7 @@ export function reduceEventSources(
           arrayToHash((action as any).sourceIds) :
           excludeStaticSources(eventSources, context),
         activeRange,
+        action.isRefetch || false,
         context,
       )
 
@@ -72,6 +73,7 @@ export function reduceEventSourcesNewTimeZone(eventSources: EventSourceHash, dat
     eventSources,
     excludeStaticSources(eventSources, context),
     activeRange,
+    true,
     context,
   )
 }
@@ -114,6 +116,7 @@ function fetchDirtySources(sourceHash: EventSourceHash, fetchRange: DateRange, c
     sourceHash,
     filterHash(sourceHash, (eventSource) => isSourceDirty(eventSource, fetchRange, context)),
     fetchRange,
+    false,
     context,
   )
 }
@@ -133,6 +136,7 @@ function fetchSourcesByIds(
   prevSources: EventSourceHash,
   sourceIdHash: { [sourceId: string]: any },
   fetchRange: DateRange,
+  isRefetch: boolean,
   context: CalendarContext,
 ): EventSourceHash {
   let nextSources: EventSourceHash = {}
@@ -141,7 +145,7 @@ function fetchSourcesByIds(
     let source = prevSources[sourceId]
 
     if (sourceIdHash[sourceId]) {
-      nextSources[sourceId] = fetchSource(source, fetchRange, context)
+      nextSources[sourceId] = fetchSource(source, fetchRange, isRefetch, context)
     } else {
       nextSources[sourceId] = source
     }
@@ -150,7 +154,7 @@ function fetchSourcesByIds(
   return nextSources
 }
 
-function fetchSource(eventSource: EventSource<any>, fetchRange: DateRange, context: CalendarContext) {
+function fetchSource(eventSource: EventSource<any>, fetchRange: DateRange, isRefetch: boolean, context: CalendarContext) {
   let { options, calendarApi } = context
   let sourceDef = context.pluginHooks.eventSourceDefs[eventSource.sourceDefId]
   let fetchId = guid()
@@ -159,6 +163,7 @@ function fetchSource(eventSource: EventSource<any>, fetchRange: DateRange, conte
     {
       eventSource,
       range: fetchRange,
+      isRefetch,
       context,
     },
     (res) => { // success callback

+ 1 - 0
packages/common/src/structs/event-source.ts

@@ -41,6 +41,7 @@ export type EventSourceFetcher<Meta> = (
   arg: {
     eventSource: EventSource<Meta>
     range: DateRange
+    isRefetch: boolean
     context: CalendarContext
   },
   success: (res: { rawEvents: EventInput[], xhr?: XMLHttpRequest }) => void,

+ 9 - 5
packages/icalendar/src/main.ts

@@ -34,7 +34,7 @@ let eventSourceDef: EventSourceDef<ICalFeedMeta> = {
     let { meta } = arg.eventSource
     let { internalState } = meta
 
-    function handleIcalEvents(errorMessage, iCalEvents, xhr) {
+    function handleICalEvents(errorMessage, iCalEvents, xhr) {
       if (errorMessage) {
         onFailure({ message: errorMessage, xhr })
       } else {
@@ -42,10 +42,14 @@ let eventSourceDef: EventSourceDef<ICalFeedMeta> = {
       }
     }
 
-    if (!internalState) {
+    /*
+    NOTE: isRefetch is a HACK. we would do the recurring-expanding in a separate plugin hook,
+    but we couldn't leverage built-in allDay-guessing, among other things.
+    */
+    if (!internalState || arg.isRefetch) {
       internalState = meta.internalState = { // our ghetto Promise
         completed: false,
-        callbacks: [handleIcalEvents],
+        callbacks: [handleICalEvents],
         errorMessage: '',
         iCalEvents: [],
         xhr: null,
@@ -77,9 +81,9 @@ let eventSourceDef: EventSourceDef<ICalFeedMeta> = {
         },
       )
     } else if (!internalState.completed) {
-      internalState.callbacks.push(handleIcalEvents)
+      internalState.callbacks.push(handleICalEvents)
     } else {
-      handleIcalEvents(internalState.errorMessage, internalState.iCalEvents, internalState.xhr)
+      handleICalEvents(internalState.errorMessage, internalState.iCalEvents, internalState.xhr)
     }
   },
 }