From 19649123154a413f55b4c97e74a68987e6df2433 Mon Sep 17 00:00:00 2001 From: Alan Guo Xiang Tan Date: Mon, 23 Oct 2023 08:32:04 +0800 Subject: [PATCH] DEV: Switch to new `addComposerToolbarPopupMenuOption` plugin API (#460) Why this change? `api.addToolbarPopupMenuOptionsCallback` has been deprecated in https://github.com/discourse/discourse/commit/913fd3a7b392b492f6344102577960a6eada00ce --- .discourse-compatibility | 1 + .../initializers/add-event-ui-builder.js | 78 ++++++++----------- .../acceptance/post-event-builder-test.js | 19 +++-- 3 files changed, 49 insertions(+), 49 deletions(-) diff --git a/.discourse-compatibility b/.discourse-compatibility index 3fd0806a0..c0d4f6604 100644 --- a/.discourse-compatibility +++ b/.discourse-compatibility @@ -1,3 +1,4 @@ +< 3.2.0.beta2: 6c659020ab5cca4af846b4b78e4ed0b4e06a2bca 3.1.0.beta3: 10077ca904956005f9fa83c3d9fb124b59e8c47b 2.9.0.beta13: b4c366b35d6f9778e54a878aa083348e8a45e86e 2.9.0.beta9: 6f16ae10dc0306d6e1369e0d1414416d7e72141d diff --git a/assets/javascripts/discourse/initializers/add-event-ui-builder.js b/assets/javascripts/discourse/initializers/add-event-ui-builder.js index ef22b6b10..24e45e9c2 100644 --- a/assets/javascripts/discourse/initializers/add-event-ui-builder.js +++ b/assets/javascripts/discourse/initializers/add-event-ui-builder.js @@ -3,50 +3,40 @@ import showModal from "discourse/lib/show-modal"; function initializeEventBuilder(api) { const currentUser = api.getCurrentUser(); - - api.addToolbarPopupMenuOptionsCallback((composerController) => { - if (!currentUser || !currentUser.can_create_discourse_post_event) { - return; - } - - const composerModel = composerController.model; - if ( - composerModel && - !composerModel.replyingToTopic && - (composerModel.topicFirstPost || - composerModel.creatingPrivateMessage || - (composerModel.editingPost && - composerModel.post && - composerModel.post.post_number === 1)) - ) { - return { - label: "discourse_post_event.builder_modal.attach", - id: "insertEvent", - group: "insertions", - icon: "calendar-day", - action: "insertEvent", - }; - } - }); - - api.modifyClass("controller:composer", { - pluginId: "discourse-calendar", - - actions: { - insertEvent() { - const eventModel = this.store.createRecord( - "discourse-post-event-event" - ); - eventModel.set("status", "public"); - eventModel.set("custom_fields", {}); - eventModel.set("starts_at", moment()); - eventModel.set("timezone", moment.tz.guess()); - - showModal("discourse-post-event-builder").setProperties({ - toolbarEvent: this.toolbarEvent, - model: { eventModel }, - }); - }, + const store = api.container.lookup("service:store"); + + api.addComposerToolbarPopupMenuOption({ + action: (toolbarEvent) => { + const eventModel = store.createRecord("discourse-post-event-event"); + eventModel.set("status", "public"); + eventModel.set("custom_fields", {}); + eventModel.set("starts_at", moment()); + eventModel.set("timezone", moment.tz.guess()); + + showModal("discourse-post-event-builder").setProperties({ + toolbarEvent, + model: { eventModel }, + }); + }, + group: "insertions", + icon: "calendar-day", + label: "discourse_post_event.builder_modal.attach", + condition: (composer) => { + if (!currentUser || !currentUser.can_create_discourse_post_event) { + return false; + } + + const composerModel = composer.model; + + return ( + composerModel && + !composerModel.replyingToTopic && + (composerModel.topicFirstPost || + composerModel.creatingPrivateMessage || + (composerModel.editingPost && + composerModel.post && + composerModel.post.post_number === 1)) + ); }, }); } diff --git a/test/javascripts/acceptance/post-event-builder-test.js b/test/javascripts/acceptance/post-event-builder-test.js index fb2416620..73ad9b6fb 100644 --- a/test/javascripts/acceptance/post-event-builder-test.js +++ b/test/javascripts/acceptance/post-event-builder-test.js @@ -1,8 +1,9 @@ -import { acceptance } from "discourse/tests/helpers/qunit-helpers"; -import { test } from "qunit"; import { click, fillIn, visit } from "@ember/test-helpers"; -import selectKit from "discourse/tests/helpers/select-kit-helper"; +import { test } from "qunit"; import sinon from "sinon"; +import { acceptance } from "discourse/tests/helpers/qunit-helpers"; +import selectKit from "discourse/tests/helpers/select-kit-helper"; +import I18n from "discourse-i18n"; acceptance("Post event - composer", function (needs) { needs.user({ admin: true, can_create_discourse_post_event: true }); @@ -21,7 +22,11 @@ acceptance("Post event - composer", function (needs) { await categoryChooser.expand(); await categoryChooser.selectRowByValue(2); await click(".toolbar-popup-menu-options .dropdown-select-box-header"); - await click(".toolbar-popup-menu-options *[data-value='insertEvent']"); + await click( + `.toolbar-popup-menu-options *[data-name='${I18n.t( + "discourse_post_event.builder_modal.attach" + )}']` + ); const modal = ".discourse-post-event-builder-modal"; @@ -87,7 +92,11 @@ acceptance("Post event - composer", function (needs) { await categoryChooser.selectRowByValue(2); await click(".toolbar-popup-menu-options .dropdown-select-box-header"); - await click(".toolbar-popup-menu-options *[data-value='insertEvent']"); + await click( + `.toolbar-popup-menu-options *[data-name='${I18n.t( + "discourse_post_event.builder_modal.attach" + )}']` + ); const modal = ".discourse-post-event-builder-modal";