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] refactor: 검색 페이지 이슈 수정 #481

Merged
merged 5 commits into from
Aug 17, 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
25 changes: 18 additions & 7 deletions frontend/src/components/Search/RecommendList/RecommendList.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { Link, Text } from '@fun-eat/design-system';
import { Button, Text } from '@fun-eat/design-system';
import type { MouseEventHandler } from 'react';
import { useRef } from 'react';
import { Link as RouterLink } from 'react-router-dom';
import styled from 'styled-components';

import { MarkedText } from '@/components/Common';
import { PATH } from '@/constants/path';
import { useIntersectionObserver } from '@/hooks/common';
import { useInfiniteProductSearchAutocompleteQuery } from '@/hooks/queries/search';

interface RecommendListProps {
searchQuery: string;
handleSearchClick: MouseEventHandler<HTMLButtonElement>;
}

const RecommendList = ({ searchQuery }: RecommendListProps) => {
const RecommendList = ({ searchQuery, handleSearchClick }: RecommendListProps) => {
const { data: searchResponse, fetchNextPage, hasNextPage } = useInfiniteProductSearchAutocompleteQuery(searchQuery);
const scrollRef = useRef<HTMLDivElement>(null);
useIntersectionObserver<HTMLDivElement>(fetchNextPage, scrollRef, hasNextPage);
Expand All @@ -26,11 +26,18 @@ const RecommendList = ({ searchQuery }: RecommendListProps) => {
return (
<>
<RecommendListContainer>
{products.map(({ id, name, categoryType }) => (
{products.map(({ id, name }) => (
<li key={id}>
<Link as={RouterLink} to={`${PATH.PRODUCT_LIST}/${categoryType}/${id}`} block>
<ProductButton
type="button"
customWidth="100%"
customHeight="100%"
color="white"
value={name}
onClick={handleSearchClick}
>
<MarkedText text={name} mark={searchQuery} />
</Link>
</ProductButton>
</li>
))}
</RecommendListContainer>
Expand All @@ -51,6 +58,10 @@ const RecommendListContainer = styled.ul`
}
`;

const ProductButton = styled(Button)`
text-align: left;
`;

const ErrorText = styled(Text)`
height: 36px;
padding: 0 10px;
Expand Down
21 changes: 16 additions & 5 deletions frontend/src/hooks/search/useSearch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ChangeEventHandler, FormEventHandler } from 'react';
import type { ChangeEventHandler, FormEventHandler, MouseEventHandler } from 'react';
import { useState } from 'react';
import { useSearchParams } from 'react-router-dom';

Expand All @@ -16,21 +16,32 @@ const useSearch = () => {

const handleSearch: FormEventHandler<HTMLFormElement> = (event) => {
event.preventDefault();
const trimmedSearchQuery = searchQuery.trim();

if (!searchQuery) {
if (!trimmedSearchQuery) {
alert('검색어를 입력해주세요');
setSearchQuery('');
return;
}

if (currentSearchQuery === searchQuery) {
if (currentSearchQuery === trimmedSearchQuery) {
return;
}

setSearchQuery(trimmedSearchQuery);
setIsSubmitted(true);
setSearchParams({ query: searchQuery });
setSearchParams({ query: trimmedSearchQuery });
};

return { searchQuery, isSubmitted, handleSearchQuery, handleSearch };
const handleSearchClick: MouseEventHandler<HTMLButtonElement> = (event) => {
const { value } = event.currentTarget;

setSearchQuery(value);
setIsSubmitted(true);
setSearchParams({ query: value });
};

return { searchQuery, isSubmitted, handleSearchQuery, handleSearch, handleSearchClick };
};

export default useSearch;
23 changes: 17 additions & 6 deletions frontend/src/pages/SearchPage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Button, Heading, Spacing, Text } from '@fun-eat/design-system';
import { useQueryErrorResetBoundary } from '@tanstack/react-query';
import type { MouseEventHandler } from 'react';
import { Suspense, useState } from 'react';
import { Suspense, useEffect, useRef, useState } from 'react';
import styled from 'styled-components';

import { ErrorBoundary, ErrorComponent, Input, Loading, SvgIcon, TabMenu } from '@/components/Common';
Expand All @@ -10,11 +10,16 @@ import { SEARCH_PAGE_TABS } from '@/constants';
import { useDebounce } from '@/hooks/common';
import { useSearch } from '@/hooks/search';

const isProductSearchTab = (tabMenu: string) => tabMenu === SEARCH_PAGE_TABS[0];
const getInputPlaceholder = (tabMenu: string) =>
isProductSearchTab(tabMenu) ? '상품 이름을 검색해보세요.' : '꿀조합에 포함된 상품을 입력해보세요.';

const SearchPage = () => {
const { searchQuery, isSubmitted, handleSearchQuery, handleSearch } = useSearch();
const { searchQuery, isSubmitted, handleSearchQuery, handleSearch, handleSearchClick } = useSearch();
const [debouncedSearchQuery, setDebouncedSearchQuery] = useState(searchQuery || '');
const [selectedTabMenu, setSelectedTabMenu] = useState<string>(SEARCH_PAGE_TABS[0]);
const { reset } = useQueryErrorResetBoundary();
const inputRef = useRef<HTMLInputElement>(null);

const handleTabMenuSelect: MouseEventHandler<HTMLButtonElement> = (event) => {
setSelectedTabMenu(event.currentTarget.value);
Expand All @@ -28,27 +33,34 @@ const SearchPage = () => {
[searchQuery]
);

useEffect(() => {
if (inputRef.current) {
inputRef.current.focus();
}
}, []);

return (
<>
<SearchSection>
<form onSubmit={handleSearch}>
<Input
customWidth="100%"
placeholder="상품 이름을 검색해보세요."
placeholder={getInputPlaceholder(selectedTabMenu)}
rightIcon={
<Button customHeight="36px" color="white">
<SvgIcon variant="search" />
</Button>
}
value={searchQuery}
onChange={handleSearchQuery}
ref={inputRef}
/>
</form>
{!isSubmitted && debouncedSearchQuery && (
<RecommendWrapper>
<ErrorBoundary fallback={ErrorComponent} handleReset={reset}>
<Suspense fallback={<Loading />}>
<RecommendList searchQuery={debouncedSearchQuery} />
<RecommendList searchQuery={debouncedSearchQuery} handleSearchClick={handleSearchClick} />
</Suspense>
</ErrorBoundary>
</RecommendWrapper>
Expand All @@ -69,7 +81,7 @@ const SearchPage = () => {
<ErrorBoundary fallback={ErrorComponent}>
<Suspense fallback={<Loading />}>
<Spacing size={20} />
{selectedTabMenu === SEARCH_PAGE_TABS[0] ? (
{isProductSearchTab(selectedTabMenu) ? (
<ProductSearchResultList searchQuery={debouncedSearchQuery} />
) : (
<RecipeSearchResultList searchQuery={debouncedSearchQuery} />
Expand All @@ -96,7 +108,6 @@ const RecommendWrapper = styled.div`
top: 100%;
left: 0;
right: 0;
height: fit-content;
max-height: 150px;
padding: 10px 0;
background-color: ${({ theme }) => theme.backgroundColors.default};
Expand Down
Loading