Skip to content

Commit

Permalink
[MS-840] fix search refresh bug (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtek007mirumee authored Nov 15, 2024
1 parent b1d1186 commit 4821a4e
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions apps/storefront/src/components/header/search-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ export const SearchForm = ({ onSubmit }: { onSubmit?: () => void }) => {
useEffect(() => {
const inputValueLength = debouncedInputValue.length;

if (!isSearchPage && inputValueLength === 0) {
return;
}

void searchProducts(debouncedInputValue).then(({ results }) => {
setSearchState((state) => ({
...state,
Expand All @@ -170,7 +174,13 @@ export const SearchForm = ({ onSubmit }: { onSubmit?: () => void }) => {

if (isSearchPage) {
if (isLoading) {
router.push(paths.search.asPath({ query: { q: debouncedInputValue } }));
if (inputValueLength > 0) {
router.push(
paths.search.asPath({ query: { q: debouncedInputValue } }),
);
} else {
router.push(paths.search.asPath());
}
} else {
const params = new URLSearchParams(searchParams);

Expand All @@ -184,7 +194,7 @@ export const SearchForm = ({ onSubmit }: { onSubmit?: () => void }) => {
}, [debouncedInputValue]);

useEffect(() => {
if (isSearchPage || isHomePage) {
if (isSearchPage) {
return;
}
resetSearchState();
Expand Down Expand Up @@ -219,7 +229,9 @@ export const SearchForm = ({ onSubmit }: { onSubmit?: () => void }) => {
setSearchState((state) => ({
...state,
status:
event.target.value.length >= minLetters ? "LOADING" : "IDLE",
event.target.value.length < minLetters && !isSearchPage
? "IDLE"
: "LOADING",
inputValue: event.target.value,
})),
onKeyDown: handleKeyDown,
Expand Down

0 comments on commit 4821a4e

Please sign in to comment.