//API
export const getMessageList = async ({roomId, pageNum}: {roomId: string, pageNum: number}) => {
console.log("roomId", roomId, "pageNum", pageNum);
const token: string | undefined = Cookies.get("access_Token")
const res: Response = await instance.get(`${process.env.NEXT_PUBLIC_API_URL}/chat/${roomId}?page=${pageNum}`, {
headers: {
'Authorization': `${token}`,
},
});
if (!res.ok) {
throw new Error('Failed to fetch data');
}
return res.json();
};
//Component
export default function Message({messages, roomId}:{messages: ContentType, roomId: string}) {
const {
data,
fetchNextPage,
hasNextPage,
isFetching,
} = useInfiniteQuery<any, any, [string, string, string], any>({
queryKey: ['chat', 'messages', roomId],
queryFn: ({ pageParam = 0 }) => getMessageList({ roomId, pageNum: pageParam }),
initialPageParam: 0,
getNextPageParam: (lastPage: any) => (lastPage.data?.length > 0 ? lastPage.page + 1 : undefined),
staleTime: 60 * 1000,
gcTime: 300 * 1000,
});
console.log(data)
const {ref, inView} = useInView({
threshold: 0,
delay: 0,
});
useEffect(() => {
if (inView) {
!isFetching && hasNextPage && fetchNextPage();
}
}, [inView, isFetching, hasNextPage, fetchNextPage]);
return (
...
//API
export const getMessageList = async ({roomId, pageNum}: {roomId: string, pageNum: number}) => {
console.log("roomId", roomId, "pageNum", pageNum);
const token: string | undefined = Cookies.get("access_Token")
const res: Response = await instance.get(`${process.env.NEXT_PUBLIC_API_URL}/chat/${roomId}?page=${pageNum}`, {
headers: {
'Authorization': `${token}`,
},
});
if (!res.ok) {
throw new Error('Failed to fetch data');
}
return res.json();
};
//Component
export default function Message({messages, roomId}:{messages: ContentType, roomId: string}) {
const {
data,
fetchNextPage,
hasNextPage,
isFetching,
} = useInfiniteQuery<any, any, [string, string, string], any>({
queryKey: ['chat', 'messages', roomId],
queryFn: ({ pageParam = 0 }) => getMessageList({ roomId, pageNum: pageParam }),
initialPageParam: 0,
getNextPageParam: (lastPage: any) => (lastPage.data?.length > 0 ? lastPage.page + 1 : undefined),
staleTime: 60 * 1000,
gcTime: 300 * 1000,
});
console.log(data)
const {ref, inView} = useInView({
threshold: 0,
delay: 0,
});
useEffect(() => {
if (inView) {
!isFetching && hasNextPage && fetchNextPage();
}
}, [inView, isFetching, hasNextPage, fetchNextPage]);
return (
...