Skip to content

Commit

Permalink
refactor: Sort Taxonomies
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisChV committed Apr 17, 2024
1 parent 56bc069 commit 9ded66e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
24 changes: 10 additions & 14 deletions src/content-tags-drawer/ContentTagsDrawer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,31 +114,27 @@ const ContentTagsDrawer = ({ id, onClose }) => {
(t) => t.contentTags.length !== 0,
);

// Count implicit tags per taxonomy
// Count implicit tags per taxonomy.
// TODO This count is also calculated individually
// in ContentTagsCollapsible. It should only be calculated once.
const tagsCountBytaxonomy = {};
taxonomiesWithData.forEach((tax) => {
const countedTags = [];
let tagsCounter = 0;
tax.contentTags.forEach((tag) => {
tag.lineage.forEach((value) => {
if (!countedTags.includes(value)) {
countedTags.push(value);
tagsCounter += 1;
}
});
});
tagsCountBytaxonomy[tax.id] = tagsCounter;
tagsCountBytaxonomy[tax.id] = new Set(
tax.contentTags.flatMap(item => item.lineage),
).size;
});

// Sort taxonomies with data by implicit count
const sortedTaxonomiesWithData = taxonomiesWithData.sort(
(a, b) => tagsCountBytaxonomy[b.id] - tagsCountBytaxonomy[a.id],
);

// Sort empty taxonomies by name
// Empty taxonomies sorted by name.
// Since the query returns sorted by name,
// it is not necessary to do another sorting here.
const emptyTaxonomies = taxonomiesList.filter(
(t) => t.contentTags.length === 0,
).sort((a, b) => a.name.localeCompare(b.name));
);

return [...sortedTaxonomiesWithData, ...emptyTaxonomies];
};
Expand Down
12 changes: 6 additions & 6 deletions src/content-tags-drawer/ContentTagsDrawer.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,18 @@ describe('<ContentTagsDrawer />', () => {
description: 'This is a description 3',
canTagObject: true,
},
{
id: 126,
name: '(B) Taxonomy 4',
description: 'This is a description 4',
canTagObject: true,
},
{
id: 127,
name: '(A) Taxonomy 5',
description: 'This is a description 5',
canTagObject: true,
},
{
id: 126,
name: '(B) Taxonomy 4',
description: 'This is a description 4',
canTagObject: true,
},
],
});

Expand Down

0 comments on commit 9ded66e

Please sign in to comment.