diff --git a/client/src/pages/filters.jsx b/client/src/pages/filters.jsx index 3cc86ad9..fd27e8a9 100644 --- a/client/src/pages/filters.jsx +++ b/client/src/pages/filters.jsx @@ -44,12 +44,31 @@ export default function Filters({ sendQuery }) { const affiliations = searchParams.getAll('affiliations'); const queries = affiliations.map((affiliation) => getRorNames(affiliation)); const rorNames = await Promise.all(queries); - let allTags = [ - ...affiliations.map((affiliation) => ({ label: affiliation, source: 'user' })), - ...rorNames.flat().map((name) => ({ label: name, source: 'ror' })), - ]; + const allTags = []; + const knownTags = {}; + affiliations.forEach((affiliation) => { + allTags.push({ label: affiliation, source: 'user' }); + knownTags[affiliation.toLowerCase()] = 1; + }); + rorNames.flat().forEach((rorElt) => { + if (knownTags[rorElt.rorId.toLowerCase()] === undefined) { + allTags.push({ label: rorElt.rorId, source: 'rorId' }); + knownTags[rorElt.rorId.toLowerCase()] = 1; + } + rorElt.names.forEach((rorName) => { + if (knownTags[rorName.toLowerCase()] === undefined) { + allTags.push({ label: rorName, source: 'rorName' }); + knownTags[rorName.toLowerCase()] = 1; + } + }); + }); + // let allTags = [ + // ...affiliations.map((affiliation) => ({ label: affiliation, source: 'user' })), + // ...rorNames.flat().map((name) => ({ label: name, source: 'ror' })), + // ]; // Remove duplicates - allTags = [...new Map(allTags.reverse().map((v) => [v.label.toLowerCase(), v])).values()].reverse(); + // allTags = [...new Map(allTags.reverse().map((v) => [v.label.toLowerCase(), v])).values()].reverse(); + console.log('allTags', allTags); setTags(allTags); } }; @@ -78,18 +97,7 @@ export default function Filters({ sendQuery }) { return ( - - - - + setSearchParams({ ...currentSearchParams, endYear: e.target.value })} /> - + + + + ); } diff --git a/client/src/utils/ror.jsx b/client/src/utils/ror.jsx index 45a336ec..101e6658 100644 --- a/client/src/utils/ror.jsx +++ b/client/src/utils/ror.jsx @@ -6,20 +6,28 @@ const isRor = (affiliation) => rorRegex.test(affiliation); const getRorNames = async (affiliation) => { const affiliationId = affiliation.replace('https://ror.org/', '').replace('ror.org/', ''); if (!isRor(affiliationId)) return []; - let response = await fetch(`https://api.ror.org/organizations/${affiliation}`); + let response = await fetch(`https://api.ror.org/organizations/${affiliationId}`); response = await response.json(); - const queries = response.relationships.filter((relationship) => relationship.type === 'Child').map((relationship) => getRorNames(relationship.id)); - let childrenNames = []; - if (queries.length > 0) { - childrenNames = await Promise.all(queries); + const children = response.relationships.filter((relationship) => relationship.type === 'Child'); + let childrenRes = []; + const childrenQueries = []; + children.forEach((child) => { + childrenQueries.push(getRorNames(child.id)); + }); + if (childrenQueries.length > 0) { + childrenRes = await Promise.all(childrenQueries); } - return [ - response.name, - ...response.acronyms, - ...response.aliases, - ...response.labels.map((item) => item.label), - ...childrenNames.flat(), - ]; + const topLevel = [{ + rorId: affiliationId, + children, + names: [ + response.name, + ...response.acronyms, + ...response.aliases, + ...response.labels.map((item) => item.label), + ] }]; + const ans = topLevel.concat(childrenRes.flat()); + return ans; }; export {