Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FC-0036] feat: Sort taxonomies #949

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion src/content-tags-drawer/ContentTagsDrawer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,36 @@ const ContentTagsDrawer = ({ id, onClose }) => {
}, []);

const taxonomies = useMemo(() => {
const sortTaxonomies = (taxonomiesList) => {
const taxonomiesWithData = taxonomiesList.filter(
(t) => t.contentTags.length !== 0,
);

// 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) => {
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],
);

// 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,
);

return [...sortedTaxonomiesWithData, ...emptyTaxonomies];
};

if (taxonomyListData && contentTaxonomyTagsData) {
// Initialize list of content tags in taxonomies to populate
const taxonomiesList = taxonomyListData.results.map((taxonomy) => ({
Expand All @@ -126,7 +156,7 @@ const ContentTagsDrawer = ({ id, onClose }) => {
}
});

return taxonomiesList;
return sortTaxonomies(taxonomiesList);
}
return [];
}, [taxonomyListData, contentTaxonomyTagsData]);
Expand Down
95 changes: 89 additions & 6 deletions src/content-tags-drawer/ContentTagsDrawer.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,78 @@ describe('<ContentTagsDrawer />', () => {
},
],
},
{
name: 'Taxonomy 2',
taxonomyId: 124,
canTagObject: true,
tags: [
{
value: 'Tag 1',
lineage: ['Tag 1'],
canDeleteObjecttag: true,
},
],
},
{
name: 'Taxonomy 3',
taxonomyId: 125,
canTagObject: true,
tags: [
{
value: 'Tag 1.1.1',
lineage: ['Tag 1', 'Tag 1.1', 'Tag 1.1.1'],
canDeleteObjecttag: true,
},
],
},
{
name: '(B) Taxonomy 4',
taxonomyId: 126,
canTagObject: true,
tags: [],
},
{
name: '(A) Taxonomy 5',
taxonomyId: 127,
canTagObject: true,
tags: [],
},
],
},
});
getTaxonomyListData.mockResolvedValue({
results: [{
id: 123,
name: 'Taxonomy 1',
description: 'This is a description 1',
canTagObject: true,
}],
results: [
{
id: 123,
name: 'Taxonomy 1',
description: 'This is a description 1',
canTagObject: true,
},
{
id: 124,
name: 'Taxonomy 2',
description: 'This is a description 2',
canTagObject: true,
},
{
id: 125,
name: 'Taxonomy 3',
description: 'This is a description 3',
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,
},
],
});

useTaxonomyTagsData.mockReturnValue({
Expand Down Expand Up @@ -413,4 +475,25 @@ describe('<ContentTagsDrawer />', () => {

postMessageSpy.mockRestore();
});

it('should taxonomies must be ordered', async () => {
setupMockDataForStagedTagsTesting();
render(<RootWrapper />);
expect(await screen.findByText('Taxonomy 1')).toBeInTheDocument();

// First, taxonomies with content sorted by count implicit
// Later, empty taxonomies sorted by name
const expectedOrder = [
'Taxonomy 3', // 3 tags
'Taxonomy 1', // 2 tags
'Taxonomy 2', // 1 tag
'(A) Taxonomy 5',
'(B) Taxonomy 4',
];

const taxonomies = screen.getAllByText(/.*Taxonomy.*/);
for (let i = 0; i !== taxonomies.length; i++) {
expect(taxonomies[i].textContent).toBe(expectedOrder[i]);
}
});
});
Loading