FIX: Load complete category objects

Not all categories are preloaded when lazy_load_categories is enabled.
In this case, the category objects that are loaded through /docs.json
must be complete.
This commit is contained in:
Bianca Nenciu 2024-03-27 17:40:59 +02:00
parent 22107a114d
commit f4959b228b
No known key found for this signature in database
GPG Key ID: 07E83B117A6B844D
2 changed files with 39 additions and 4 deletions

View File

@ -1,5 +1,6 @@
import EmberObject from "@ember/object";
import { ajax } from "discourse/lib/ajax";
import Site from "discourse/models/site";
import Topic from "discourse/models/topic";
import User from "discourse/models/user";
import { getDocs } from "../../lib/get-docs";
@ -38,6 +39,13 @@ Docs.reopenClass({
}
return ajax(`/${docsPath}.json?${filters.join("&")}`).then((data) => {
const site = Site.current();
if (site.lazy_load_categories) {
data.categories?.forEach((category) => site.updateCategory(category));
data.topics.topic_list.categories?.forEach((category) =>
site.updateCategory(category)
);
}
data.topics.topic_list.topics = data.topics.topic_list.topics.map(
(topic) => Topic.create(topic)
);
@ -51,6 +59,12 @@ Docs.reopenClass({
loadMore(loadMoreUrl) {
return ajax(loadMoreUrl).then((data) => {
const site = Site.current();
if (site.lazy_load_categories) {
data.topics.topic_list.categories?.forEach((category) =>
site.updateCategory(category)
);
}
data.topics.topic_list.topics = data.topics.topic_list.topics.map(
(topic) => Topic.create(topic)
);

View File

@ -210,12 +210,33 @@ module Docs
tags_object.sort_by { |tag| [tag[:active] ? 0 : 1, -tag[:count]] }
end
def create_categories_object(categories)
def create_categories_object(category_counts)
categories =
Category
.where(id: category_counts.keys)
.includes(
:uploaded_logo,
:uploaded_logo_dark,
:uploaded_background,
:uploaded_background_dark,
)
.joins("LEFT JOIN topics t on t.id = categories.topic_id")
.select("categories.*, t.slug topic_slug")
.index_by(&:id)
categories_object = []
categories.each do |category|
active = @filters[:category].include?(category[0].to_s) if @filters[:category]
categories_object << { id: category[0], count: category[1], active: active || false }
category_counts.each do |id, count|
active = @filters[:category] && @filters[:category].include?(id.to_s)
categories_object << if @guardian.can_lazy_load_categories?
BasicCategorySerializer
.new(categories[id], scope: @guardian, root: false)
.as_json
.merge(id:, count:, active:)
else
category_object = { id:, count:, active: }
end
end
categories_object.sort_by { |category| [category[:active] ? 0 : 1, -category[:count]] }