DEV: uses new topic-button API (#43)

This commit is contained in:
Joffrey JAFFEUX 2019-07-19 10:20:55 +02:00 committed by GitHub
parent 75c6cecfdb
commit 02c6fa6d24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 36 deletions

View File

@ -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. // TODO: This has to be removed when 2.3 becomes the new stable version.
import { ListItemDefaults } from "discourse/components/topic-list-item"; import { ListItemDefaults } from "discourse/components/topic-list-item";
const ACTION_ID = "assign"; function registerTopicFooterButtons(api) {
api.registerTopicFooterButton({
function modifySelectKit(api) { id: "assign",
api icon() {
.modifySelectKit("topic-footer-mobile-dropdown") const hasAssignement = this.get("topic.assigned_to_user");
.modifyContent((context, existingContent) => { return hasAssignement ? "user-times" : "user-plus";
if (context.get("currentUser.can_assign")) { },
const hasAssignement = context.get("topic.assigned_to_user"); priority: 250,
const button = { title() {
id: ACTION_ID, const hasAssignement = this.get("topic.assigned_to_user");
icon: hasAssignement ? "user-times" : "user-plus", return `discourse_assign.${hasAssignement ? "unassign" : "assign"}.help`;
name: I18n.t( },
`discourse_assign.${hasAssignement ? "unassign" : "assign"}.title` label() {
) const hasAssignement = this.get("topic.assigned_to_user");
}; return `discourse_assign.${hasAssignement ? "unassign" : "assign"}.title`;
existingContent.push(button); },
} action() {
return existingContent; if (!this.get("currentUser.staff")) {
})
.onSelect((context, value) => {
if (!context.get("currentUser.can_assign") || value !== ACTION_ID) {
return; return;
} }
const topic = context.topic; const topic = this.topic;
const assignedUser = topic.get("assigned_to_user.username"); const assignedUser = topic.get("assigned_to_user.username");
if (assignedUser) { if (assignedUser) {
ajax("/assign/unassign", { ajax("/assign/unassign", {
type: "PUT", type: "PUT",
data: { topic_id: topic.id } data: { topic_id: topic.id }
}) }).then(result => {
.then(result => { if (result.success && result.success === "OK") {
if (result.success && result.success === "OK") { topic.set("assigned_to_user", null);
topic.set("assigned_to_user", null); }
} });
})
.finally(() => context._compute());
} else { } else {
showModal("assign-user", { showModal("assign-user", {
model: { model: {
@ -56,12 +51,25 @@ function modifySelectKit(api) {
username: topic.get("assigned_to_user.username"), username: topic.get("assigned_to_user.username"),
onClose: assignedToUser => { onClose: assignedToUser => {
topic.set("assigned_to_user", 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) { function initialize(api) {
@ -252,6 +260,6 @@ export default {
} }
withPluginApi("0.8.11", api => initialize(api, container)); withPluginApi("0.8.11", api => initialize(api, container));
withPluginApi("0.8.13", api => modifySelectKit(api, container)); withPluginApi("0.8.28", api => registerTopicFooterButtons(api, container));
} }
}; };

View File

@ -1,13 +1,13 @@
import selectKit from "helpers/select-kit-helper"; import selectKit from "helpers/select-kit-helper";
import { acceptance, updateCurrentUser } from "helpers/qunit-helpers"; 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", { acceptance("Assign disabled mobile", {
loggedIn: true, loggedIn: true,
mobileView: true, mobileView: true,
settings: { assign_enabled: false }, settings: { assign_enabled: false },
beforeEach() { beforeEach() {
clearCallbacks(); clearTopicFooterButtons();
} }
}); });

View File

@ -1,13 +1,13 @@
import selectKit from "helpers/select-kit-helper"; import selectKit from "helpers/select-kit-helper";
import { acceptance, updateCurrentUser } from "helpers/qunit-helpers"; 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", { acceptance("Assign mobile", {
loggedIn: true, loggedIn: true,
mobileView: true, mobileView: true,
settings: { assign_enabled: true }, settings: { assign_enabled: true },
beforeEach() { beforeEach() {
clearCallbacks(); clearTopicFooterButtons();
} }
}); });