Skip to content

Commit

Permalink
fix: 태그 검색의 endpoint를 tagId로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
xodms0309 committed Jun 3, 2024
1 parent fc8190a commit 4820834
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,11 @@ import { useIntersectionObserver } from '@/hooks/common';
import { useInfiniteProductSearchResultsQuery } from '@/hooks/queries/search';

interface TagSearchResultListProps {
searchQuery: string;
tagId: string;
}

const TagSearchResultList = ({ searchQuery }: TagSearchResultListProps) => {
const {
data: searchResponse,
fetchNextPage,
hasNextPage,
} = useInfiniteProductSearchResultsQuery(searchQuery, 'tags');
const TagSearchResultList = ({ tagId }: TagSearchResultListProps) => {
const { data: searchResponse, fetchNextPage, hasNextPage } = useInfiniteProductSearchResultsQuery(tagId, 'tags');

const scrollRef = useRef<HTMLDivElement>(null);
useIntersectionObserver<HTMLDivElement>(fetchNextPage, scrollRef, hasNextPage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type SearchResultEndPoint = 'tags' | 'products';
const fetchProductSearchResults = async (query: string, endpoint: SearchResultEndPoint, pageParam: number) => {
const response = await searchApi.get({
params: `/${endpoint}/results`,
queries: `?query=${query}&lastProductId=${pageParam}`,
queries: `?${endpoint === 'tags' ? 'tagId' : 'query'}=${query}&lastProductId=${pageParam}`,
});
const data: ProductSearchResultResponse = await response.json();

Expand Down
7 changes: 3 additions & 4 deletions src/hooks/search/useSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,11 @@ const useSearch = () => {
setIsAutocompleteOpen(false);
};

const handleTagSearch: MouseEventHandler<HTMLButtonElement> = (event) => {
const { value } = event.currentTarget;
setSearchQuery(value);
const handleTagSearch = (id: number, name: string) => {
setSearchQuery(name);
setIsSubmitted(true);

navigate(`${PATH.SEARCH}/tags?query=${value}`);
navigate(`${PATH.SEARCH}/tags?query=${name}&id=${id}`);
};

const resetSearchQuery = () => {
Expand Down
12 changes: 10 additions & 2 deletions src/mocks/handlers/searchHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import searchingProducts from '../data/searchingProducts.json';
export const searchHandlers = [
rest.get('/api/search/:searchId/results', (req, res, ctx) => {
const { searchId } = req.params;
const query = req.url.searchParams.get('query');
const query = req.url.searchParams.get('query') || req.url.searchParams.get('tagId');
const page = Number(req.url.searchParams.get('page'));

if (query === null) {
return res(ctx.status(400));
}

if (searchId === 'products' || searchId === 'tags') {
if (searchId === 'products') {
const filteredProducts = {
page: { ...productSearchResults.page },
products: productSearchResults.products
Expand All @@ -24,6 +24,14 @@ export const searchHandlers = [
return res(ctx.status(200), ctx.json(filteredProducts), ctx.delay(1000));
}

if (searchId === 'tags') {
const filteredProducts = {
page: { ...productSearchResults.page },
products: productSearchResults.products.slice(page * 5, (page + 1) * 5),
};
return res(ctx.status(200), ctx.json(filteredProducts), ctx.delay(1000));
}

if (searchId === 'recipes') {
const filteredRecipes = {
page: { ...recipeResponse.page },
Expand Down
2 changes: 1 addition & 1 deletion src/pages/SearchPage/SearchPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export const SearchPage = () => {
</Text>
<div className={badgeContainer}>
{RECOMMENDED_TAGS.map(({ id, name }) => (
<button type="button" key={id} value={name} onClick={handleTagSearch}>
<button type="button" key={id} value={name} onClick={() => handleTagSearch(id, name)}>
<Badge color="#e6e6e6" textColor="#808080">
{name}
</Badge>
Expand Down
6 changes: 5 additions & 1 deletion src/pages/SearchPage/TagSearchResultPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Suspense } from 'react';
import { useSearchParams } from 'react-router-dom';

import { form, formWrapper, searchResultTitle, searchSection } from './searchPage.css';

Expand All @@ -9,6 +10,9 @@ import { useSearch } from '@/hooks/search';
export const TagSearchResultPage = () => {
const { inputRef, searchQuery, isSubmitted, handleSearchQuery, handleSearchForm } = useSearch();

const [searchParams, _setSearchParams] = useSearchParams();
const tagId = searchParams.get('id') || '';

return (
<>
<div className={formWrapper}>
Expand All @@ -28,7 +32,7 @@ export const TagSearchResultPage = () => {
</Text>
<ErrorBoundary fallback={ErrorComponent}>
<Suspense fallback={<Loading />}>
<TagSearchResultList searchQuery={searchQuery} />
<TagSearchResultList tagId={tagId} />
</Suspense>
</ErrorBoundary>
</section>
Expand Down

0 comments on commit 4820834

Please sign in to comment.