FIX: Show only top categories in first category-drop (#24575)

This commit is contained in:
Bianca Nenciu
2023-11-29 09:41:25 +02:00
committed by GitHub
parent b09422428d
commit dbb8b66a37
3 changed files with 23 additions and 3 deletions

View File

@ -135,12 +135,15 @@ export default ComboBoxComponent.extend({
async search(filter) { async search(filter) {
const opts = { const opts = {
parentCategoryId: this.options.parentCategory?.id, parentCategoryId: this.options.parentCategory?.id || -1,
includeUncategorized: this.siteSettings.allow_uncategorized_topics, includeUncategorized: this.siteSettings.allow_uncategorized_topics,
}; };
if (this.siteSettings.lazy_load_categories) { if (this.siteSettings.lazy_load_categories) {
const results = await Category.asyncSearch(filter, { ...opts, limit: 5 }); const results = await Category.asyncSearch(filter, {
...opts,
limit: 15,
});
return this.shortcuts.concat( return this.shortcuts.concat(
results.sort((a, b) => { results.sort((a, b) => {
if (a.parent_category_id && !b.parent_category_id) { if (a.parent_category_id && !b.parent_category_id) {

View File

@ -353,7 +353,13 @@ class CategoriesController < ApplicationController
) if term.present? ) if term.present?
categories = categories =
categories.where(parent_category_id: parent_category_id) if parent_category_id.present? (
if parent_category_id != -1
categories.where(parent_category_id: parent_category_id)
else
categories.where(parent_category_id: nil)
end
) if parent_category_id.present?
categories = categories =
categories.where.not(id: SiteSetting.uncategorized_category_id) if !include_uncategorized categories.where.not(id: SiteSetting.uncategorized_category_id) if !include_uncategorized

View File

@ -1096,6 +1096,17 @@ RSpec.describe CategoriesController do
"Foobar", "Foobar",
) )
end end
it "can return only top-level categories" do
get "/categories/search.json", params: { parent_category_id: -1 }
expect(response.parsed_body["categories"].size).to eq(3)
expect(response.parsed_body["categories"].map { |c| c["name"] }).to contain_exactly(
"Uncategorized",
"Foo",
"Notfoo",
)
end
end end
context "with include_uncategorized" do context "with include_uncategorized" do