-
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.
Browse files
Browse the repository at this point in the history
…over 둘러보기(discover) 페이지 구현 #82
- Loading branch information
Showing
3 changed files
with
127 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { useSuspenseInfiniteQuery } from '@tanstack/react-query'; | ||
import { isAxiosError } from 'axios'; | ||
|
||
import { fetchInstance } from '../instance'; | ||
|
||
export type Product = { | ||
id: number; | ||
name: string; | ||
artist: string; | ||
price: number; | ||
thumbnailUrl: string; | ||
}; | ||
|
||
type GetFeedResponse = { | ||
hasNext: boolean; | ||
products: Product[]; | ||
}; | ||
|
||
async function getFeed(size: number, pageParam: number): Promise<GetFeedResponse> { | ||
try { | ||
const response = await fetchInstance().get(`/products/feed?size=${size}&page=${pageParam}`); | ||
// console.log('getFeed response: ', response); | ||
|
||
return response.data; | ||
} catch (error) { | ||
if (isAxiosError(error)) { | ||
if (error.response) { | ||
throw new Error(error.response.data.message || '피드 가져오기 실패'); | ||
} else { | ||
throw new Error('네트워크 오류 또는 서버에 연결할 수 없습니다.'); | ||
} | ||
} else { | ||
throw new Error('알 수 없는 오류입니다.'); | ||
} | ||
} | ||
} | ||
|
||
const useGetFeed = () => { | ||
const size = 20; | ||
|
||
return useSuspenseInfiniteQuery<GetFeedResponse, Error, void>({ | ||
queryKey: ['feed', size], | ||
queryFn: ({ pageParam = 0 }) => getFeed(size, pageParam as number), | ||
initialPageParam: 0, | ||
getNextPageParam: (lastPage) => { | ||
return lastPage.hasNext ? lastPage.products.length / size + 1 : undefined; | ||
}, | ||
}); | ||
}; | ||
|
||
export default useGetFeed; |
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,5 +1,76 @@ | ||
import styled from '@emotion/styled'; | ||
import { Suspense, useEffect } from 'react'; | ||
|
||
import useGetFeed, { type Product } from '@/apis/products/useGetFeed'; | ||
import { HEADER_HEIGHT } from '@/components/layouts/Header'; | ||
import SearchBar from '@/components/layouts/SearchBar'; | ||
import { TABBAR_HEIGHT } from '@/components/layouts/TabBar'; | ||
|
||
const Discover = () => { | ||
return <>Discover</>; | ||
const { data, fetchNextPage, hasNextPage } = useGetFeed(); | ||
|
||
// 스크롤 내려감에 따라 다음 페이지 데이터 페칭 | ||
const handleScroll = () => { | ||
const { scrollTop, scrollHeight, clientHeight } = document.documentElement; | ||
if (scrollTop + clientHeight >= scrollHeight - 100 && hasNextPage) { | ||
fetchNextPage(); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
window.addEventListener('scroll', handleScroll); | ||
|
||
return () => window.removeEventListener('scroll', handleScroll); // 언마운트될 때 이벤트 리스너 해제 | ||
}, [fetchNextPage, hasNextPage]); | ||
|
||
return ( | ||
<Wrapper> | ||
<SearchBar /> | ||
<ContentWrapper> | ||
<Suspense fallback={<>Loading...</>}> | ||
<ImageGrid> | ||
{data?.pages.map((page) => | ||
Check failure on line 32 in src/pages/Discover/index.tsx GitHub Actions / Deploy
|
||
page.products.map((product: Product) => ( | ||
<ImageItem key={product.id}> | ||
<img src={product.thumbnailUrl} alt={product.name} /> | ||
</ImageItem> | ||
)), | ||
)} | ||
</ImageGrid> | ||
</Suspense> | ||
</ContentWrapper> | ||
</Wrapper> | ||
); | ||
}; | ||
|
||
export default Discover; | ||
|
||
const Wrapper = styled.div` | ||
flex: 1; | ||
display: flex; | ||
flex-direction: column; | ||
margin: ${HEADER_HEIGHT} 0 ${TABBAR_HEIGHT} 0; | ||
`; | ||
|
||
const ContentWrapper = styled.div` | ||
padding: 4px; | ||
overflow-y: auto; | ||
flex: 1; | ||
`; | ||
|
||
const ImageGrid = styled.div` | ||
display: grid; | ||
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); | ||
gap: 8px; | ||
`; | ||
|
||
const ImageItem = styled.div` | ||
border-radius: var(--border-radius); | ||
overflow: hidden; | ||
img { | ||
width: 100%; | ||
height: auto; | ||
display: block; | ||
} | ||
`; |