From 02c6fa6d246180cd600e775043bae48b792af275 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Fri, 19 Jul 2019 10:20:55 +0200 Subject: [PATCH] DEV: uses new topic-button API (#43) --- .../initializers/extend-for-assigns.js.es6 | 72 ++++++++++--------- .../acceptance/assign-disabled-test.js.es6 | 4 +- .../acceptance/assign-enabled-test.js.es6 | 4 +- 3 files changed, 44 insertions(+), 36 deletions(-) diff --git a/assets/javascripts/discourse-assign/initializers/extend-for-assigns.js.es6 b/assets/javascripts/discourse-assign/initializers/extend-for-assigns.js.es6 index 5b6aac8..15ab267 100644 --- a/assets/javascripts/discourse-assign/initializers/extend-for-assigns.js.es6 +++ b/assets/javascripts/discourse-assign/initializers/extend-for-assigns.js.es6 @@ -11,44 +11,39 @@ import { iconHTML } from "discourse-common/lib/icon-library"; // TODO: This has to be removed when 2.3 becomes the new stable version. import { ListItemDefaults } from "discourse/components/topic-list-item"; -const ACTION_ID = "assign"; - -function modifySelectKit(api) { - api - .modifySelectKit("topic-footer-mobile-dropdown") - .modifyContent((context, existingContent) => { - if (context.get("currentUser.can_assign")) { - const hasAssignement = context.get("topic.assigned_to_user"); - const button = { - id: ACTION_ID, - icon: hasAssignement ? "user-times" : "user-plus", - name: I18n.t( - `discourse_assign.${hasAssignement ? "unassign" : "assign"}.title` - ) - }; - existingContent.push(button); - } - return existingContent; - }) - .onSelect((context, value) => { - if (!context.get("currentUser.can_assign") || value !== ACTION_ID) { +function registerTopicFooterButtons(api) { + api.registerTopicFooterButton({ + id: "assign", + icon() { + const hasAssignement = this.get("topic.assigned_to_user"); + return hasAssignement ? "user-times" : "user-plus"; + }, + priority: 250, + title() { + const hasAssignement = this.get("topic.assigned_to_user"); + return `discourse_assign.${hasAssignement ? "unassign" : "assign"}.help`; + }, + label() { + const hasAssignement = this.get("topic.assigned_to_user"); + return `discourse_assign.${hasAssignement ? "unassign" : "assign"}.title`; + }, + action() { + if (!this.get("currentUser.staff")) { return; } - const topic = context.topic; + const topic = this.topic; const assignedUser = topic.get("assigned_to_user.username"); if (assignedUser) { ajax("/assign/unassign", { type: "PUT", data: { topic_id: topic.id } - }) - .then(result => { - if (result.success && result.success === "OK") { - topic.set("assigned_to_user", null); - } - }) - .finally(() => context._compute()); + }).then(result => { + if (result.success && result.success === "OK") { + topic.set("assigned_to_user", null); + } + }); } else { showModal("assign-user", { model: { @@ -56,12 +51,25 @@ function modifySelectKit(api) { username: topic.get("assigned_to_user.username"), onClose: assignedToUser => { topic.set("assigned_to_user", assignedToUser); - context._compute(); } } }); } - }); + }, + dropdown() { + return this.site.mobileView && !this.get("topic.isPrivateMessage"); + }, + classNames: ["assign"], + dependentKeys: [ + "topic.isPrivateMessage", + "topic.assigned_to_user", + "currentUser.staff", + "topic.assigned_to_user.username" + ], + displayed() { + return this.get("currentUser.staff"); + } + }); } function initialize(api) { @@ -252,6 +260,6 @@ export default { } withPluginApi("0.8.11", api => initialize(api, container)); - withPluginApi("0.8.13", api => modifySelectKit(api, container)); + withPluginApi("0.8.28", api => registerTopicFooterButtons(api, container)); } }; diff --git a/test/javascripts/acceptance/assign-disabled-test.js.es6 b/test/javascripts/acceptance/assign-disabled-test.js.es6 index 1c929de..3b60ed7 100644 --- a/test/javascripts/acceptance/assign-disabled-test.js.es6 +++ b/test/javascripts/acceptance/assign-disabled-test.js.es6 @@ -1,13 +1,13 @@ import selectKit from "helpers/select-kit-helper"; import { acceptance, updateCurrentUser } from "helpers/qunit-helpers"; -import { clearCallbacks } from "select-kit/mixins/plugin-api"; +import { clearTopicFooterButtons } from "discourse/lib/register-topic-footer-button"; acceptance("Assign disabled mobile", { loggedIn: true, mobileView: true, settings: { assign_enabled: false }, beforeEach() { - clearCallbacks(); + clearTopicFooterButtons(); } }); diff --git a/test/javascripts/acceptance/assign-enabled-test.js.es6 b/test/javascripts/acceptance/assign-enabled-test.js.es6 index c514544..e3bedc4 100644 --- a/test/javascripts/acceptance/assign-enabled-test.js.es6 +++ b/test/javascripts/acceptance/assign-enabled-test.js.es6 @@ -1,13 +1,13 @@ import selectKit from "helpers/select-kit-helper"; import { acceptance, updateCurrentUser } from "helpers/qunit-helpers"; -import { clearCallbacks } from "select-kit/mixins/plugin-api"; +import { clearTopicFooterButtons } from "discourse/lib/register-topic-footer-button"; acceptance("Assign mobile", { loggedIn: true, mobileView: true, settings: { assign_enabled: true }, beforeEach() { - clearCallbacks(); + clearTopicFooterButtons(); } });