From 6b6af3e0fbe69e2d51d3a77dff9f94167a1883a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anne=20L=27H=C3=B4te?= Date: Fri, 22 Nov 2024 12:17:20 +0100 Subject: [PATCH] feat(openalex): Support "Get ROR children feature" --- client/src/components/tag-input/index.jsx | 8 ++--- client/src/components/tiles/openalex.jsx | 1 - .../openalex-ror/components/works-list.jsx | 6 ++-- .../src/pages/openalex-ror/results/index.jsx | 31 ++++++++++++------- client/src/pages/openalex-ror/search.jsx | 28 +++++++++-------- 5 files changed, 42 insertions(+), 32 deletions(-) diff --git a/client/src/components/tag-input/index.jsx b/client/src/components/tag-input/index.jsx index 3f6da25..e1d3910 100644 --- a/client/src/components/tag-input/index.jsx +++ b/client/src/components/tag-input/index.jsx @@ -30,7 +30,7 @@ export default function TagInput({ placeholder, seeMoreAction, seeMoreAfter, - setGetRorChildren, + switchGetRorChildren, tags, }) { const [excludedValues, setExcludedValues] = useState(deletedTags); @@ -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" > @@ -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), }; @@ -225,6 +225,6 @@ TagInput.defaultProps = { placeholder: '', seeMoreAfter: SEE_MORE_AFTER, seeMoreAction: undefined, - setGetRorChildren: () => { }, + switchGetRorChildren: () => { }, tags: [], }; diff --git a/client/src/components/tiles/openalex.jsx b/client/src/components/tiles/openalex.jsx index c54a394..f2a16ee 100644 --- a/client/src/components/tiles/openalex.jsx +++ b/client/src/components/tiles/openalex.jsx @@ -1,4 +1,3 @@ - import { FormattedMessage } from 'react-intl'; export default function OpenalexTile() { diff --git a/client/src/pages/openalex-ror/components/works-list.jsx b/client/src/pages/openalex-ror/components/works-list.jsx index 4ef67f0..b51b30e 100644 --- a/client/src/pages/openalex-ror/components/works-list.jsx +++ b/client/src/pages/openalex-ror/components/works-list.jsx @@ -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 ( @@ -23,7 +23,7 @@ export default function WorksList({ works }) { { works.length > 5 && ( ) } diff --git a/client/src/pages/openalex-ror/results/index.jsx b/client/src/pages/openalex-ror/results/index.jsx index 936a54e..a40cd10 100644 --- a/client/src/pages/openalex-ror/results/index.jsx +++ b/client/src/pages/openalex-ror/results/index.jsx @@ -142,6 +142,7 @@ 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 = []; @@ -149,19 +150,27 @@ export default function Affiliations() { 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, diff --git a/client/src/pages/openalex-ror/search.jsx b/client/src/pages/openalex-ror/search.jsx index e16f59e..97c1cfd 100644 --- a/client/src/pages/openalex-ror/search.jsx +++ b/client/src/pages/openalex-ror/search.jsx @@ -25,7 +25,7 @@ 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(); @@ -33,7 +33,6 @@ export default function OpenalexRorSearch() { 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); @@ -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', }); @@ -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', }); @@ -82,7 +83,6 @@ export default function OpenalexRorSearch() { } }, [ deletedAffiliations, - getRorChildren, searchedAffiliations, searchParams, setSearchParams, @@ -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), @@ -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', @@ -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', @@ -159,7 +159,7 @@ export default function OpenalexRorSearch() { }; getData(); - }, [deletedAffiliations, getRorChildren, searchedAffiliations]); + }, [currentSearchParams.getRorChildren, deletedAffiliations, searchedAffiliations]); const onTagsChange = async (_affiliations, _deletedAffiliations) => { const affiliations = _affiliations @@ -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); @@ -259,7 +261,7 @@ export default function OpenalexRorSearch() { @@ -299,7 +301,7 @@ export default function OpenalexRorSearch() {