mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: on initial load category subcategories were not mapped correctly
This commit is contained in:
@@ -116,14 +116,30 @@ Site.reopenClass(Singleton, {
|
|||||||
const store = result.store;
|
const store = result.store;
|
||||||
|
|
||||||
if (result.categories) {
|
if (result.categories) {
|
||||||
|
let subcatMap = {};
|
||||||
|
|
||||||
result.categoriesById = {};
|
result.categoriesById = {};
|
||||||
result.categories = _.map(
|
result.categories = _.map(result.categories, c => {
|
||||||
result.categories,
|
if (c.parent_category_id) {
|
||||||
c => (result.categoriesById[c.id] = store.createRecord("category", c))
|
subcatMap[c.parent_category_id] =
|
||||||
);
|
subcatMap[c.parent_category_id] || [];
|
||||||
|
subcatMap[c.parent_category_id].push(c.id);
|
||||||
|
}
|
||||||
|
return (result.categoriesById[c.id] = store.createRecord(
|
||||||
|
"category",
|
||||||
|
c
|
||||||
|
));
|
||||||
|
});
|
||||||
|
|
||||||
// Associate the categories with their parents
|
// Associate the categories with their parents
|
||||||
result.categories.forEach(c => {
|
result.categories.forEach(c => {
|
||||||
|
let subcategoryIds = subcatMap[c.get("id")];
|
||||||
|
if (subcategoryIds) {
|
||||||
|
c.set(
|
||||||
|
"subcategories",
|
||||||
|
subcategoryIds.map(id => result.categoriesById[id])
|
||||||
|
);
|
||||||
|
}
|
||||||
if (c.get("parent_category_id")) {
|
if (c.get("parent_category_id")) {
|
||||||
c.set(
|
c.set(
|
||||||
"parentCategory",
|
"parentCategory",
|
||||||
|
|||||||
@@ -44,6 +44,8 @@ QUnit.test("create categories", assert => {
|
|||||||
assert.present(parent, "it loaded the parent category");
|
assert.present(parent, "it loaded the parent category");
|
||||||
assert.blank(parent.get("parentCategory"), "it has no parent category");
|
assert.blank(parent.get("parentCategory"), "it has no parent category");
|
||||||
|
|
||||||
|
assert.equal(parent.get("subcategories").length, 1);
|
||||||
|
|
||||||
const subcategory = categories.findBy("id", 3456);
|
const subcategory = categories.findBy("id", 3456);
|
||||||
assert.present(subcategory, "it loaded the subcategory");
|
assert.present(subcategory, "it loaded the subcategory");
|
||||||
assert.equal(
|
assert.equal(
|
||||||
|
|||||||
Reference in New Issue
Block a user