From 71795c13b099ff4290667a6e9cd20d39f59374f1 Mon Sep 17 00:00:00 2001 From: Adam Antal Date: Tue, 23 Apr 2024 21:42:00 +0200 Subject: [PATCH] Move shouldShowInvalidSearchPage helper content into the search result ..component. Because of other refactorings, the content of the helper function was miniscule, and as such can nicely fit into an if statement. --- src/apps/search-result/helper.ts | 9 --------- src/apps/search-result/search-result.tsx | 21 +++++++++++---------- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/src/apps/search-result/helper.ts b/src/apps/search-result/helper.ts index 19bea71e8e..2b14b7d3a4 100644 --- a/src/apps/search-result/helper.ts +++ b/src/apps/search-result/helper.ts @@ -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 {}; diff --git a/src/apps/search-result/search-result.tsx b/src/apps/search-result/search-result.tsx index fe3381706a..1177d7870c 100644 --- a/src/apps/search-result/search-result.tsx +++ b/src/apps/search-result/search-result.tsx @@ -30,7 +30,6 @@ 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; @@ -38,8 +37,6 @@ interface SearchResultProps { } const SearchResult: React.FC = ({ q, pageSize }) => { - const queryParametersFromUrl = new URLSearchParams(window.location.search); - const qUrlParameter = queryParametersFromUrl.get("q"); const { filters, clearFilter, addFilterFromUrlParamListener } = useFilterHandler(); const cleanBranches = useGetCleanBranches(); @@ -55,6 +52,7 @@ const SearchResult: React.FC = ({ 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. @@ -99,12 +97,15 @@ const SearchResult: React.FC = ({ 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) { @@ -167,7 +168,7 @@ const SearchResult: React.FC = ({ q, pageSize }) => { if (filtersUrlParam !== "usePersistedFilters") clearFilter(); }, [clearFilter]); - if (shouldShowInvalidSearchPage(q, qUrlParameter)) { + if (!q || q.length < minimalQueryLength) { return ; }