Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: add "New category" action to sidebar for admins (#37082)

Régis Hanolbc53187cfb53
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 1d651fce0ac..d037eafdd33 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -5468,6 +5468,9 @@ en:
click_to_get_started: "Click here to get started."
header_link_text: "Categories"
header_action_title: "Edit your sidebar categories"
+ header_action_new: "New category"
+ header_action_edit_sidebar: "Edit sidebar categories"
+ header_action_edit_header_dropdown: "Edit nav categories"
configure_defaults: "Configure defaults"
community:
edit_section:
diff --git a/frontend/discourse/app/components/sidebar/user/categories-section.gjs b/frontend/discourse/app/components/sidebar/user/categories-section.gjs
index 28a566c408b..d756fc1917a 100644
--- a/frontend/discourse/app/components/sidebar/user/categories-section.gjs
+++ b/frontend/discourse/app/components/sidebar/user/categories-section.gjs
@@ -1,6 +1,5 @@
import { cached } from "@glimmer/tracking";
-import { array, hash } from "@ember/helper";
-import { action } from "@ember/object";
+import { hash } from "@ember/helper";
import { service } from "@ember/service";
import { debounce } from "discourse/lib/decorators";
import { hasDefaultSidebarCategories } from "discourse/lib/sidebar/helpers";
@@ -19,6 +18,8 @@ export default class SidebarUserCategoriesSection extends CommonCategoriesSectio
@service appEvents;
@service currentUser;
@service modal;
+ @service navigationMenu;
+ @service router;
constructor() {
super(...arguments);
@@ -66,22 +67,39 @@ export default class SidebarUserCategoriesSection extends CommonCategoriesSectio
return hasDefaultSidebarCategories(this.siteSettings);
}
- @action
- showModal() {
- this.modal.show(EditNavigationMenuCategoriesModal);
+ @cached
+ get headerActions() {
+ const actions = [];
+
+ if (this.currentUser.admin) {
+ actions.push({
+ id: "new-category",
+ action: () => this.router.transitionTo("newCategory"),
+ title: i18n("sidebar.sections.categories.header_action_new"),
+ });
+ }
+
+ actions.push({
+ id: "edit-categories",
+ action: () => this.modal.show(EditNavigationMenuCategoriesModal),
+ title: i18n(
+ `sidebar.sections.categories.header_action_edit_${this.navigationMenu.displayMode}`
+ ),
+ });
+
+ return actions;
+ }
+
+ get headerActionsIcon() {
+ return this.headerActions.length > 1 ? "ellipsis-vertical" : "pencil";
}
<template>
<Section
@sectionName="categories"
@headerLinkText={{i18n "sidebar.sections.categories.header_link_text"}}
- @headerActions={{array
- (hash
- action=this.showModal
- title=(i18n "sidebar.sections.categories.header_action_title")
- )
- }}
- @headerActionsIcon="pencil"
+ @headerActions={{this.headerActions}}
+ @headerActionsIcon={{this.headerActionsIcon}}
@collapsable={{@collapsable}}
>
diff --git a/frontend/discourse/app/lib/sidebar/user/community-section/admin-section.js b/frontend/discourse/app/lib/sidebar/user/community-section/admin-section.js
index 74c9eba413e..68eb169d833 100644
--- a/frontend/discourse/app/lib/sidebar/user/community-section/admin-section.js
+++ b/frontend/discourse/app/lib/sidebar/user/community-section/admin-section.js
@@ -15,11 +15,7 @@ export default class extends CommonCommunitySection {
get moreSectionButtonText() {
return i18n(
- `sidebar.sections.community.edit_section.${
- this.navigationMenu.isDesktopDropdownMode
- ? "header_dropdown"
- : "sidebar"
- }`
+ `sidebar.sections.community.edit_section.${this.navigationMenu.displayMode}`
);
}
diff --git a/frontend/discourse/app/services/navigation-menu.js b/frontend/discourse/app/services/navigation-menu.js
index 78a43497289..f7d8b6e95ea 100644
--- a/frontend/discourse/app/services/navigation-menu.js
+++ b/frontend/discourse/app/services/navigation-menu.js
@@ -13,4 +13,8 @@ export default class NavigationMenu extends Service {
get isDesktopDropdownMode() {
return this.site.desktopView && this.isHeaderDropdownMode;
}
+
+ get displayMode() {
+ return this.isDesktopDropdownMode ? "header_dropdown" : "sidebar";
+ }
}
diff --git a/frontend/discourse/tests/acceptance/sidebar-user-categories-section-test.js b/frontend/discourse/tests/acceptance/sidebar-user-categories-section-test.js
index 00cd706daf9..4986eb1e5a5 100644
--- a/frontend/discourse/tests/acceptance/sidebar-user-categories-section-test.js
+++ b/frontend/discourse/tests/acceptance/sidebar-user-categories-section-test.js
@@ -12,6 +12,7 @@ import {
queryAll,
updateCurrentUser,
} from "discourse/tests/helpers/qunit-helpers";
+import selectKit from "discourse/tests/helpers/select-kit-helper";
import { i18n } from "discourse-i18n";
acceptance(
@@ -637,7 +638,7 @@ acceptance("Sidebar - Logged on user - Categories Section", function (needs) {
);
});
- test("category section link have the right title", async function (assert) {
+ test("category section link has the right title", async function (assert) {
const categories = Site.current().categories;
// Category with link HTML tag in description
@@ -977,7 +978,7 @@ acceptance("Sidebar - Logged on user - Categories Section", function (needs) {
);
});
- test("clean up topic tracking state state changed callbacks when Sidebar is collapsed", async function (assert) {
+ test("cleans up topic tracking state changed callbacks when sidebar is collapsed", async function (assert) {
setupUserSidebarCategories();
await visit("/");
@@ -1021,6 +1022,39 @@ acceptance("Sidebar - Logged on user - Categories Section", function (needs) {
"it links to the admin site settings page correctly"
);
});
+
+ test("categories section header shows dropdown with new and edit actions for admin user", async function (assert) {
+ updateCurrentUser({ admin: true });
+
+ await visit("/");
+
+ const headerDropdown = selectKit(
+ ".sidebar-section[data-section-name='categories'] .sidebar-section-header-dropdown"
+ );
+
+ assert.true(headerDropdown.exists());
+
+ await headerDropdown.expand();
+
+ assert.true(headerDropdown.rowByValue("new-category").exists());
+ assert.true(headerDropdown.rowByValue("edit-categories").exists());
+
+ await headerDropdown.selectRowByValue("new-category");
+
+ assert.strictEqual(currentURL(), "/new-category");
+ });
+
+ test("categories section header shows single edit button for non-admin user", async function (assert) {
+ await visit("/");
+
+ const categoriesSection =
+ ".sidebar-section[data-section-name='categories']";
+
+ assert.dom(`${categoriesSection} .sidebar-section-header-button`).exists();
+ assert
+ .dom(`${categoriesSection} .sidebar-section-header-dropdown`)
+ .doesNotExist();
+ });
});
acceptance(
@@ -1328,3 +1362,29 @@ acceptance(
});
}
);
+
+acceptance(
+ "Sidebar - Admin - Categories Section - Header Dropdown Mode",
+ function (needs) {
+ needs.user({ admin: true });
+ needs.settings({ navigation_menu: "header dropdown" });
+
+ test("edit action shows 'Edit nav categories' text", async function (assert) {
+ await visit("/");
+ await click("#toggle-hamburger-menu");
+
+ const headerDropdown = selectKit(
+ ".sidebar-section[data-section-name='categories'] .sidebar-section-header-dropdown"
+ );
+
+ await headerDropdown.expand();
+
+ assert.true(
+ headerDropdown
+ .rowByValue("edit-categories")
+ .label()
+ .includes("Edit nav categories")
+ );
+ });
+ }
+);
diff --git a/spec/system/page_objects/components/sidebar_header_dropdown.rb b/spec/system/page_objects/components/sidebar_header_dropdown.rb
index cd5287074cf..c8ff910652e 100644
--- a/spec/system/page_objects/components/sidebar_header_dropdown.rb
+++ b/spec/system/page_objects/components/sidebar_header_dropdown.rb
@@ -24,10 +24,7 @@ module PageObjects
end
def click_categories_header_button
- page.click_button(
- I18n.t("js.sidebar.sections.categories.header_action_title"),
- class: "sidebar-section-header-button",
- )
+ page.find("[data-section-name='categories'] .sidebar-section-header-button").click
end
def click_topics_link