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

Account for topics with no subtopics #1053

Merged
merged 2 commits into from
Sep 24, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,12 @@ export class CantabularMetadataController extends Component {
try {
const extractTopicItems = ({ items }) => items;
let rootTopicsArr = await topics.getRootTopics().then(extractTopicItems);
const getSubtopics = ({ id }) => topics.getSubtopics(id).then(extractTopicItems);
const getSubtopics = ({ id, next: { subtopics_ids } }) => {
if (subtopics_ids.length > 0) {
return topics.getSubtopics(id).then(extractTopicItems);
}
return [];
};
let allSubtopics = await Promise.all(rootTopicsArr.map(getSubtopics)).then(subtopics => subtopics.flat());
const extractTopicOptions = ({ id, next: { title } }) => ({ value: id, label: title });
const rootTopics = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,7 @@ describe("Calling getTopics", () => {
href: "http://localhost:25300/topics/testID1/subtopics",
},
},
subtopics_ids: ["testSubtopicID1", "testSubtopicID2"],
},
current: {
id: "testID1",
Expand All @@ -983,6 +984,7 @@ describe("Calling getTopics", () => {
href: "http://localhost:25300/topics/testID1/subtopics",
},
},
subtopics_ids: ["testSubtopicID1", "testSubtopicID2"],
},
},
],
Expand Down
15 changes: 10 additions & 5 deletions src/legacy/js/functions/_tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function formatResponse(response){
response.items.forEach((item, i) => {
result.push ({
"id" : item.current.id,
"subtopics_ids": item.current.subtopics_ids,
"title" : item.current.title
})
})
Expand Down Expand Up @@ -87,11 +88,15 @@ async function getAllTopics(){
// Get subtopics and format into something more readable.
let subtopics = []
for (const i in topics) {
const subtopicsAPIResult = await getSubTopics(topics[i].id)
subtopics.push(formatResponse(subtopicsAPIResult))
subtopics[i].forEach(function(subtopic){
subtopic.title = topics[i].title + " - " + subtopic.title
})
if(topics[i].subtopics_ids.length > 0){
const subtopicsAPIResult = await getSubTopics(topics[i].id)
const subtopicsFormattedResult = formatResponse(subtopicsAPIResult)
const subtopicsWithParentTopicTitle = subtopicsFormattedResult.map(function(subtopic){
subtopic.title = topics[i].title + " - " + subtopic.title
return subtopic
})
subtopics.push(subtopicsWithParentTopicTitle)
}
}
subtopics = subtopics.flat()

Expand Down
Loading