FIX: Update category breadcrumbs more reliably (#26608)

The breadcrumbs were updated everytime there were changes to the
categories which was not efficient and caused unnecessary rerendering
of the CategoryDrop elements when "lazy load categories" is enabled.

This commit also ensures that all category fields are serialized for
ancestors too for the categories#search endpoint.
This commit is contained in:
Bianca Nenciu
2024-04-12 11:56:32 +03:00
committed by GitHub
parent 4d2dcdad9b
commit 38e1706b61
2 changed files with 23 additions and 21 deletions

View File

@ -10,30 +10,31 @@ export default Component.extend({
editingCategory: false, editingCategory: false,
editingCategoryTab: null, editingCategoryTab: null,
@discourseComputed("category.ancestors", "categories", "noSubcategories") @discourseComputed("category", "categories", "noSubcategories")
categoryBreadcrumbs(categoryAncestors, filteredCategories, noSubcategories) { categoryBreadcrumbs(category, filteredCategories, noSubcategories) {
categoryAncestors = categoryAncestors || []; const ancestors = category?.ancestors || [];
const parentCategories = [undefined, ...categoryAncestors]; const parentCategories = [undefined, ...ancestors];
const categories = [...categoryAncestors, undefined]; const categories = [...ancestors, undefined];
const zipped = parentCategories.map((x, i) => [x, categories[i]]);
return zipped.map((record) => { return parentCategories
const [parentCategory, category] = record; .map((x, i) => [x, categories[i]])
.map((record) => {
const [parentCategory, subCategory] = record;
const options = filteredCategories.filter( const options = filteredCategories.filter(
(c) => (c) =>
c.get("parentCategory.id") === (parentCategory && parentCategory.id) c.get("parentCategory.id") === (parentCategory && parentCategory.id)
); );
return { return {
category, category: subCategory,
parentCategory, parentCategory,
options, options,
isSubcategory: !!parentCategory, isSubcategory: !!parentCategory,
noSubcategories: !category && noSubcategories, noSubcategories: !subCategory && noSubcategories,
hasOptions: !parentCategory || parentCategory.has_children, hasOptions: !parentCategory || parentCategory.has_children,
}; };
}); });
}, },
@discourseComputed("siteSettings.tagging_enabled", "editingCategory") @discourseComputed("siteSettings.tagging_enabled", "editingCategory")

View File

@ -436,6 +436,7 @@ class CategoriesController < ApplicationController
if include_ancestors if include_ancestors
ancestors = Category.secured(guardian).ancestors_of(categories.map(&:id)) ancestors = Category.secured(guardian).ancestors_of(categories.map(&:id))
Category.preload_user_fields!(guardian, ancestors)
response[:ancestors] = serialize_data(ancestors, SiteCategorySerializer, scope: guardian) response[:ancestors] = serialize_data(ancestors, SiteCategorySerializer, scope: guardian)
end end