From 8dd0f8a712086158b7c3ad8440744f804e80327c Mon Sep 17 00:00:00 2001 From: Krzysztof Kotlarek Date: Wed, 20 Jul 2022 13:44:13 +1000 Subject: [PATCH] FIX: pass sidebar custom link willDestroy (#17565) Custom sidebar link willDestroy function has to be passed to SidebarLink component to call when component is removed. Very similar to https://github.com/discourse/discourse/pull/17551 --- .../app/components/sidebar/section-link.js | 6 +++++ .../templates/components/sidebar/sections.hbs | 1 + .../acceptance/sidebar-plugin-api-test.js | 23 +++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/app/assets/javascripts/discourse/app/components/sidebar/section-link.js b/app/assets/javascripts/discourse/app/components/sidebar/section-link.js index f34d267aa68..1a1b51c6319 100644 --- a/app/assets/javascripts/discourse/app/components/sidebar/section-link.js +++ b/app/assets/javascripts/discourse/app/components/sidebar/section-link.js @@ -2,6 +2,12 @@ import GlimmerComponent from "@glimmer/component"; import { htmlSafe } from "@ember/template"; export default class SectionLink extends GlimmerComponent { + willDestroy() { + if (this.args.willDestroy) { + this.args.willDestroy(); + } + } + get prefixCSS() { const color = this.args.prefixColor; if (!color || !color.match(/^\w{6}$/)) { diff --git a/app/assets/javascripts/discourse/app/templates/components/sidebar/sections.hbs b/app/assets/javascripts/discourse/app/templates/components/sidebar/sections.hbs index 18d58292f0b..076e09e6c60 100644 --- a/app/assets/javascripts/discourse/app/templates/components/sidebar/sections.hbs +++ b/app/assets/javascripts/discourse/app/templates/components/sidebar/sections.hbs @@ -39,6 +39,7 @@ @hoverAction={{link.hoverAction}} @hoverTitle={{link.hoverTitle}} @currentWhen={{link.currentWhen}} + @willDestroy={{link.willDestroy}} @content={{link.text}} /> {{/each}} diff --git a/app/assets/javascripts/discourse/tests/acceptance/sidebar-plugin-api-test.js b/app/assets/javascripts/discourse/tests/acceptance/sidebar-plugin-api-test.js index 3d42921850a..aab97c2907c 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/sidebar-plugin-api-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/sidebar-plugin-api-test.js @@ -9,13 +9,17 @@ import { } from "discourse/tests/helpers/qunit-helpers"; import { withPluginApi } from "discourse/lib/plugin-api"; import { resetSidebarSection } from "discourse/lib/sidebar/custom-sections"; +import { bind } from "discourse-common/utils/decorators"; acceptance("Sidebar - section API", function (needs) { needs.user({ experimental_sidebar_enabled: true }); needs.hooks.afterEach(() => { resetSidebarSection(); + linkDestroy = undefined; + sectionDestroy = undefined; }); + let linkDestroy, sectionDestroy; test("Multiple header actions and links", async function (assert) { withPluginApi("1.3.0", (api) => { @@ -54,6 +58,10 @@ acceptance("Sidebar - section API", function (needs) { }, ]; } + @bind + willDestroy() { + sectionDestroy = "section test"; + } get links() { return [ new (class extends BaseCustomSidebarSectionLink { @@ -93,6 +101,10 @@ acceptance("Sidebar - section API", function (needs) { get suffixCSSClass() { return "unread"; } + @bind + willDestroy() { + linkDestroy = "link test"; + } })(), new (class extends BaseCustomSidebarSectionLink { get name() { @@ -275,6 +287,17 @@ acceptance("Sidebar - section API", function (needs) { "hover button title attribute", "displays hover button with correct title" ); + await click(".header-sidebar-toggle button"); + assert.strictEqual( + linkDestroy, + "link test", + "calls link willDestroy function" + ); + assert.strictEqual( + sectionDestroy, + "section test", + "calls section willDestroy function" + ); }); test("Single header action and no links", async function (assert) {