Skip to content

Commit

Permalink
search on search page is now not called if search query is empty
Browse files Browse the repository at this point in the history
- this is required as the API treats empty string as null and thus
throws an error
  • Loading branch information
sebaholesz committed Nov 3, 2023
1 parent 76de182 commit 5583b9d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 26 deletions.
5 changes: 2 additions & 3 deletions storefront/components/Pages/Search/SearchContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { ProductsSearch } from './ProductsSearch';
import { Heading } from 'components/Basic/Heading/Heading';
import { SimpleNavigation } from 'components/Blocks/SimpleNavigation/SimpleNavigation';
import { SkeletonPageProductsList } from 'components/Blocks/Skeleton/SkeletonPageProductsList';
import { Webline } from 'components/Layout/Webline/Webline';
import { SearchQueryApi, SimpleCategoryFragmentApi } from 'graphql/generated';
import { mapConnectionEdges } from 'helpers/mappers/connection';
import { getStringFromUrlQuery } from 'helpers/parsing/urlParsing';
Expand Down Expand Up @@ -30,7 +29,7 @@ export const SearchContent: FC<SearchContentProps> = ({ searchResults, fetching
const isFetchingInitialData = !searchResults && fetching;

return (
<Webline>
<>
<Heading type="h1">{`${t('Search results for')} "${getStringFromUrlQuery(router.query.q)}"`}</Heading>
{isFetchingInitialData ? (
<SkeletonPageProductsList />
Expand Down Expand Up @@ -69,6 +68,6 @@ export const SearchContent: FC<SearchContentProps> = ({ searchResults, fetching
</>
)
)}
</Webline>
</>
);
};
58 changes: 35 additions & 23 deletions storefront/pages/search.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MetaRobots } from 'components/Basic/Head/MetaRobots';
import { getEndCursor } from 'components/Blocks/Product/Filter/helpers/getEndCursor';
import { CommonLayout } from 'components/Layout/CommonLayout';
import { Webline } from 'components/Layout/Webline/Webline';
import { SearchContent } from 'components/Pages/Search/SearchContent';
import { DEFAULT_PAGE_SIZE } from 'config/constants';
import {
Expand Down Expand Up @@ -43,11 +44,12 @@ const SearchPage: FC<ServerSidePropsType> = () => {

const [{ data: searchData, fetching }] = useSearchQueryApi({
variables: {
search: searchString ?? '',
search: searchString!,
orderingMode: sort,
filter: mapParametersFilter(filter),
pageSize: DEFAULT_PAGE_SIZE * (currentLoadMore + 1),
},
pause: !searchString,
});

const [searchUrl] = getInternationalizedStaticUrls(['/search'], url);
Expand All @@ -62,7 +64,15 @@ const SearchPage: FC<ServerSidePropsType> = () => {
<>
<MetaRobots content="noindex, nofollow" />
<CommonLayout breadcrumbs={breadcrumbs} title={title}>
<SearchContent fetching={fetching} searchResults={searchData} />
<Webline>
{searchString ? (
<SearchContent fetching={fetching} searchResults={searchData} />
) : (
<div className="mb-5 p-12 text-center">
<strong>{t('There are no results as you have searched with an empty query...')}</strong>
</div>
)}
</Webline>
</CommonLayout>
</>
);
Expand All @@ -84,27 +94,29 @@ export const getServerSideProps = getServerSidePropsWrapper(({ redisClient, doma

return initServerSideProps({
context,
prefetchedQueries: [
{
query: SearchQueryDocumentApi,
variables: {
search,
orderingMode,
filter,
pageSize: DEFAULT_PAGE_SIZE * (loadMore + 1),
},
},
{
query: SearchProductsQueryDocumentApi,
variables: {
search,
orderingMode,
filter,
endCursor: getEndCursor(page),
pageSize: DEFAULT_PAGE_SIZE * (loadMore + 1),
},
},
],
prefetchedQueries: !search
? []
: [
{
query: SearchQueryDocumentApi,
variables: {
search,
orderingMode,
filter,
pageSize: DEFAULT_PAGE_SIZE * (loadMore + 1),
},
},
{
query: SearchProductsQueryDocumentApi,
variables: {
search,
orderingMode,
filter,
endCursor: getEndCursor(page),
pageSize: DEFAULT_PAGE_SIZE * (loadMore + 1),
},
},
],
redisClient,
domainConfig,
t,
Expand Down
1 change: 1 addition & 0 deletions storefront/public/locales/cs/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@
"The transport you selected is no longer available.": "Doprava, kterou jste si zvolili, již není dostupná.",
"There are no products in the shared wishlist.": "Sdílený seznam oblíbených produktů je prázdný.",
"There are no products in the wishlist. Add some first.": "Seznam oblíbených produktů je prázdný. Nejprve přidejte nějaké produkty do oblíbených.",
"There are no results as you have searched with an empty query...": "Vaše vyhledávání nevrátilo žádné výsledky, protože jste nezadali žádný dotaz...",
"There was an error while adding a promo code to the order.": "Během přidávání slevového kupónu do objednávky se vyskytla chyba.",
"There was an error while changing your password": "Při změně Vašeho hesla došlo k chybě",
"There was an error while deleting your delivery address": "Při mazání dodací adresy došlo k chybě",
Expand Down
1 change: 1 addition & 0 deletions storefront/public/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@
"The transport you selected is no longer available.": "The transport you selected is no longer available.",
"There are no products in the shared wishlist.": "There are no products in the shared wishlist.",
"There are no products in the wishlist. Add some first.": "There are no products in the wishlist. Add some first.",
"There are no results as you have searched with an empty query...": "There are no results as you have searched with an empty query...",
"There was an error while adding a promo code to the order.": "There was an error while adding a promo code to the order.",
"There was an error while changing your password": "There was an error while changing your password",
"There was an error while deleting your delivery address": "There was an error while deleting your delivery address",
Expand Down
1 change: 1 addition & 0 deletions storefront/public/locales/sk/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@
"The transport you selected is no longer available.": "The transport you selected is no longer available.",
"There are no products in the shared wishlist.": "Zdieľaný zoznam obľúbených produktov je prázdny.",
"There are no products in the wishlist. Add some first.": "Zoznam obľúbených produktov je prázdny. Najskôr pridajte niektoré produkty do svojich obľúbených.",
"There are no results as you have searched with an empty query...": "Vaše vyhľadávanie neprinieslo žiadne výsledky, pretože ste nezadali žiadny dotaz...",
"There was an error while adding a promo code to the order.": "Behom pridávania zľavového kupónu do objednávky sa vyskytla chyba.",
"There was an error while changing your password": "Pri zmene Vášho hesla došlo k chybe ",
"There was an error while deleting your delivery address": "Pri mazaní dodacej adresy došlo k chybe",
Expand Down

0 comments on commit 5583b9d

Please sign in to comment.