@massds/mayflower-react
Version:
React versions of Mayflower design system UI components
99 lines (75 loc) • 4.12 kB
JavaScript
;
exports.__esModule = true;
exports.getRandomKey = exports.formatTime = exports.calculateDuration = exports.buildUrl = void 0;
var _moment = _interopRequireDefault(require("moment"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
/** The following helper function was implemented following the same logic/strucure
* as react-add-to-calendar helpers class.
* (https://github.com/jasonsalzman/react-add-to-calendar/blob/master/src/helpers/index.js)
*/
var getRandomKey = function getRandomKey() {
var n = Math.floor(Math.random() * 999999999999).toString();
return new Date().getTime().toString() + "_" + n;
};
exports.getRandomKey = getRandomKey;
var formatTime = function formatTime(date) {
var formattedDate = _moment["default"].utc(date).format('YYYYMMDDTHHmmssZ');
return formattedDate.replace('+00:00', 'Z');
};
exports.formatTime = formatTime;
var calculateDuration = function calculateDuration(startTime, endTime) {
// snag parameters and format properly in UTC
var end = _moment["default"].utc(endTime).format('DD/MM/YYYY HH:mm:ss');
var start = _moment["default"].utc(startTime).format('DD/MM/YYYY HH:mm:ss'); // calculate the difference in milliseconds between the start and end times
var difference = (0, _moment["default"])(end, 'DD/MM/YYYY HH:mm:ss').diff((0, _moment["default"])(start, 'DD/MM/YYYY HH:mm:ss')); // convert difference from above to a proper momentJs duration object
var duration = _moment["default"].duration(difference);
return Math.floor(duration.asHours()) + _moment["default"].utc(difference).format(':mm');
};
exports.calculateDuration = calculateDuration;
var buildUrl = function buildUrl(event, type, window) {
var calendarUrl = ''; // allow mobile browsers to open the gmail data URI within native calendar app
// type = (type == "google" && this.isMobile()) ? "outlook" : type;
switch (type) {
case 'google':
calendarUrl = 'https://calendar.google.com/calendar/render';
calendarUrl += '?action=TEMPLATE';
calendarUrl += "&dates=" + formatTime(event.startDate);
calendarUrl += "/" + formatTime(event.endDate);
calendarUrl += "&location=" + encodeURIComponent(event.location);
calendarUrl += "&text=" + encodeURIComponent(event.title);
calendarUrl += "&details=" + encodeURIComponent(event.description);
break;
case 'yahoo':
{
// yahoo doesn't utilize endTime so we need to calulate duration
var duration = calculateDuration(event.startDate, event.endDate);
calendarUrl = 'https://calendar.yahoo.com/?v=60&view=d&type=20';
calendarUrl += "&title=" + encodeURIComponent(event.title);
calendarUrl += "&st=" + formatTime(event.startDate);
calendarUrl += "&dur=" + duration;
calendarUrl += "&desc=" + encodeURIComponent(event.description);
calendarUrl += "&in_loc=" + encodeURIComponent(event.location);
break;
}
case 'outlookcom':
calendarUrl = 'https://outlook.live.com/owa/?rru=addevent';
calendarUrl += "&startdt=" + formatTime(event.startDate);
calendarUrl += "&enddt=" + formatTime(event.endDate);
calendarUrl += "&subject=" + encodeURIComponent(event.title);
calendarUrl += "&location=" + encodeURIComponent(event.location);
calendarUrl += "&body=" + encodeURIComponent(event.description);
calendarUrl += '&allday=false';
calendarUrl += "&uid=" + getRandomKey();
calendarUrl += '&path=/calendar/view/Month';
break;
default:
calendarUrl = ['BEGIN:VCALENDAR', 'VERSION:2.0', 'BEGIN:VEVENT', "URL:" + document.URL, "DTSTART:" + formatTime(event.startDate), "DTEND:" + formatTime(event.endDate), "SUMMARY:" + event.title, "DESCRIPTION:" + event.description, "LOCATION:" + event.location, 'END:VEVENT', 'END:VCALENDAR'].join('\n');
if (window) {
calendarUrl = window.URL.createObjectURL(new Blob([calendarUrl], {
type: 'text/calendar;charset=utf-8'
}));
}
}
return calendarUrl;
};
exports.buildUrl = buildUrl;