Skip to content

Commit

Permalink
FIX: refactoring after code review
Browse files Browse the repository at this point in the history
  • Loading branch information
lis2 committed Nov 28, 2023
1 parent 29d570b commit 47f05f5
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
19 changes: 12 additions & 7 deletions app/assets/javascripts/discourse/app/lib/download-calendar.js
Expand Up @@ -3,7 +3,7 @@ import User from "discourse/models/user";
import { getOwnerWithFallback } from "discourse-common/lib/get-owner";
import getURL from "discourse-common/lib/get-url";

export function downloadCalendar(title, dates, recurrenceRule) {
export function downloadCalendar(title, dates, recurrenceRule = null) {
const currentUser = User.current();

const formattedDates = formatDates(dates);
Expand Down Expand Up @@ -39,16 +39,21 @@ export function downloadIcs(title, dates, recurrenceRule) {

export function downloadGoogle(title, dates, recurrenceRule) {
dates.forEach((date) => {
const encodedTitle = encodeURIComponent(title);
let link = `https://www.google.com/calendar/event?action=TEMPLATE&text=${encodedTitle}&dates=${_formatDateForGoogleApi(
date.startsAt
)}/${_formatDateForGoogleApi(date.endsAt)}`;
const link = new URL("https://www.google.com/calendar/event");
link.searchParams.append("action", "TEMPLATE");
link.searchParams.append("text", title);
link.searchParams.append(
"dates",
`${_formatDateForGoogleApi(date.startsAt)}/${_formatDateForGoogleApi(
date.endsAt
)}`
);

if (recurrenceRule) {
link = link + `&recur=RRULE:${recurrenceRule}`;
link.searchParams.append("recur", `RRULE:${recurrenceRule}`);
}

window.open(getURL(link).trim(), "_blank", "noopener", "noreferrer");
window.open(getURL(link.href).trim(), "_blank", "noopener", "noreferrer");
});
}

Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/discourse/app/lib/plugin-api.js
Expand Up @@ -1835,7 +1835,7 @@ class PluginApi {
* ```
*
*/
downloadCalendar(title, dates, recurrenceRule) {
downloadCalendar(title, dates, recurrenceRule = null) {
downloadCalendar(title, dates, recurrenceRule);
}

Expand Down
Expand Up @@ -16,7 +16,7 @@ module("Unit | Utility | download-calendar", function (hooks) {
sinon.stub(win, "focus");
});

test("correct data for Ics", function (assert) {
test("correct data for ICS", function (assert) {
const now = moment.tz("2022-04-04 23:15", "Europe/Paris").valueOf();
sinon.useFakeTimers({
now,
Expand Down Expand Up @@ -46,7 +46,7 @@ END:VCALENDAR`
);
});

test("correct data for Ics when recurring event", function (assert) {
test("correct data for ICS when recurring event", function (assert) {
const now = moment.tz("2022-04-04 23:15", "Europe/Paris").valueOf();
sinon.useFakeTimers({
now,
Expand Down Expand Up @@ -92,7 +92,7 @@ END:VCALENDAR`
]);
assert.ok(
window.open.calledWith(
"https://www.google.com/calendar/event?action=TEMPLATE&text=event&dates=20211012T150000Z/20211012T160000Z",
"https://www.google.com/calendar/event?action=TEMPLATE&text=event&dates=20211012T150000Z%2F20211012T160000Z",
"_blank",
"noopener",
"noreferrer"
Expand All @@ -113,7 +113,7 @@ END:VCALENDAR`
);
assert.ok(
window.open.calledWith(
"https://www.google.com/calendar/event?action=TEMPLATE&text=event&dates=20211012T150000Z/20211012T160000Z&recur=RRULE:FREQ=DAILY;BYDAY=MO,TU,WE,TH,FR",
"https://www.google.com/calendar/event?action=TEMPLATE&text=event&dates=20211012T150000Z%2F20211012T160000Z&recur=RRULE%3AFREQ%3DDAILY%3BBYDAY%3DMO%2CTU%2CWE%2CTH%2CFR",
"_blank",
"noopener",
"noreferrer"
Expand Down
2 changes: 1 addition & 1 deletion docs/CHANGELOG-JAVASCRIPT-PLUGIN-API.md
Expand Up @@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Added `recurrence rule` option to `downloadCalendar`, this can be used to set recurring event in the calendar.
- Added `recurrenceRule` option to `downloadCalendar`, this can be used to set recurring events in the calendar. Rule syntax can be found at https://datatracker.ietf.org/doc/html/rfc5545#section-3.3.10.

## [1.15.0] - 2023-10-18

Expand Down
Expand Up @@ -90,7 +90,7 @@ acceptance(
assert.deepEqual(
[...arguments],
[
`https://www.google.com/calendar/event?action=TEMPLATE&text=title%20to%20trim&dates=${startDate}T180000Z/${startDate}T190000Z`,
`https://www.google.com/calendar/event?action=TEMPLATE&text=title+to+trim&dates=${startDate}T180000Z%2F${startDate}T190000Z`,
"_blank",
"noopener",
"noreferrer",
Expand Down

0 comments on commit 47f05f5

Please sign in to comment.