From c1f078cacfdce5fe0913857a997cb3a2a5be654c Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Thu, 17 Oct 2024 19:47:53 +0300 Subject: [PATCH] FIX: Make subcategories page more like categories (#29240) The subcategories page was not paginated and it was using the subcategory style from the category settings. The same page style should be used for categories and subcategories page. --- .../discovery/categories-display.gjs | 6 +++++- app/models/category_list.rb | 19 ++++++------------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/app/assets/javascripts/discourse/app/components/discovery/categories-display.gjs b/app/assets/javascripts/discourse/app/components/discovery/categories-display.gjs index 2674b52ce95..e1e0bd8d6ce 100644 --- a/app/assets/javascripts/discourse/app/components/discovery/categories-display.gjs +++ b/app/assets/javascripts/discourse/app/components/discovery/categories-display.gjs @@ -36,6 +36,7 @@ const globalComponents = { }; export default class CategoriesDisplay extends Component { + @service router; @service siteSettings; @service site; @@ -69,7 +70,10 @@ export default class CategoriesDisplay extends Component { } get categoriesComponent() { - if (this.args.parentCategory) { + if ( + this.args.parentCategory && + this.router.currentRouteName === "discovery.category" + ) { return this.#componentForSubcategories; } else { return this.#globalComponent; diff --git a/app/models/category_list.rb b/app/models/category_list.rb index 02f97560781..53224a8e475 100644 --- a/app/models/category_list.rb +++ b/app/models/category_list.rb @@ -133,22 +133,15 @@ class CategoryList def find_categories query = Category.includes(CategoryList.included_associations).secured(@guardian) - - query = - query.where( - "categories.parent_category_id = ?", - @options[:parent_category_id].to_i, - ) if @options[:parent_category_id].present? - query = self.class.order_categories(query) + if @options[:parent_category_id].present? || @guardian.can_lazy_load_categories? + query = query.where(parent_category_id: @options[:parent_category_id]) + end + page = [1, @options[:page].to_i].max - if @guardian.can_lazy_load_categories? && @options[:parent_category_id].blank? - query = - query - .where(parent_category_id: nil) - .limit(CATEGORIES_PER_PAGE) - .offset((page - 1) * CATEGORIES_PER_PAGE) + if @guardian.can_lazy_load_categories? + query = query.limit(CATEGORIES_PER_PAGE).offset((page - 1) * CATEGORIES_PER_PAGE) elsif page > 1 # Pagination is supported only when lazy load is enabled. If it is not, # everything is returned on page 1.