Skip to content

A platform for community discussion. Free, open, simple.

UX: Convert "Add to calendar" modal to FormKit (#43717)

Martin Brennan07edf35a4986
diff --git a/app/assets/stylesheets/common/components/download-calendar.scss b/app/assets/stylesheets/common/components/download-calendar.scss
index fd8693db0c6..63cf3f1bbcb 100644
--- a/app/assets/stylesheets/common/components/download-calendar.scss
+++ b/app/assets/stylesheets/common/components/download-calendar.scss
@@ -1,7 +1,3 @@
-.download-calendar-modal .remember {
- margin-top: 2em;
-}
-
.download-calendar {
color: var(--primary-med-or-secondary-med);
}
diff --git a/app/models/user_option.rb b/app/models/user_option.rb
index 1c05ce17053..567efbf2bab 100644
--- a/app/models/user_option.rb
+++ b/app/models/user_option.rb
@@ -41,7 +41,9 @@ class UserOption < ActiveRecord::Base
scope :human_users, -> { where("user_id > 0") }
- enum :default_calendar, { none_selected: 0, ics: 1, google: 2 }, scopes: false
+ enum :default_calendar,
+ { none_selected: 0, ics: 1, google: 2, outlook: 3, apple: 4 },
+ scopes: false
enum :push_notification_level, { none: 0, all: 1, chat_only: 2 }, prefix: true, scopes: false
enum :send_shortcut, { enter: 0, meta_enter: 1 }, prefix: true, scopes: false
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 190a56e1df4..b95e1b295aa 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -6164,16 +6164,21 @@ en:
favorite_count: "%{count}/%{max} badges marked as favorite"
download_calendar:
- title: "Download calendar"
+ title: "Add to calendar"
+ calendar: "Calendar"
save_ics: "Download .ics file"
save_google: "Add to Google calendar"
+ save_outlook: "Add to Microsoft Outlook"
+ save_apple: "Add to Apple Calendar"
remember: "Don’t ask me again"
- remember_explanation: "(you can change this preference in your user prefs)"
+ remember_explanation: "You can change this preference later in your profile preferences."
download: "Download"
default_calendar: "Default calendar"
default_calendar_instruction: "Determine which calendar should be used when dates are saved"
add_to_calendar: "Add to calendar"
google: "Google Calendar"
+ outlook: "Microsoft Outlook"
+ apple: "Apple Calendar"
ics: "ICS"
default_title: "Scheduled Event"
tagging:
diff --git a/frontend/discourse/app/components/modal/download-calendar.gjs b/frontend/discourse/app/components/modal/download-calendar.gjs
index 27d8225f3a8..1f4cffaac68 100644
--- a/frontend/discourse/app/components/modal/download-calendar.gjs
+++ b/frontend/discourse/app/components/modal/download-calendar.gjs
@@ -1,59 +1,49 @@
import Component from "@glimmer/component";
-import { tracked } from "@glimmer/tracking";
-import { Input } from "@ember/component";
-import { fn } from "@ember/helper";
import { action } from "@ember/object";
import { service } from "@ember/service";
-import { downloadGoogle, downloadIcs } from "discourse/lib/download-calendar";
+import Form from "discourse/components/form";
+import { addToCalendar } from "discourse/lib/download-calendar";
import DButton from "discourse/ui-kit/d-button";
import DModal from "discourse/ui-kit/d-modal";
-import DRadioButton from "discourse/ui-kit/d-radio-button";
+import DModalCancel from "discourse/ui-kit/d-modal-cancel";
+import dIcon from "discourse/ui-kit/helpers/d-icon";
import { i18n } from "discourse-i18n";
export default class DownloadCalendar extends Component {
@service currentUser;
- @tracked selectedCalendar = "ics";
- @tracked remember = false;
+ formData = { calendar: "ics", remember: false };
+ #formApi;
@action
- downloadCalendar() {
- if (this.remember) {
- this.currentUser.set(
- "user_option.default_calendar",
- this.selectedCalendar
- );
+ handleSubmit(data) {
+ if (data.remember) {
+ this.currentUser.set("user_option.default_calendar", data.calendar);
this.currentUser.save(["default_calendar"]);
}
- if (this.selectedCalendar === "ics") {
- downloadIcs(
- this.args.model.calendar.title,
- this.args.model.calendar.dates,
- {
- rrule: this.args.model.calendar.rrule,
- location: this.args.model.calendar.location,
- details: this.args.model.calendar.details,
- timezone: this.args.model.calendar.timezone,
- }
- );
- } else {
- downloadGoogle(
- this.args.model.calendar.title,
- this.args.model.calendar.dates,
- {
- rrule: this.args.model.calendar.rrule,
- location: this.args.model.calendar.location,
- details: this.args.model.calendar.details,
- timezone: this.args.model.calendar.timezone,
- }
- );
- }
+
+ addToCalendar(
+ data.calendar,
+ this.args.model.calendar.title,
+ this.args.model.calendar.dates,
+ {
+ rrule: this.args.model.calendar.rrule,
+ location: this.args.model.calendar.location,
+ details: this.args.model.calendar.details,
+ timezone: this.args.model.calendar.timezone,
+ }
+ );
this.args.closeModal();
}
@action
- selectCalendar(calendar) {
- this.selectedCalendar = calendar;
+ registerFormApi(api) {
+ this.#formApi = api;
+ }
+
+ @action
+ submit() {
+ this.#formApi.submit();
}
<template>
@@ -63,54 +53,70 @@ export default class DownloadCalendar extends Component {
@title={{i18n "download_calendar.title"}}
>
<:body>
- <div class="control-group">
- <div class="ics">
- <label class="radio" for="ics">
- <DRadioButton
- id="ics"
- @name="select-calendar"
- @onChange={{fn this.selectCalendar "ics"}}
- @selection={{this.selectedCalendar}}
- @value="ics"
- />
- {{i18n "download_calendar.save_ics"}}
- </label>
- </div>
- <div class="google">
- <label class="radio" for="google">
- <DRadioButton
- id="google"
- @name="select-calendar"
- @onChange={{fn this.selectCalendar "google"}}
- @selection={{this.selectedCalendar}}
- @value="google"
- />
- {{i18n "download_calendar.save_google"}}
- </label>
- </div>
- </div>
+ <Form
+ @data={{this.formData}}
+ @onRegisterApi={{this.registerFormApi}}
+ @onSubmit={{this.handleSubmit}}
+ as |form|
+ >
+ <form.Field
+ @format="full"
+ @name="calendar"
+ @title={{i18n "download_calendar.calendar"}}
+ @type="radio-group"
+ @validation="required"
+ as |field|
+ >
+ <field.Control as |radioGroup|>
+ <radioGroup.Radio @value="ics" as |radio|>
+ <radio.Title>
+ {{dIcon "download"}}
+ {{i18n "download_calendar.save_ics"}}
+ </radio.Title>
+ </radioGroup.Radio>
+ <radioGroup.Radio @value="google" as |radio|>
+ <radio.Title>
+ {{dIcon "fab-google"}}
+ {{i18n "download_calendar.save_google"}}
+ </radio.Title>
+ </radioGroup.Radio>
+ <radioGroup.Radio @value="outlook" as |radio|>
+ <radio.Title>
+ {{dIcon "fab-microsoft"}}
+ {{i18n "download_calendar.save_outlook"}}
+ </radio.Title>
+ </radioGroup.Radio>
+ <radioGroup.Radio @value="apple" as |radio|>
+ <radio.Title>
+ {{dIcon "fab-apple"}}
+ {{i18n "download_calendar.save_apple"}}
+ </radio.Title>
+ </radioGroup.Radio>
+ </field.Control>
+ </form.Field>
- {{#if this.currentUser}}
- <div class="control-group remember">
- <label class="checkbox-label">
- <Input @checked={{this.remember}} @type="checkbox" />
- <span>{{i18n "download_calendar.remember"}}</span>
- </label>
- <span>{{i18n "download_calendar.remember_explanation"}}</span>
- </div>
- {{/if}}
+ {{#if this.currentUser}}
+ <form.Field
+ @format="full"
+ @name="remember"
+ @title={{i18n "download_calendar.remember"}}
+ @type="checkbox"
+ as |field|
+ >
+ <field.Control>
+ {{i18n "download_calendar.remember_explanation"}}
+ </field.Control>
+ </form.Field>
+ {{/if}}
+ </Form>
</:body>
<:footer>
<DButton
class="btn-primary"
- @action={{this.downloadCalendar}}
- @label="download_calendar.download"
- />
- <DButton
- class="btn-flat d-modal-cancel"
- @action={{@closeModal}}
- @label="cancel"
+ @action={{this.submit}}
+ @label="download_calendar.add_to_calendar"
/>
+ <DModalCancel @close={{@closeModal}} />
</:footer>
</DModal>
</template>
diff --git a/frontend/discourse/app/controllers/preferences/profile.js b/frontend/discourse/app/controllers/preferences/profile.js
index 832e7f81d5a..cb1faa01699 100644
--- a/frontend/discourse/app/controllers/preferences/profile.js
+++ b/frontend/discourse/app/controllers/preferences/profile.js
@@ -17,6 +17,8 @@ export default class ProfileController extends Controller {
calendarOptions = [
{ name: i18n("download_calendar.google"), value: "google" },
+ { name: i18n("download_calendar.outlook"), value: "outlook" },
+ { name: i18n("download_calendar.apple"), value: "apple" },
{ name: i18n("download_calendar.ics"), value: "ics" },
];
diff --git a/frontend/discourse/app/lib/download-calendar.js b/frontend/discourse/app/lib/download-calendar.js
index a155f0ee143..c92bde8fa31 100644
--- a/frontend/discourse/app/lib/download-calendar.js
+++ b/frontend/discourse/app/lib/download-calendar.js
@@ -10,16 +10,27 @@ export function downloadCalendar(title, dates, options = {}) {
const formattedDates = formatDates(dates);
title = (title || i18n("download_calendar.default_title")).trim();
- switch (currentUser?.user_option.default_calendar) {
- case "ics":
- downloadIcs(title, formattedDates, options);
- break;
+ const defaultCalendar = currentUser?.user_option.default_calendar;
+
+ if (["ics", "google", "outlook", "apple"].includes(defaultCalendar)) {
+ addToCalendar(defaultCalendar, title, formattedDates, options);
+ } else {
+ _displayModal(title, formattedDates, options);
+ }
+}
+
+export function addToCalendar(calendar, title, dates, options = {}) {
+ switch (calendar) {
case "google":
- downloadGoogle(title, formattedDates, options);
+ downloadGoogle(title, dates, options);
+ break;
+ case "outlook":
+ downloadOutlook(title, dates, options);
+ break;
+ case "apple":
+ case "ics":
+ downloadIcs(title, dates, options);
break;
- case "none_selected":
- default:
- _displayModal(title, formattedDates, options);
}
}
@@ -75,6 +86,51 @@ export function downloadGoogle(title, dates, options = {}) {
});
}
+export function downloadOutlook(title, dates, options = {}) {
+ const parsedRrule = _parseRRule(options.rrule);
+
+ if (parsedRrule && _hasFreq(parsedRrule)) {
+ downloadIcs(title, dates, options);
+ return;
+ }
+
+ dates.forEach((date) => {
+ const link = new URL(
+ "https://outlook.live.com/calendar/0/deeplink/compose"
+ );
+ link.searchParams.append("path", "/calendar/action/compose");
+ link.searchParams.append("rru", "addevent");
+ link.searchParams.append("subject", title);
+
+ if (date.allDay) {
+ const { startDate, endDate } = _allDayMoments(date);
+ link.searchParams.append("startdt", startDate.format("YYYY-MM-DD"));
+ link.searchParams.append("enddt", endDate.format("YYYY-MM-DD"));
+ link.searchParams.append("allday", "true");
+ } else {
+ link.searchParams.append(
+ "startdt",
+ _formatDateForOutlookApi(date.startsAt, date.timezone)
+ );
+ link.searchParams.append(
+ "enddt",
+ _formatDateForOutlookApi(date.endsAt, date.timezone)
+ );
+ link.searchParams.append("allday", "false");
+ }
+
+ if (options.location) {
+ link.searchParams.append("location", options.location);
+ }
+
+ if (options.details) {
+ link.searchParams.append("body", options.details);
+ }
+
+ window.open(getURL(link.href).trim(), "_blank", "noopener", "noreferrer");
+ });
+}
+
export function formatDates(dates) {
return dates.map((date) => {
const formatted = {
@@ -467,3 +523,8 @@ function _formatDateForGoogleApi(date, timezone) {
const momentDate = timezone ? moment.tz(date, timezone) : moment(date);
return momentDate.utc().format("YYYYMMDD[T]HHmmss[Z]");
}
+
+function _formatDateForOutlookApi(date, timezone) {
+ const momentDate = timezone ? moment.tz(date, timezone) : moment(date);
+ return momentDate.toISOString();
+}
diff --git a/frontend/discourse/app/lib/plugin-api.gjs b/frontend/discourse/app/lib/plugin-api.gjs
index a9a9e3dcf7d..2caff693b0b 100644
--- a/frontend/discourse/app/lib/plugin-api.gjs
+++ b/frontend/discourse/app/lib/plugin-api.gjs
@@ -2590,7 +2590,7 @@ class _PluginApi {
}
/**
- * Download calendar modal which allow to pick between ICS and Google Calendar. Optionally, recurrence rule can be specified - https://datatracker.ietf.org/doc/html/rfc5545#section-3.3.10
+ * Add an event to an ICS-compatible app, Google Calendar, Microsoft Outlook, or Apple Calendar. Optionally, recurrence rule can be specified - https://datatracker.ietf.org/doc/html/rfc5545#section-3.3.10
*
* ```javascript
* api.downloadCalendar("title of the event",
diff --git a/frontend/discourse/tests/acceptance/user-preferences-profile-test.js b/frontend/discourse/tests/acceptance/user-preferences-profile-test.js
index 354bb0b5f34..f34cb52936d 100644
--- a/frontend/discourse/tests/acceptance/user-preferences-profile-test.js
+++ b/frontend/discourse/tests/acceptance/user-preferences-profile-test.js
@@ -3,6 +3,7 @@ import { test } from "qunit";
import { cloneJSON } from "discourse/lib/object";
import userFixtures from "discourse/tests/fixtures/user-fixtures";
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
+import selectKit from "discourse/tests/helpers/select-kit-helper";
acceptance(
"User - Preferences - Profile - Hide Profile Allowed",
@@ -129,6 +130,16 @@ acceptance(
assert
.dom("#user-default-calendar")
.exists("option to change default calendar");
+
+ const calendarSelector = selectKit("#user-default-calendar");
+ await calendarSelector.expand();
+
+ for (const calendar of ["google", "outlook", "apple", "ics"]) {
+ assert.true(
+ calendarSelector.rowByValue(calendar).exists(),
+ `${calendar} is available as a default calendar`
+ );
+ }
});
}
);
diff --git a/frontend/discourse/tests/unit/lib/download-calendar-test.js b/frontend/discourse/tests/unit/lib/download-calendar-test.js
index c825c490e84..2bd90d74203 100644
--- a/frontend/discourse/tests/unit/lib/download-calendar-test.js
+++ b/frontend/discourse/tests/unit/lib/download-calendar-test.js
@@ -3,6 +3,7 @@ import { module, test } from "qunit";
import sinon from "sinon";
import {
downloadGoogle,
+ downloadOutlook,
formatDates,
generateIcsData,
} from "discourse/lib/download-calendar";
@@ -428,6 +429,71 @@ module("Unit | Utility | download-calendar", function (hooks) {
);
});
+ test("correct url for Outlook", function (assert) {
+ downloadOutlook(
+ "event & lunch",
+ [
+ {
+ startsAt: "2021-10-12T18:30:00",
+ endsAt: "2021-10-12T21:00:00",
+ timezone: "America/New_York",
+ },
+ ],
+ { details: "Cool", location: "New York" }
+ );
+
+ const [url, target, ...features] = window.open.getCall(0).args;
+ const link = new URL(url);
+
+ assert.strictEqual(
+ link.origin + link.pathname,
+ "https://outlook.live.com/calendar/0/deeplink/compose"
+ );
+ assert.deepEqual(Object.fromEntries(link.searchParams), {
+ path: "/calendar/action/compose",
+ rru: "addevent",
+ subject: "event & lunch",
+ startdt: "2021-10-12T22:30:00.000Z",
+ enddt: "2021-10-13T01:00:00.000Z",
+ allday: "false",
+ location: "New York",
+ body: "Cool",
+ });
+ assert.strictEqual(target, "_blank");
+ assert.deepEqual(features, ["noopener", "noreferrer"]);
+ });
+
+ test("all-day Outlook url uses an exclusive date-only range", function (assert) {
+ downloadOutlook("all day event", [
+ { startsAt: "2026-03-12", endsAt: "2026-03-14", allDay: true },
+ ]);
+
+ const link = new URL(window.open.getCall(0).args[0]);
+
+ assert.strictEqual(link.searchParams.get("startdt"), "2026-03-12");
+ assert.strictEqual(link.searchParams.get("enddt"), "2026-03-15");
+ assert.strictEqual(link.searchParams.get("allday"), "true");
+ });
+
+ test("recurring Outlook events fall back to ICS", function (assert) {
+ sinon.stub(URL, "createObjectURL").returns("blob:calendar");
+ sinon.stub(HTMLAnchorElement.prototype, "click");
+
+ downloadOutlook(
+ "event",
+ [
+ {
+ startsAt: "2021-10-12T15:00:00.000Z",
+ endsAt: "2021-10-12T16:00:00.000Z",
+ },
+ ],
+ { rrule: "FREQ=WEEKLY;BYDAY=TU" }
+ );
+
+ assert.true(HTMLAnchorElement.prototype.click.calledOnce);
+ assert.false(window.open.called);
+ });
+
test("calculates end date when none given", function (assert) {
let dates = formatDates([{ startsAt: "2021-10-12T15:00:00.000Z" }]);
assert.deepEqual(
diff --git a/plugins/discourse-local-dates/test/javascripts/acceptance/download-calendar-test.js b/plugins/discourse-local-dates/test/javascripts/acceptance/download-calendar-test.js
index 5b77baccf94..7db068244cc 100644
--- a/plugins/discourse-local-dates/test/javascripts/acceptance/download-calendar-test.js
+++ b/plugins/discourse-local-dates/test/javascripts/acceptance/download-calendar-test.js
@@ -34,7 +34,11 @@ acceptance(
"it should display modal to select calendar"
);
- assert.dom(".control-group.remember").exists();
+ assert.form().field("calendar").hasValue("ics");
+ assert
+ .dom('[data-name="calendar"] input[type="radio"]')
+ .exists({ count: 4 });
+ assert.form().field("remember").exists();
});
}
);
@@ -64,7 +68,8 @@ acceptance("Download calendar as an anonymous user", function (needs) {
"it should display modal to select calendar"
);
- assert.dom(".control-group.remember").doesNotExist();
+ assert.form().field("calendar").hasValue("ics");
+ assert.form().field("remember").doesNotExist();
});
});
@@ -97,7 +102,7 @@ acceptance(
acceptance(
"Download calendar with default calendar option set",
function (needs) {
- needs.user({ "user_option.default_calendar": "google" });
+ needs.user({ "user_option.default_calendar": "outlook" });
needs.settings({ discourse_local_dates_enabled: true });
needs.pretender((server, helper) => {
const response = cloneJSON(fixturesByUrl["/t/281.json"]);
@@ -118,15 +123,19 @@ acceptance(
await visit("/t/local-dates/281");
sinon.stub(window, "open").callsFake(function () {
- assert.deepEqual(
- [...arguments],
- [
- `https://www.google.com/calendar/event?action=TEMPLATE&text=title+to+trim&dates=${startDate}T180000Z%2F${startDate}T190000Z`,
- "_blank",
- "noopener",
- "noreferrer",
- ]
+ const [url, target, ...features] = arguments;
+ const link = new URL(url);
+
+ assert.strictEqual(
+ link.origin + link.pathname,
+ "https://outlook.live.com/calendar/0/deeplink/compose"
+ );
+ assert.strictEqual(
+ link.searchParams.get("startdt"),
+ `${moment(startDate, "YYYYMMDD").format("YYYY-MM-DD")}T18:00:00.000Z`
);
+ assert.strictEqual(target, "_blank");
+ assert.deepEqual(features, ["noopener", "noreferrer"]);
return { focus() {} };
});
diff --git a/spec/models/user_option_spec.rb b/spec/models/user_option_spec.rb
index 88356aaaf52..b1882b40384 100644
--- a/spec/models/user_option_spec.rb
+++ b/spec/models/user_option_spec.rb
@@ -80,6 +80,14 @@ RSpec.describe UserOption do
user = Fabricate(:user)
expect(user.user_option.composition_mode).to eq(UserOption.composition_mode_types[:markdown])
end
+
+ it "stores each supported default calendar" do
+ %w[ics google outlook apple].each do |calendar|
+ user.user_option.update!(default_calendar: calendar)
+
+ expect(user.user_option.reload.default_calendar).to eq(calendar)
+ end
+ end
end
describe "understood languages" do