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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 3 deletions

View File

@ -135,12 +135,15 @@ export default ComboBoxComponent.extend({
async search(filter) {
const opts = {
parentCategoryId: this.options.parentCategory?.id,
parentCategoryId: this.options.parentCategory?.id || -1,
includeUncategorized: this.siteSettings.allow_uncategorized_topics,
};
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(
results.sort((a, b) => {
if (a.parent_category_id && !b.parent_category_id) {

View File

@ -353,7 +353,13 @@ class CategoriesController < ApplicationController
) if term.present?
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.where.not(id: SiteSetting.uncategorized_category_id) if !include_uncategorized

View File

@ -1096,6 +1096,17 @@ RSpec.describe CategoriesController do
"Foobar",
)
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
context "with include_uncategorized" do