A platform for community discussion. Free, open, simple.
FIX: Show more button now loads additional root-level subcategories (#35101)
Martin Brennan1c8762963895
diff --git a/app/assets/javascripts/discourse/app/components/sidebar/edit-navigation-menu/categories-modal.gjs b/app/assets/javascripts/discourse/app/components/sidebar/edit-navigation-menu/categories-modal.gjsindex 34764ff122b..36ebacd0620 100644--- a/app/assets/javascripts/discourse/app/components/sidebar/edit-navigation-menu/categories-modal.gjs+++ b/app/assets/javascripts/discourse/app/components/sidebar/edit-navigation-menu/categories-modal.gjs@@ -95,11 +95,19 @@ function findPartialCategories(categories) {}for (const [id, count] of subcategoryCounts) {- if (count === 5 && categoriesById.has(id)) {- partialCategoryInfos.set(id, {- level: categoriesById.get(id).level + 1,- offset: subcategoryCountsRecursive.get(id),- });+ if (count === 5) {+ if (id === undefined) {+ // Root level categories (parent_category_id is undefined)+ partialCategoryInfos.set(id, {+ level: 0,+ offset: subcategoryCountsRecursive.get(id),+ });+ } else if (categoriesById.has(id)) {+ partialCategoryInfos.set(id, {+ level: categoriesById.get(id).level + 1,+ offset: subcategoryCountsRecursive.get(id),+ });+ }}}@@ -232,12 +240,22 @@ export default class SidebarEditNavigationMenuCategoriesModal extends Component// which is the default page size and indicates there might be more to load.// If we received fewer than 5, we've reached the end of the subcategories.if (subcategories.length === 5) {- const parentCategory = this.fetchedCategories.find((c) => c.id === id);- if (parentCategory) {+ if (id === undefined) {+ // Root level categoriesthis.partialCategoryInfos.set(id, {- level: parentCategory.level + 1,+ level: 0,offset: offset + subcategories.length,});+ } else {+ const parentCategory = this.fetchedCategories.find(+ (c) => c.id === id+ );+ if (parentCategory) {+ this.partialCategoryInfos.set(id, {+ level: parentCategory.level + 1,+ offset: offset + subcategories.length,+ });+ }}}@@ -300,7 +318,12 @@ export default class SidebarEditNavigationMenuCategoriesModal extends Componentthis.loadedPage = requestedPage;} else if (this.subcategoryLoadList.length !== 0) {const { id, offset } = this.subcategoryLoadList.shift();- const opts = { parentCategoryId: id, offset, ...this.searchOpts() };+ const opts = { offset, ...this.searchOpts() };++ // Only add parentCategoryId if it's not undefined (for root categories)+ if (id !== undefined) {+ opts.parentCategoryId = id;+ }let subcategories = await Category.asyncHierarchicalSearch(requestedFilter,diff --git a/spec/system/editing_sidebar_categories_navigation_spec.rb b/spec/system/editing_sidebar_categories_navigation_spec.rbindex 41a7d0827bb..54380b1a78b 100644--- a/spec/system/editing_sidebar_categories_navigation_spec.rb+++ b/spec/system/editing_sidebar_categories_navigation_spec.rb@@ -277,6 +277,98 @@ RSpec.describe "Editing sidebar categories navigation", type: :system doendend+ context "when loading more subcategories with 3-level hierarchy" do+ before_all { SiteSetting.max_category_nesting = 3 }++ before do+ SiteSetting.max_category_nesting = 3+ Jobs.with_immediate_jobs do+ SearchIndexer.with_indexing do+ grandparent_category.index_search+ parent1.index_search+ parent2.index_search+ parent3.index_search+ parent4.index_search+ parent5.index_search+ parent6.index_search+ child1_1.index_search+ child1_2.index_search+ child1_3.index_search+ child1_4.index_search+ child1_5.index_search+ child1_6.index_search+ end+ end+ end++ fab!(:grandparent_category) { Fabricate(:category, name: "grandparent category") }++ fab!(:parent1) do+ Fabricate(:category, parent_category_id: grandparent_category.id, name: "parent 1")+ end++ fab!(:parent2) do+ Fabricate(:category, parent_category_id: grandparent_category.id, name: "parent 2")+ end++ fab!(:parent3) do+ Fabricate(:category, parent_category_id: grandparent_category.id, name: "parent 3")+ end++ fab!(:parent4) do+ Fabricate(:category, parent_category_id: grandparent_category.id, name: "parent 4")+ end++ fab!(:parent5) do+ Fabricate(:category, parent_category_id: grandparent_category.id, name: "parent 5")+ end++ fab!(:parent6) do+ Fabricate(:category, parent_category_id: grandparent_category.id, name: "parent 6")+ end++ fab!(:child1_1) { Fabricate(:category, parent_category_id: parent1.id, name: "child 1-1") }+ fab!(:child1_2) { Fabricate(:category, parent_category_id: parent1.id, name: "child 1-2") }+ fab!(:child1_3) { Fabricate(:category, parent_category_id: parent1.id, name: "child 1-3") }+ fab!(:child1_4) { Fabricate(:category, parent_category_id: parent1.id, name: "child 1-4") }+ fab!(:child1_5) { Fabricate(:category, parent_category_id: parent1.id, name: "child 1-5") }+ fab!(:child1_6) { Fabricate(:category, parent_category_id: parent1.id, name: "child 1-6") }++ around(:each) do |example|+ stub_const(Object, :CategoriesController, CategoriesController.clone) do+ stub_const(CategoriesController, :MAX_CATEGORIES_LIMIT, 5) { example.run }+ end+ end++ it "shows 'Show more' button at grandparent level and loads additional parent categories" do+ visit "/latest"++ modal = sidebar.click_edit_categories_button++ # Should show grandparent category and first 5 parent categories+ expect(modal).to have_category_row(grandparent_category)+ modal.scroll_to_category(grandparent_category)+ expect(modal).to have_category_row(parent1)+ expect(modal).to have_category_row(parent2)+ expect(modal).to have_category_row(parent3)+ modal.scroll_to_category(parent3)+ expect(modal).to have_category_row(parent4)+ expect(modal).to have_category_row(parent5)+ expect(modal).to have_no_category_row(parent6)++ # Should have a "Show more" button for grandparent's children+ modal.scroll_to_category(parent5)+ expect(modal).to have_show_more_button(level: 1)++ # Click the "Show more" button to load more parent categories under grandparent+ wait_for_timeout(300)+ modal.click_show_more_button(level: 1)++ expect(modal).to have_category_row(parent6)+ expect(modal).to have_no_show_more_button(level: 1)+ end+ end+context "when there are more categories than the page limit" doaround(:each) do |example|search_calls = 0diff --git a/spec/system/page_objects/modals/sidebar_edit_categories.rb b/spec/system/page_objects/modals/sidebar_edit_categories.rbindex 7a08b04a544..33015736dcd 100644--- a/spec/system/page_objects/modals/sidebar_edit_categories.rb+++ b/spec/system/page_objects/modals/sidebar_edit_categories.rb@@ -55,12 +55,20 @@ module PageObjectshas_css?(".sidebar-categories-form__category-row[data-category-id='#{category.id}']")end- def has_no_show_more_button?- has_no_css?(".sidebar-categories-form__show-more-btn")+ def has_no_category_row?(category)+ has_no_css?(".sidebar-categories-form__category-row[data-category-id='#{category.id}']")end- def has_show_more_button?- has_css?(".sidebar-categories-form__show-more-btn")+ def has_no_show_more_button?(level: 1)+ has_no_css?(+ ".sidebar-categories-form__category-row[data-category-level=\"#{level}\"] .sidebar-categories-form__show-more-btn",+ )+ end++ def has_show_more_button?(level: 1)+ has_css?(+ ".sidebar-categories-form__category-row[data-category-level=\"#{level}\"] .sidebar-categories-form__show-more-btn",+ )enddef scroll_to_category(category)@@ -68,8 +76,10 @@ module PageObjectsselfend- def click_show_more_button- find(".sidebar-categories-form__show-more-btn").click+ def click_show_more_button(level: 1)+ find(+ ".sidebar-categories-form__category-row[data-category-level=\"#{level}\"] .sidebar-categories-form__show-more-btn",+ ).clickselfendend