Skip to content

Commit

Permalink
fix(openalex): Fix filter and sort feature
Browse files Browse the repository at this point in the history
  • Loading branch information
annelhote committed Dec 2, 2024
1 parent c418900 commit 6c7d3cf
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions client/src/pages/openalex-ror/results/views-selector.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,34 +49,26 @@ export default function ViewsSelector({

useEffect(() => {
// Deep copy of filteredAffiliations object
const initialAffiliations = JSON.parse(JSON.stringify(filteredAffiliations));
if (sortsAndFilters.sortOnNumberOfRors === 'default') {
setSortedOrFilteredAffiliations(initialAffiliations);
}
let initialAffiliations = JSON.parse(JSON.stringify(filteredAffiliations));
if (sortsAndFilters.sortOnNumberOfRors === 'numberASC') {
setSortedOrFilteredAffiliations(initialAffiliations.sort((a, b) => a.rors.length - b.rors.length));
}
if (sortsAndFilters.sortOnNumberOfRors === 'numberDESC') {
setSortedOrFilteredAffiliations(initialAffiliations.sort((a, b) => b.rors.length - a.rors.length));
}
if (sortsAndFilters.sortOnNumberOfRors === 'empty') {
setSortedOrFilteredAffiliations(initialAffiliations.filter((affiliation) => affiliation.rors.length === 0));
}
if (sortsAndFilters.showAffiliations === 'all') {
setSortedOrFilteredAffiliations(initialAffiliations);
initialAffiliations.sort((a, b) => a.rors.length - b.rors.length);
} else if (sortsAndFilters.sortOnNumberOfRors === 'numberDESC') {
initialAffiliations.sort((a, b) => b.rors.length - a.rors.length);
} else if (sortsAndFilters.sortOnNumberOfRors === 'empty') {
initialAffiliations = initialAffiliations.filter((affiliation) => affiliation.rors.length === 0);
}

if (sortsAndFilters.showAffiliations === 'onlyWithCorrections') {
setSortedOrFilteredAffiliations(initialAffiliations.filter((affiliation) => affiliation.hasCorrection));
}
if (sortsAndFilters.showAffiliations === 'onlyWithNoCorrection') {
setSortedOrFilteredAffiliations(initialAffiliations.filter((affiliation) => !affiliation.hasCorrection));
}
if (sortsAndFilters.rorCountry === 'all') {
setSortedOrFilteredAffiliations(initialAffiliations);
initialAffiliations = initialAffiliations.filter((affiliation) => affiliation.hasCorrection);
} else if (sortsAndFilters.showAffiliations === 'onlyWithNoCorrection') {
initialAffiliations = initialAffiliations.filter((affiliation) => !affiliation.hasCorrection);
}

if (sortsAndFilters.rorCountry !== 'all') {
setSortedOrFilteredAffiliations(initialAffiliations.filter((affiliation) => affiliation.rors.some((ror) => ror.rorCountry === sortsAndFilters.rorCountry)));
initialAffiliations = initialAffiliations.filter((affiliation) => affiliation.rors.some((ror) => ror.rorCountry === sortsAndFilters.rorCountry));
}

setSortedOrFilteredAffiliations(initialAffiliations);
}, [filteredAffiliations, sortsAndFilters]);

const changeView = (view) => {
Expand Down

0 comments on commit 6c7d3cf

Please sign in to comment.