Skip to content

Commit

Permalink
Move shouldShowInvalidSearchPage helper content into the search result
Browse files Browse the repository at this point in the history
..component. Because of other refactorings, the content of the helper
function was miniscule, and as such can nicely fit into an if statement.
  • Loading branch information
Adamik10 committed Apr 23, 2024
1 parent 97a3515 commit 71795c1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
9 changes: 0 additions & 9 deletions src/apps/search-result/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,4 @@ export const getFirstMaterialTypeFromFilters = (
: undefined;
};

export const shouldShowInvalidSearchPage = (
q: string,
qUrlParameter: string | null
) => {
return (
(!qUrlParameter && !q) || (!qUrlParameter && q === "null") || q.length < 3
);
};

export default {};
21 changes: 11 additions & 10 deletions src/apps/search-result/search-result.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,13 @@ import useFilterHandler from "./useFilterHandler";
import SearchResultSkeleton from "./search-result-skeleton";
import SearchResultZeroHits from "./search-result-zero-hits";
import SearchResultInvalidSearch from "./search-result-not-valid-search";
import { shouldShowInvalidSearchPage } from "./helper";

interface SearchResultProps {
q: string;
pageSize: number;
}

const SearchResult: React.FC<SearchResultProps> = ({ q, pageSize }) => {
const queryParametersFromUrl = new URLSearchParams(window.location.search);
const qUrlParameter = queryParametersFromUrl.get("q");
const { filters, clearFilter, addFilterFromUrlParamListener } =
useFilterHandler();
const cleanBranches = useGetCleanBranches();
Expand All @@ -55,6 +52,7 @@ const SearchResult: React.FC<SearchResultProps> = ({ q, pageSize }) => {
null
);
const { facets: campaignFacets } = useGetFacets(q, filters);
const minimalQueryLength = 3;

// If q changes (eg. in Storybook context)
// then make sure that we reset the entire result set.
Expand Down Expand Up @@ -99,12 +97,15 @@ const SearchResult: React.FC<SearchResultProps> = ({ q, pageSize }) => {
addFilterFromUrlParamListener(FacetField.WorkTypes);
}, [addFilterFromUrlParamListener]);

const { data, isLoading } = useSearchWithPaginationQuery({
q: { all: q },
offset: page * pageSize,
limit: pageSize,
filters: createFilters(filters, cleanBranches)
});
const { data, isLoading } = useSearchWithPaginationQuery(
{
q: { all: q },
offset: page * pageSize,
limit: pageSize,
filters: createFilters(filters, cleanBranches)
},
{ enabled: q.length >= minimalQueryLength }
);

useEffect(() => {
if (!data) {
Expand Down Expand Up @@ -167,7 +168,7 @@ const SearchResult: React.FC<SearchResultProps> = ({ q, pageSize }) => {
if (filtersUrlParam !== "usePersistedFilters") clearFilter();
}, [clearFilter]);

if (shouldShowInvalidSearchPage(q, qUrlParameter)) {
if (!q || q.length < minimalQueryLength) {
return <SearchResultInvalidSearch />;
}

Expand Down

0 comments on commit 71795c1

Please sign in to comment.