From 6b66a06174163c38504c556173a741b010f5a641 Mon Sep 17 00:00:00 2001 From: Kelv Date: Thu, 26 Oct 2023 18:07:30 +0800 Subject: [PATCH] FIX: restore button to add user notes to post admin menu (#85) * FIX: use api.addPostAdminMenuButton for modifying post admin menu instead of widget We use app events here to force refresh of specific post, as previously the behaviour of the widget would trigger the re-render at the same time as the primary actions. This could also be handled in the modal instead of via the action passed by the button. --------- Co-authored-by: Joffrey JAFFEUX --- .discourse-compatibility | 1 + .../initializers/enable-user-notes.js | 53 +++++++++++++------ 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/.discourse-compatibility b/.discourse-compatibility index 0c72b2c..618cc30 100644 --- a/.discourse-compatibility +++ b/.discourse-compatibility @@ -1,2 +1,3 @@ +< 3.2.0.beta2-dev: 134fcba29baf2ca92ec555dab1707c096712af14 3.1.999: 20f8f68d127a923942cca11a586caf4ea14019eb 2.9.0.beta12: 7137499c69065928c0fdd8795a3fb8d175e1ff3d diff --git a/assets/javascripts/discourse/initializers/enable-user-notes.js b/assets/javascripts/discourse/initializers/enable-user-notes.js index 703f89a..4d6a21d 100644 --- a/assets/javascripts/discourse/initializers/enable-user-notes.js +++ b/assets/javascripts/discourse/initializers/enable-user-notes.js @@ -10,6 +10,8 @@ export default { initialize(container) { const siteSettings = container.lookup("site-settings:main"); const currentUser = container.lookup("current-user:main"); + const appEvents = container.lookup("service:app-events"); + if ( !siteSettings.user_notes_enabled || !currentUser || @@ -68,8 +70,13 @@ export default { return; } - const cfs = dec.attrs.userCustomFields || {}; - if (cfs.user_notes_count > 0) { + const post = dec.getModel(); + if (!post) { + return; + } + + const ucf = post.user_custom_fields || {}; + if (ucf.user_notes_count > 0) { return dec.attach("user-notes-icon"); } }); @@ -79,23 +86,39 @@ export default { return; } - const cfs = dec.attrs.userCustomFields || {}; - if (cfs.user_notes_count > 0) { + const post = dec.getModel(); + if (!post) { + return; + } + + const ucf = post.user_custom_fields || {}; + if (ucf.user_notes_count > 0) { return dec.attach("user-notes-icon"); } }); + api.addPostAdminMenuButton((attrs) => { + return { + icon: "pencil-alt", + label: "user_notes.attach", + action: (post) => { + showUserNotes( + store, + attrs.user_id, + (count) => { + const ucf = post.user_custom_fields || {}; + ucf.user_notes_count = count; + post.set("user_custom_fields", ucf); - api.decorateWidget("post-admin-menu:after", (dec) => { - return dec.h( - "ul", - dec.attach("post-admin-menu-button", { - icon: "pencil-alt", - label: "user_notes.attach", - action: "showUserNotes", - secondaryAction: "closeAdminMenu", - className: "add-user-note", - }) - ); + appEvents.trigger("post-stream:refresh", { + id: post.id, + }); + }, + { postId: attrs.id } + ); + }, + secondaryAction: "closeAdminMenu", + className: "add-user-note", + }; }); api.attachWidgetAction("post", "showUserNotes", widgetshowUserNotes);