mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-03-06 04:21:17 +01:00
fix: parent cid
This commit is contained in:
@@ -330,7 +330,7 @@ Categories.flattenCategories = function (allCategories, categoryData) {
|
|||||||
* @param parentCid {number} start from 0 to build full tree
|
* @param parentCid {number} start from 0 to build full tree
|
||||||
*/
|
*/
|
||||||
Categories.getTree = function (categories, parentCid) {
|
Categories.getTree = function (categories, parentCid) {
|
||||||
parentCid = parentCid || 0;
|
parentCid = String(parentCid || 0);
|
||||||
const cids = categories.map(category => category && category.cid);
|
const cids = categories.map(category => category && category.cid);
|
||||||
const cidToCategory = {};
|
const cidToCategory = {};
|
||||||
const parents = {};
|
const parents = {};
|
||||||
@@ -351,15 +351,15 @@ Categories.getTree = function (categories, parentCid) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!category.hasOwnProperty('parentCid') || category.parentCid === null) {
|
if (!category.hasOwnProperty('parentCid') || category.parentCid === null) {
|
||||||
category.parentCid = 0;
|
category.parentCid = '0';
|
||||||
}
|
}
|
||||||
if (category.parentCid === parentCid) {
|
if (String(category.parentCid) === parentCid) {
|
||||||
tree.push(category);
|
tree.push(category);
|
||||||
category.parent = parents[parentCid];
|
category.parent = parents[parentCid];
|
||||||
} else {
|
} else {
|
||||||
const parent = cidToCategory[category.parentCid];
|
const parent = cidToCategory[String(category.parentCid)];
|
||||||
if (parent && parent.cid !== category.cid) {
|
if (parent && parent.cid !== category.cid) {
|
||||||
category.parent = parents[category.parentCid];
|
category.parent = parents[String(category.parentCid)];
|
||||||
parent.children = parent.children || [];
|
parent.children = parent.children || [];
|
||||||
parent.children.push(category);
|
parent.children.push(category);
|
||||||
}
|
}
|
||||||
@@ -413,10 +413,10 @@ Categories.buildForSelectCategories = function (categories, fields, parentCid) {
|
|||||||
category.children.forEach(child => recursive(child, categoriesData, ` ${level}`, depth + 1));
|
category.children.forEach(child => recursive(child, categoriesData, ` ${level}`, depth + 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
parentCid = parentCid || 0;
|
parentCid = String(parentCid || 0);
|
||||||
const categoriesData = [];
|
const categoriesData = [];
|
||||||
|
|
||||||
const rootCategories = categories.filter(category => category && category.parentCid === parentCid);
|
const rootCategories = categories.filter(category => category && String(category.parentCid) === parentCid);
|
||||||
|
|
||||||
rootCategories.sort((a, b) => {
|
rootCategories.sort((a, b) => {
|
||||||
if (a.order !== b.order) {
|
if (a.order !== b.order) {
|
||||||
|
|||||||
@@ -130,9 +130,10 @@ describe('Search', () => {
|
|||||||
|
|
||||||
it('should search for categories', async () => {
|
it('should search for categories', async () => {
|
||||||
const socketCategories = require('../src/socket.io/categories');
|
const socketCategories = require('../src/socket.io/categories');
|
||||||
let data = await socketCategories.categorySearch({ uid: phoebeUid }, { query: 'baz', parentCid: 0 });
|
let data = await socketCategories.categorySearch({ uid: phoebeUid }, { search: 'baz', parentCid: 0 });
|
||||||
|
assert.strictEqual(data.length, 1);
|
||||||
assert.strictEqual(data[0].name, 'baz category');
|
assert.strictEqual(data[0].name, 'baz category');
|
||||||
data = await socketCategories.categorySearch({ uid: phoebeUid }, { query: '', parentCid: 0 });
|
data = await socketCategories.categorySearch({ uid: phoebeUid }, { search: '', parentCid: 0 });
|
||||||
assert.strictEqual(data.length, 5);
|
assert.strictEqual(data.length, 5);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user