Skip to content

Commit

Permalink
get ror data in a structured format
Browse files Browse the repository at this point in the history
  • Loading branch information
ericjeangirard committed Feb 21, 2024
1 parent 03026ef commit 93cf73e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 31 deletions.
57 changes: 38 additions & 19 deletions client/src/pages/filters.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
Expand Down Expand Up @@ -78,18 +97,7 @@ export default function Filters({ sendQuery }) {

return (
<Row gutters alignItems="top">
<Col n="5">
<TagInput
hint="Press ENTER to search for several terms / expressions. If several, an OR operator is used."
label="Affiliation name"
message={message}
messageType={messageType}
onTagsChange={onTagsChange}
tags={tags}
onInputHandler={setOnInputAffiliationsHandler}
/>
</Col>
<Col n="1">
<Col n="2">
<Select
hint="&nbsp;"
label="Start year"
Expand All @@ -98,7 +106,7 @@ export default function Filters({ sendQuery }) {
onChange={(e) => setSearchParams({ ...currentSearchParams, startYear: e.target.value })}
/>
</Col>
<Col n="1">
<Col n="2">
<Select
hint="&nbsp;"
label="End year"
Expand All @@ -107,7 +115,7 @@ export default function Filters({ sendQuery }) {
onChange={(e) => setSearchParams({ ...currentSearchParams, endYear: e.target.value })}
/>
</Col>
<Col n="2">
<Col n="4">
<CheckboxGroup
hint="&nbsp;"
legend="&nbsp;"
Expand All @@ -127,6 +135,17 @@ export default function Filters({ sendQuery }) {
Search works
</Button>
</Col>
<Col n="12">
<TagInput
hint="Press ENTER to search for several terms / expressions. If several, an OR operator is used."
label="Affiliation name"
message={message}
messageType={messageType}
onTagsChange={onTagsChange}
tags={tags}
onInputHandler={setOnInputAffiliationsHandler}
/>
</Col>
</Row>
);
}
Expand Down
32 changes: 20 additions & 12 deletions client/src/utils/ror.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 93cf73e

Please sign in to comment.