Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
57 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 45 additions & 39 deletions
84
packages/common/src/event-sources/ical-feed-event-source.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,76 @@ | ||
import { requestJson } from '../util/requestJson' | ||
import { EventSourceDef } from '../structs/event-source-def' | ||
import { __assign } from 'tslib' | ||
import { createPlugin } from '../plugin-system' | ||
import { ICAL_FEED_EVENT_SOURCE_REFINERS } from './ical-feed-event-source-refiners' | ||
|
||
export function requestICal(url: string, successCallback, failureCallback) { | ||
|
||
const xhr = new XMLHttpRequest() | ||
xhr.open('GET', url, true) | ||
|
||
xhr.onload = function() { | ||
if (xhr.status >= 200 && xhr.status < 400) { | ||
|
||
const iCalFeed = xhr.responseText | ||
console.log(iCalFeed) | ||
|
||
successCallback(iCalFeed, xhr) | ||
} else { | ||
failureCallback('Request failed', xhr) | ||
} | ||
} | ||
|
||
xhr.onerror = () => failureCallback('Request failed', xhr) | ||
|
||
xhr.send(null) | ||
} | ||
|
||
|
||
interface ICalFeedMeta { | ||
feedUrl: string | ||
method: string | ||
extraParams?: any | ||
} | ||
|
||
|
||
let eventSourceDef: EventSourceDef<ICalFeedMeta> = { | ||
|
||
parseMeta(refined) { | ||
if (refined.feedUrl) { | ||
return { | ||
feedUrl: refined.feedUrl, | ||
method: (refined.method || 'GET').toUpperCase(), | ||
} | ||
} | ||
return null | ||
}, | ||
|
||
fetch(arg, success, failure) { | ||
let meta: ICalFeedMeta = arg.eventSource.meta | ||
let requestParams = buildRequestParams(meta) | ||
|
||
requestJson( | ||
meta.method, meta.feedUrl, requestParams, | ||
function(_, xhr) { | ||
const rawEvents = [ | ||
{ | ||
title: 'id-123', | ||
start: '2019-04-10T10:30:00Z', | ||
end: '2019-04-13T17:00Z', | ||
}, | ||
] | ||
|
||
success({ rawEvents, xhr }) | ||
}, | ||
function(errorMessage, xhr) { | ||
failure({ message: errorMessage, xhr }) | ||
} | ||
) | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
requestICal(meta.feedUrl, | ||
(_, xhr) => { | ||
const rawEvents = [ | ||
{ | ||
title: 'id-123', | ||
start: '2019-04-10T10:30:00Z', | ||
end: '2019-04-13T17:00Z', | ||
}, | ||
] | ||
|
||
success({ rawEvents, xhr }) | ||
resolve() | ||
}, | ||
(errorMessage, xhr) => { | ||
failure({ message: errorMessage, xhr }) | ||
reject() | ||
}, | ||
) | ||
}) | ||
} | ||
} | ||
|
||
|
||
export const iCalFeedEventSourcePlugin = createPlugin({ | ||
eventSourceRefiners: ICAL_FEED_EVENT_SOURCE_REFINERS, | ||
eventSourceDefs: [ eventSourceDef ] | ||
}) | ||
|
||
|
||
function buildRequestParams(meta: ICalFeedMeta) { | ||
let customRequestParams | ||
let params = {} | ||
|
||
if (typeof meta.extraParams === 'function') { | ||
customRequestParams = meta.extraParams() | ||
} else { | ||
customRequestParams = meta.extraParams || {} | ||
} | ||
|
||
__assign(params, customRequestParams) | ||
|
||
return params | ||
} |