Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FE] fix: 검색 결과 두 번 요청 수정 #676

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions frontend/src/hooks/search/useSearch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ChangeEventHandler, FormEventHandler, MouseEventHandler, RefObject } from 'react';
import { useEffect, useRef, useState } from 'react';
import type { ChangeEventHandler, FormEventHandler, MouseEventHandler } from 'react';
import { useRef, useState } from 'react';
import { useSearchParams } from 'react-router-dom';

const useSearch = () => {
Expand All @@ -12,19 +12,16 @@ const useSearch = () => {
const [isSubmitted, setIsSubmitted] = useState(!!currentSearchQuery);
const [isAutocompleteOpen, setIsAutocompleteOpen] = useState(searchQuery.length > 0);

useEffect(() => {
setIsAutocompleteOpen(searchQuery.length > 0);
}, [searchQuery]);

const focusInput = () => {
if (inputRef?.current) {
if (inputRef.current) {
inputRef.current.focus();
}
};

const handleSearchQuery: ChangeEventHandler<HTMLInputElement> = (event) => {
setIsSubmitted(false);
setSearchQuery(event.currentTarget.value);
setIsAutocompleteOpen(event.currentTarget.value.length > 0);
};

const handleSearch: FormEventHandler<HTMLFormElement> = (event) => {
Expand All @@ -35,7 +32,7 @@ const useSearch = () => {
if (!trimmedSearchQuery) {
alert('검색어를 입력해주세요');
focusInput();
setSearchQuery('');
resetSearchQuery();
return;
}

Expand Down
6 changes: 3 additions & 3 deletions frontend/src/pages/IntegratedSearchPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const IntegratedSearchPage = () => {
handleTabMenuSelect={handleTabMenuClick}
/>
<SearchResultSection>
{isSubmitted && debouncedSearchQuery ? (
{isSubmitted && searchQuery ? (
<>
<Heading as="h2" size="lg" weight="regular">
<Mark>&apos;{searchQuery}&apos;</Mark>에 대한 검색결과입니다.
Expand All @@ -88,9 +88,9 @@ const IntegratedSearchPage = () => {
<Suspense fallback={<Loading />}>
<Spacing size={20} />
{isProductSearchTab ? (
<ProductSearchResultList searchQuery={debouncedSearchQuery} />
<ProductSearchResultList searchQuery={searchQuery} />
) : (
<RecipeSearchResultList searchQuery={debouncedSearchQuery} />
<RecipeSearchResultList searchQuery={searchQuery} />
)}
</Suspense>
</ErrorBoundary>
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/pages/SearchPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,16 @@ const SearchPage = () => {
)}
</SearchSection>
<SearchResultSection>
{isSubmitted && debouncedSearchQuery ? (
{isSubmitted && searchQuery ? (
<>
<Heading as="h2" size="lg" weight="regular">
<Mark>&apos;{searchQuery}&apos;</Mark>에 대한 검색결과입니다.
</Heading>
<ErrorBoundary fallback={ErrorComponent}>
<Suspense fallback={<Loading />}>
<Spacing size={20} />
{isProductSearchPage && <ProductSearchResultList searchQuery={debouncedSearchQuery} />}
{isRecipeSearchPage && <RecipeSearchResultList searchQuery={debouncedSearchQuery} />}
{isProductSearchPage && <ProductSearchResultList searchQuery={searchQuery} />}
{isRecipeSearchPage && <RecipeSearchResultList searchQuery={searchQuery} />}
</Suspense>
</ErrorBoundary>
</>
Expand Down