FIX: on initial load category subcategories were not mapped correctly

This commit is contained in:
Sam 2018-07-12 17:59:38 +10:00
parent 79ba418edd
commit 1f0bbca87e
2 changed files with 22 additions and 4 deletions

View File

@ -116,14 +116,30 @@ Site.reopenClass(Singleton, {
const store = result.store;
if (result.categories) {
let subcatMap = {};
result.categoriesById = {};
result.categories = _.map(
result.categories,
c => (result.categoriesById[c.id] = store.createRecord("category", c))
);
result.categories = _.map(result.categories, c => {
if (c.parent_category_id) {
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
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")) {
c.set(
"parentCategory",

View File

@ -44,6 +44,8 @@ QUnit.test("create categories", assert => {
assert.present(parent, "it loaded the 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);
assert.present(subcategory, "it loaded the subcategory");
assert.equal(