-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
119 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,37 @@ | ||
import Story from '@/components/Story'; | ||
import Image from 'next/image'; | ||
import Pagination from '@/app/subscribe/components/Pagination'; | ||
import Link from 'next/link'; | ||
import { useSearchContext } from '../SearchFetcher/SearchContext'; | ||
|
||
interface ContentListProps { | ||
currentPage: number; | ||
onPageChange: (page: number) => void; | ||
} | ||
|
||
export default function ContentList({ | ||
currentPage, | ||
onPageChange, | ||
}: ContentListProps) { | ||
export default function ContentList() { | ||
const { postSearchItem } = useSearchContext(); | ||
const { postSearchList, isFirst, isLast } = postSearchItem; | ||
|
||
return ( | ||
<div> | ||
<div className="flex flex-col gap-30"> | ||
{postSearchList.map(({ blogId, title, postId, preview }) => ( | ||
<Link | ||
href={`/blog/${blogId}/post/${postId}`} | ||
key={postId} | ||
className="flex flex-row gap-10 cursor-pointer" | ||
> | ||
<div className="absolute w-150 h-150"> | ||
{/* TODO: photoUrl 적용 */} | ||
<Image | ||
src="/tempImage/4.jpg" | ||
alt={String(postId)} | ||
layout="fill" | ||
objectFit="cover" | ||
className="rounded-3" | ||
/> | ||
</div> | ||
<div className="relative pl-170 py-1"> | ||
<Story key={postId} title={title} content={preview} /> | ||
</div> | ||
</Link> | ||
))} | ||
{postSearchItem.postSearchList.map( | ||
({ blogId, title, postId, preview }) => ( | ||
<Link | ||
href={`/blog/${blogId}/post/${postId}`} | ||
key={postId} | ||
className="flex flex-row gap-10 cursor-pointer" | ||
> | ||
<div className="absolute w-150 h-150"> | ||
<Image | ||
src="/tempImage/4.jpg" | ||
alt={String(postId)} | ||
layout="fill" | ||
objectFit="cover" | ||
className="rounded-3" | ||
/> | ||
</div> | ||
<div className="relative pl-170 py-1"> | ||
<Story key={postId} title={title} content={preview} /> | ||
</div> | ||
</Link> | ||
), | ||
)} | ||
</div> | ||
<Pagination | ||
isFirst={isFirst} | ||
isLast={isLast} | ||
currentPage={currentPage} | ||
onPageChange={onPageChange} | ||
/> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
'use client'; | ||
|
||
import { generateContext } from '@/react-utils'; | ||
import { SearchResponse } from './types'; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,87 @@ | ||
import { ReactNode, useEffect } from 'react'; | ||
import { ReactNode, useCallback, useMemo } from 'react'; | ||
import { useIntersect } from '@/hooks'; | ||
import { SearchResultProvider } from './SearchContext'; | ||
import { useSearchResult } from './quries'; | ||
import { useSearchResult } from './queries'; | ||
import { SearchResponse } from './types'; | ||
|
||
interface SearchResultFetcherProps { | ||
children: ReactNode; | ||
page: number; | ||
size: number; | ||
keyword: string; | ||
} | ||
|
||
export function SearchResultFetcher({ | ||
children, | ||
page, | ||
size, | ||
keyword, | ||
}: SearchResultFetcherProps) { | ||
const { data, refetch } = useSearchResult(page, size, keyword); | ||
const { data, isLoading, fetchNextPage, hasNextPage } = useSearchResult( | ||
keyword, | ||
size, | ||
); | ||
|
||
useEffect(() => { | ||
refetch(); | ||
}, [keyword, refetch]); | ||
const handleIntersect = useCallback( | ||
(entry: IntersectionObserverEntry) => { | ||
if (entry.isIntersecting && hasNextPage && !isLoading) { | ||
fetchNextPage(); | ||
} | ||
}, | ||
[isLoading, hasNextPage, fetchNextPage], | ||
); | ||
|
||
return <SearchResultProvider {...data}>{children}</SearchResultProvider>; | ||
const endOfListRef = useIntersect(handleIntersect); | ||
const pages = data?.pages || []; | ||
const mergedPostSearchItem = pages.reduce( | ||
(acc, { result }) => { | ||
acc.postSearchList.push(...result.postSearchItem.postSearchList); | ||
acc.listSize += result.postSearchItem.listSize; | ||
acc.totalPage = result.postSearchItem.totalPage; | ||
acc.totalElements = result.postSearchItem.totalElements; | ||
acc.hasNext = result.postSearchItem.hasNext; | ||
return acc; | ||
}, | ||
{ | ||
postSearchList: [] as SearchResponse['postSearchItem']['postSearchList'], | ||
listSize: 0, | ||
totalPage: 0, | ||
totalElements: 0, | ||
isFirst: true, | ||
isLast: false, | ||
hasNext: false, | ||
}, | ||
); | ||
|
||
const mergedBlogInfoItem = pages.reduce( | ||
(acc, { result }) => { | ||
acc.blogInfoList.push(...result.blogInfoItem.blogInfoList); | ||
acc.listSize += result.blogInfoItem.listSize; | ||
acc.totalPage = result.blogInfoItem.totalPage; | ||
acc.totalElements = result.blogInfoItem.totalElements; | ||
acc.hasNext = result.blogInfoItem.hasNext; | ||
return acc; | ||
}, | ||
{ | ||
blogInfoList: [] as SearchResponse['blogInfoItem']['blogInfoList'], | ||
listSize: 0, | ||
totalPage: 0, | ||
totalElements: 0, | ||
isFirst: true, | ||
isLast: false, | ||
hasNext: false, | ||
}, | ||
); | ||
|
||
const mergedData = useMemo(() => { | ||
return { | ||
postSearchItem: mergedPostSearchItem, | ||
blogInfoItem: mergedBlogInfoItem, | ||
}; | ||
}, [mergedPostSearchItem, mergedBlogInfoItem]); | ||
|
||
return ( | ||
<SearchResultProvider {...mergedData}> | ||
{children} | ||
<div ref={endOfListRef} /> | ||
</SearchResultProvider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { useInfiniteQuery } from '@tanstack/react-query'; | ||
import { getSearchResult } from './api'; | ||
|
||
export const useSearchResult = (keyword: string, size: number) => { | ||
const { data, isLoading, ...rest } = useInfiniteQuery({ | ||
queryKey: ['search-result', keyword], | ||
queryFn: ({ pageParam }) => getSearchResult(pageParam, keyword, size), | ||
initialPageParam: 0, | ||
getNextPageParam: (lastPage, allPages) => { | ||
const nextPage = allPages.length; | ||
return lastPage.result.postSearchItem.hasNext ? nextPage : undefined; | ||
}, | ||
refetchOnMount: false, | ||
}); | ||
return { data, isLoading, ...rest }; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters