Created
January 24, 2021 08:15
-
-
Save dnswd/77c043c655f7480d4c46c642e10c27e7 to your computer and use it in GitHub Desktop.
Generate google calendar event link
This file contains hidden or 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
/** | |
* Encode URL params | |
* Credit: https://stackoverflow.com/a/12040639 | |
* @param { Object } data Javascript object | |
* @return { String } Encoded URL Parameter | |
*/ | |
function encodeQueryData(data) { | |
return encodeURI(Object.keys(data).map((key) => [key, data[key]].map(encodeURIComponent).join('=')).join('&')); | |
} | |
/** | |
* Convert Date object into YYYYMMDDTHHmmSSZ date format. | |
* @param { Date } date a Date object | |
* @return { String } time in YYYYMMDDTHHmmSSZ date format | |
*/ | |
function formatTime(date) { | |
return date.toISOString().replace(/-|:|\.\d+/g, ''); | |
} | |
function getEndTime(event) { | |
return event.end | |
? formatTime(event.end) | |
: formatTime(new Date(event.start.getTime() + (event.duration * 60 * 1000))); | |
} | |
export function getGoogleCalendar(event) { | |
const queries = { | |
action: 'TEMPLATE', | |
dates: [formatTime(event.start), formatTime(getEndTime(event))].join('/'), | |
}; | |
if (event.title) queries.text = event.title; | |
if (event.description) queries.details = event.description; | |
if (event.guests) queries.add = event.guests.join(','); | |
return ['http://www.google.com/calendar/event', encodeQueryData(queries)].join('?'); | |
} | |
export default getGoogleCalendar; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment