Skip to content

Commit

Permalink
Adding check for URL changes. Moving forward with running search if a…
Browse files Browse the repository at this point in the history
…ny updates are required. Simplifying code.
  • Loading branch information
erinesullivan committed Sep 27, 2024
1 parent 2e09126 commit 5696274
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
9 changes: 2 additions & 7 deletions src/modules/datastores/components/DatastoreRoute/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@ import React from 'react';
const DatastoreRoute = () => {
const { datastoreSlug } = useParams();
const location = useLocation();
const { list } = config.datastores;
const slugDs = list.find((datastore) => {
return datastore.slug === datastoreSlug;
const isDatastore = config.datastores.list.some(({ slug, uid }) => {
return [slug, uid].includes(datastoreSlug);
});
const uidDs = list.find((datastore) => {
return datastore.uid === datastoreSlug;
});
const isDatastore = Boolean(slugDs || uidDs);
const urlState = getStateFromURL({ location });

if (isDatastore && urlState) {
Expand Down
41 changes: 20 additions & 21 deletions src/modules/pride/components/URLSearchQueryWrapper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
runSearch,
switchPrideToDatastore
} from '../../../pride';
import { memo, useEffect } from 'react';
import { memo, useEffect, useMemo } from 'react';
import {
searching,
setPage,
Expand Down Expand Up @@ -43,7 +43,7 @@ const handleURLState = ({
}

if (!datastoreUid) {
return null;
return;
}

const updateRequired = {
Expand All @@ -54,20 +54,18 @@ const handleURLState = ({
sort: urlState.sort !== sort
};

// Run search and apply updates based on changes
if (Object.values(updateRequired).some(Boolean)) {
// If URL has a state, apply updates based on changes
if (Object.keys(urlState).length) {
if (updateRequired.query) {
const newQuery = urlState.query || '';
dispatch(setSearchQuery(newQuery));
dispatch(setSearchQueryInput(newQuery));
}

if (updateRequired.filters) {
const actionProps = {
datastoreUid,
filters: urlState.filter || null
};
dispatch(urlState.filter ? setActiveFilters(actionProps) : clearActiveFilters(actionProps));
dispatch(urlState.filter
? setActiveFilters({ datastoreUid, filters: urlState.filter })
: clearActiveFilters({ datastoreUid }));
}

if (updateRequired.page) {
Expand All @@ -88,21 +86,20 @@ const handleURLState = ({
dispatch(setActiveInstitution(urlState.library));
}

dispatch(setA11yMessage('Search modified.'));
dispatch(setParserMessage(null));
runSearch();
}

// Run search if any updates are true
if (Object.values(updateRequired).some(Boolean)) {
dispatch(setA11yMessage('Search modified.'));
dispatch(setParserMessage(null));
runSearch();
}
// If URL does not have a state, reset the filters and the query
if (!Object.keys(urlState).length) {
} else {
dispatch(resetFilters());
dispatch(setSearchQuery(''));
}

// Decide if the UI should be in a "Searching" state based on URL having query or filter
dispatch(searching(Boolean(urlState.query || urlState.filter)));

return null;
};

const URLSearchQueryWrapper = ({ children }) => {
Expand All @@ -118,7 +115,7 @@ const URLSearchQueryWrapper = ({ children }) => {
const institution = useSelector((state) => {
return state.institution;
});
const { query, searching: isSearching } = useSelector((state) => {
const { query } = useSelector((state) => {
return state.search;
});
const page = useSelector((state) => {
Expand All @@ -128,21 +125,23 @@ const URLSearchQueryWrapper = ({ children }) => {
return state.search.sort[activeDatastore];
});

const datastoreUid = useMemo(() => {
return getDatastoreUidBySlug(params.datastoreSlug);
}, [params.datastoreSlug]);

useEffect(() => {
const datastoreUid = getDatastoreUidBySlug(params.datastoreSlug);
switchPrideToDatastore(datastoreUid);

handleURLState({
activeFilters,
datastoreUid,
institution,
isSearching,
location,
page,
query,
sort
}, dispatch);
}, [params.datastoreSlug, isSearching, query, activeFilters, location, page, sort, institution, dispatch]);
}, [params.datastoreSlug, query, activeFilters, location, page, sort, institution, dispatch]);

return children;
};
Expand Down

0 comments on commit 5696274

Please sign in to comment.