-
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.
* feat: 소식페이지 무한스크롤을 위한 API 훅 추가 * feat: 소식 페이지 무한 스크롤 추가 * fix: eslint-disable 삭제 * fix: eslint-disable 삭제
- Loading branch information
Showing
6 changed files
with
86 additions
and
28 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,13 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
|
||
import { fetchData } from '@/apis/fetch-data'; | ||
import { NewsResponse } from '@/features/news'; | ||
|
||
export async function GET(request: NextRequest) { | ||
const { searchParams } = new URL(request.url); | ||
const cursorId = searchParams.get('cursorId'); | ||
const queryString = cursorId ? `?cursorId=${cursorId}` : ''; | ||
const data = await fetchData<NewsResponse>(`/news${queryString}`, 'GET'); | ||
|
||
return NextResponse.json(data); | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './use-news-data'; |
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,25 @@ | ||
import { useInfiniteQuery } from '@tanstack/react-query'; | ||
|
||
import { NewsResponse } from '../types'; | ||
|
||
const getNewsData = async (cursorId: unknown) => { | ||
const queryString = cursorId ? `?cursorId=${cursorId as string}` : ''; | ||
const res = await fetch(`/api/news${queryString}`, { | ||
method: 'GET', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
return res.json(); | ||
}; | ||
|
||
export const useNewsData = () => { | ||
return useInfiniteQuery<NewsResponse>({ | ||
queryKey: ['newsData'], | ||
queryFn: ({ pageParam = undefined }) => getNewsData(pageParam), | ||
initialPageParam: null, | ||
getNextPageParam: (lastPage) => | ||
lastPage.data.hasNext ? lastPage.data.cursorId : undefined, | ||
}); | ||
}; |
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,13 @@ | ||
import { Response } from '@/apis'; | ||
import { TimeLineContent } from '@/features/main'; | ||
|
||
import { NewsItemWrapperProps } from '../components'; | ||
|
||
export type NewsItem = TimeLineContent & NewsItemWrapperProps; | ||
export type NewsContent = TimeLineContent & NewsItemWrapperProps; | ||
|
||
export type NewsResponse = Response<{ | ||
content: Array<NewsContent>; | ||
pageSize: number; | ||
cursorId: number; | ||
hasNext: boolean; | ||
}>; |