FIX: subcategory filter limits results (#15655)
When the subcategory dropdown is searched, it should only display categories belonging to the same parent category.
This commit is contained in:
parent
e814f77eaa
commit
51a31f7835
|
@ -542,6 +542,7 @@ Category.reopenClass({
|
|||
|
||||
search(term, opts) {
|
||||
let limit = 5;
|
||||
let parentCategoryId;
|
||||
|
||||
if (opts) {
|
||||
if (opts.limit === 0) {
|
||||
|
@ -549,6 +550,9 @@ Category.reopenClass({
|
|||
} else if (opts.limit) {
|
||||
limit = opts.limit;
|
||||
}
|
||||
if (opts.parentCategoryId) {
|
||||
parentCategoryId = opts.parentCategoryId;
|
||||
}
|
||||
}
|
||||
|
||||
const emptyTerm = term === "";
|
||||
|
@ -569,13 +573,21 @@ Category.reopenClass({
|
|||
return data.length === limit;
|
||||
};
|
||||
|
||||
const validCategoryParent = (category) => {
|
||||
return (
|
||||
!parentCategoryId ||
|
||||
category.get("parent_category_id") === parentCategoryId
|
||||
);
|
||||
};
|
||||
|
||||
for (i = 0; i < length && !done(); i++) {
|
||||
const category = categories[i];
|
||||
if (
|
||||
(emptyTerm && !category.get("parent_category_id")) ||
|
||||
(!emptyTerm &&
|
||||
(category.get("name").toLowerCase().indexOf(term) === 0 ||
|
||||
category.get("slug").toLowerCase().indexOf(slugTerm) === 0))
|
||||
((emptyTerm && !category.get("parent_category_id")) ||
|
||||
(!emptyTerm &&
|
||||
(category.get("name").toLowerCase().indexOf(term) === 0 ||
|
||||
category.get("slug").toLowerCase().indexOf(slugTerm) === 0))) &&
|
||||
validCategoryParent(category)
|
||||
) {
|
||||
data.push(category);
|
||||
}
|
||||
|
@ -586,9 +598,10 @@ Category.reopenClass({
|
|||
const category = categories[i];
|
||||
|
||||
if (
|
||||
!emptyTerm &&
|
||||
(category.get("name").toLowerCase().indexOf(term) > 0 ||
|
||||
category.get("slug").toLowerCase().indexOf(slugTerm) > 0)
|
||||
((!emptyTerm &&
|
||||
category.get("name").toLowerCase().indexOf(term) > 0) ||
|
||||
category.get("slug").toLowerCase().indexOf(slugTerm) > 0) &&
|
||||
validCategoryParent(category)
|
||||
) {
|
||||
if (data.indexOf(category) === -1) {
|
||||
data.push(category);
|
||||
|
|
|
@ -276,9 +276,17 @@ module("Unit | Model | category", function () {
|
|||
id: 2,
|
||||
name: "middle term",
|
||||
slug: "another-different-slug",
|
||||
}),
|
||||
subcategory = store.createRecord("category", {
|
||||
id: 3,
|
||||
name: "middle term",
|
||||
slug: "another-different-slug2",
|
||||
parent_category_id: 2,
|
||||
});
|
||||
|
||||
sinon.stub(Category, "listByActivity").returns([category1, category2]);
|
||||
sinon
|
||||
.stub(Category, "listByActivity")
|
||||
.returns([category1, category2, subcategory]);
|
||||
|
||||
assert.deepEqual(
|
||||
Category.search("term", { limit: 0 }),
|
||||
|
@ -292,24 +300,30 @@ module("Unit | Model | category", function () {
|
|||
);
|
||||
assert.deepEqual(
|
||||
Category.search("term"),
|
||||
[category1, category2],
|
||||
[category1, category2, subcategory],
|
||||
"orders by activity"
|
||||
);
|
||||
|
||||
category2.set("name", "TeRm start");
|
||||
assert.deepEqual(
|
||||
Category.search("tErM"),
|
||||
[category2, category1],
|
||||
[category2, category1, subcategory],
|
||||
"ignores case of category name and search term"
|
||||
);
|
||||
|
||||
category2.set("name", "term start");
|
||||
assert.deepEqual(
|
||||
Category.search("term"),
|
||||
[category2, category1],
|
||||
[category2, category1, subcategory],
|
||||
"orders matching begin with and then contains"
|
||||
);
|
||||
|
||||
assert.deepEqual(
|
||||
Category.search("term", { parentCategoryId: 2 }),
|
||||
[subcategory],
|
||||
"search only subcategories belonging to specific parent category"
|
||||
);
|
||||
|
||||
sinon.restore();
|
||||
|
||||
const child_category1 = store.createRecord("category", {
|
||||
|
|
|
@ -131,7 +131,10 @@ export default ComboBoxComponent.extend({
|
|||
|
||||
search(filter) {
|
||||
if (filter) {
|
||||
let results = Category.search(filter);
|
||||
let opts = {
|
||||
parentCategoryId: this.options.parentCategory?.id,
|
||||
};
|
||||
let results = Category.search(filter, opts);
|
||||
results = this._filterUncategorized(results).sort((a, b) => {
|
||||
if (a.parent_category_id && !b.parent_category_id) {
|
||||
return 1;
|
||||
|
|
Loading…
Reference in New Issue