Skip to content

Commit

Permalink
feat(openalex): Support "Get ROR children feature"
Browse files Browse the repository at this point in the history
  • Loading branch information
annelhote committed Nov 22, 2024
1 parent 3e93178 commit 6b6af3e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 32 deletions.
8 changes: 4 additions & 4 deletions client/src/components/tag-input/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function TagInput({
placeholder,
seeMoreAction,
seeMoreAfter,
setGetRorChildren,
switchGetRorChildren,
tags,
}) {
const [excludedValues, setExcludedValues] = useState(deletedTags);
Expand Down Expand Up @@ -151,7 +151,7 @@ export default function TagInput({
className="fr-mr-1w"
// eslint-disable-next-line react/no-array-index-key
key={`tags-ror-${index}`}
onClick={() => setGetRorChildren(!getRorChildren)}
onClick={() => switchGetRorChildren()}
size="sm"
variant="text"
>
Expand Down Expand Up @@ -209,7 +209,7 @@ TagInput.propTypes = {
placeholder: PropTypes.string,
seeMoreAction: PropTypes.func,
seeMoreAfter: PropTypes.number,
setGetRorChildren: PropTypes.func,
switchGetRorChildren: PropTypes.func,
tags: PropTypes.arrayOf(PropTypes.object),
};

Expand All @@ -225,6 +225,6 @@ TagInput.defaultProps = {
placeholder: '',
seeMoreAfter: SEE_MORE_AFTER,
seeMoreAction: undefined,
setGetRorChildren: () => { },
switchGetRorChildren: () => { },
tags: [],
};
1 change: 0 additions & 1 deletion client/src/components/tiles/openalex.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import { FormattedMessage } from 'react-intl';

export default function OpenalexTile() {
Expand Down
6 changes: 3 additions & 3 deletions client/src/pages/openalex-ror/components/works-list.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { Button, Link, Text } from '@dataesr/dsfr-plus';
import PropTypes from 'prop-types';
import { useState } from 'react';

const WORKS_LENGTH = 5;
const SEE_MORE_AFTER = 5;

export default function WorksList({ works }) {
const [showMore, setShowMore] = useState(false);

const displayedWorks = showMore ? works : works.slice(0, WORKS_LENGTH);
const displayedWorks = showMore ? works : works.slice(0, SEE_MORE_AFTER);

return (
<Text className="fr-my-1w fr-pl-1w" size="sm" style={{ borderLeft: '5px solid #aaa' }}>
Expand All @@ -23,7 +23,7 @@ export default function WorksList({ works }) {
{
works.length > 5 && (
<Button onClick={() => setShowMore(!showMore)} variant="text">
{showMore ? 'show less works' : `show more works (${works.length - WORKS_LENGTH})`}
{showMore ? 'show less works' : `show more works (${works.length - SEE_MORE_AFTER})`}
</Button>
)
}
Expand Down
31 changes: 20 additions & 11 deletions client/src/pages/openalex-ror/results/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,26 +142,35 @@ export default function Affiliations() {
const getData = async () => {
const queryParams = {
endYear: searchParams.get('endYear') ?? '2023',
getRorChildren: searchParams.get('getRorChildren') ?? '0',
startYear: searchParams.get('startYear') ?? '2023',
};
queryParams.deletedAffiliations = [];
queryParams.rorExclusions = [];
queryParams.affiliations = await Promise.all(
searchParams.getAll('affiliations').map(async (affiliation) => {
const label = normalize(affiliation);
let children = [];
const children = [];
// Compute rorNames
if (isRor(label)) {
const rorNames = await getRorData(label);
children = rorNames
.map((item) => item.names)
.flat()
.map((name) => ({
isDisabled: name.length < VITE_APP_TAG_LIMIT,
isRor: false,
label: name,
source: 'ror',
}));
const rors = await getRorData(label, queryParams.getRorChildren === '1');
rors
.forEach((item) => {
children.push({
isDisabled: false,
label: item.rorId,
source: 'ror',
type: 'rorId',
});
item.names.forEach((name) => {
children.push({
isDisabled: name.length < VITE_APP_TAG_LIMIT,
label: name,
source: 'ror',
type: 'affiliationString',
});
});
});
}
return {
children,
Expand Down
28 changes: 15 additions & 13 deletions client/src/pages/openalex-ror/search.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ const years = [...Array(new Date().getFullYear() - START_YEAR + 1).keys()]
.map((year) => ({ label: year, value: year }));

export default function OpenalexRorSearch() {
const { pathname, search } = useLocation();
const { search } = useLocation();
const navigate = useNavigate();
const [searchParams, setSearchParams] = useSearchParams();

const [currentSearchParams, setCurrentSearchParams] = useState({});
const [deletedAffiliations, setDeletedAffiliations] = useState([]);
const [isLoading, setIsLoading] = useState(false);
const [isOpen, setIsOpen] = useState(false);
const [getRorChildren, setGetRorChildren] = useState(false);
const [message, setMessage] = useState('');
const [messageType, setMessageType] = useState('');
const [onInputAffiliationsHandler, setOnInputAffiliationsHandler] = useState(false);
Expand All @@ -42,12 +41,13 @@ export default function OpenalexRorSearch() {
const [tags, setTags] = useState([]);

useEffect(() => {
if (searchParams.size < 2) {
if (searchParams.size < 4) {
// Set default params values
setSearchParams({
affiliations: searchParams.getAll('affiliations') ?? [],
deletedAffiliations: searchParams.getAll('deletedAffiliations') ?? [],
endYear: searchParams.get('endYear') ?? '2023',
getRorChildren: searchParams.get('getRorChildren') ?? '0',
startYear: searchParams.get('startYear') ?? '2023',
view: searchParams.get('view') ?? 'list',
});
Expand All @@ -60,6 +60,7 @@ export default function OpenalexRorSearch() {
affiliations,
deletedAffiliations: deletedAffiliations1,
endYear: searchParams.get('endYear') ?? '2023',
getRorChildren: searchParams.get('getRorChildren') ?? '0',
startYear: searchParams.get('startYear') ?? '2023',
view: searchParams.get('view') ?? 'list',
});
Expand All @@ -82,7 +83,6 @@ export default function OpenalexRorSearch() {
}
}, [
deletedAffiliations,
getRorChildren,
searchedAffiliations,
searchParams,
setSearchParams,
Expand All @@ -94,7 +94,7 @@ export default function OpenalexRorSearch() {
const filteredSearchedAffiliation = searchedAffiliations.filter(
(affiliation) => !deletedAffiliations.includes(affiliation),
);
const queries = filteredSearchedAffiliation.map((affiliation) => getRorData(affiliation, getRorChildren));
const queries = filteredSearchedAffiliation.map((affiliation) => getRorData(affiliation, currentSearchParams.getRorChildren === '1'));
let rorNames = await Promise.all(queries);
rorNames = rorNames.filter(
(rorName) => !deletedAffiliations.includes(rorName),
Expand All @@ -107,7 +107,7 @@ export default function OpenalexRorSearch() {
const label = cleanRor(affiliation);
if (isRor(label)) {
allTags.push({
isDisabled: label.length < VITE_APP_TAG_LIMIT,
isDisabled: false,
label,
source: 'user',
type: 'rorId',
Expand All @@ -127,7 +127,7 @@ export default function OpenalexRorSearch() {
if (knownTags[rorElt.rorId.toLowerCase()] === undefined) {
if (!deletedAffiliations.includes(rorElt.rorId)) {
allTags.push({
isDisabled: rorElt.rorId.length < VITE_APP_TAG_LIMIT,
isDisabled: false,
label: rorElt.rorId,
source: 'ror',
type: 'rorId',
Expand Down Expand Up @@ -159,7 +159,7 @@ export default function OpenalexRorSearch() {
};

getData();
}, [deletedAffiliations, getRorChildren, searchedAffiliations]);
}, [currentSearchParams.getRorChildren, deletedAffiliations, searchedAffiliations]);

const onTagsChange = async (_affiliations, _deletedAffiliations) => {
const affiliations = _affiliations
Expand Down Expand Up @@ -196,9 +196,11 @@ export default function OpenalexRorSearch() {
}
setMessageType('');
setMessage('');
navigate(`/${pathname.split('/')[1]}/results${search}`);
navigate(`/openalex-ror/results${search}`);
};

const switchGetRorChildren = () => setSearchParams({ ...currentSearchParams, getRorChildren: currentSearchParams.getRorChildren === '1' ? '0' : '1' });

const NB_TAGS_STICKY = 2;
const tagsDisplayed = tags.slice(0, NB_TAGS_STICKY);

Expand Down Expand Up @@ -259,7 +261,7 @@ export default function OpenalexRorSearch() {
</Col>
<Col xs="12">
<TagInput
getRorChildren={getRorChildren}
getRorChildren={currentSearchParams.getRorChildren === '1'}
hint="Press ENTER to search for several terms / expressions. If several, an OR operator is used."
isLoading={isLoading}
isRequired
Expand All @@ -269,7 +271,7 @@ export default function OpenalexRorSearch() {
onInputHandler={setOnInputAffiliationsHandler}
onTagsChange={onTagsChange}
seeMoreAfter={0}
setGetRorChildren={setGetRorChildren}
switchGetRorChildren={switchGetRorChildren}
tags={tags}
/>
</Col>
Expand Down Expand Up @@ -299,7 +301,7 @@ export default function OpenalexRorSearch() {
<Row className="fr-pt-2w fr-pr-2w fr-pb-0 fr-pl-2w">
<Col xs="8">
<TagInput
getRorChildren={getRorChildren}
getRorChildren={currentSearchParams.getRorChildren === '1'}
hint="Press ENTER to search for several terms / expressions. If several, an OR operator is used."
isLoading={isLoading}
isRequired
Expand All @@ -312,7 +314,7 @@ export default function OpenalexRorSearch() {
setIsOpen(true);
e.preventDefault();
}}
setGetRorChildren={setGetRorChildren}
switchGetRorChildren={switchGetRorChildren}
tags={tags}
/>
</Col>
Expand Down

0 comments on commit 6b6af3e

Please sign in to comment.