diff --git a/code/frontend/src/components/Post.tsx b/code/frontend/src/components/Post.tsx index 9e52b7e..a7736f2 100644 --- a/code/frontend/src/components/Post.tsx +++ b/code/frontend/src/components/Post.tsx @@ -73,7 +73,7 @@ export default function Post({ postId }: PostProps) { if (!post) { return ( - + Loading... diff --git a/code/frontend/src/components/feed/Feed.tsx b/code/frontend/src/components/feed/Feed.tsx index a6f0ee5..f94a3f9 100644 --- a/code/frontend/src/components/feed/Feed.tsx +++ b/code/frontend/src/components/feed/Feed.tsx @@ -2,6 +2,7 @@ import React, { useState, useEffect, useRef } from "react"; import Post from "../Post"; import "./feed.css"; import api from "../../api/axios"; +import { create } from "axios"; interface PostListItem { id: string; @@ -21,15 +22,15 @@ function Feed() { if (loading || !hasMore) return; setLoading(true); try { - const params: any = { limit: PAGE_SIZE }; - if (nextCursor) params.createdAt = nextCursor; + let url = `/feed?limit=${PAGE_SIZE}`; + if (nextCursor) { + url = `/feed?createdAt=${encodeURIComponent(nextCursor)}&limit=${PAGE_SIZE}`; + } interface FeedResponse { - posts: PostListItem[]; - + posts: PostListItem[]; nextCursor: string | null; } - const response = await api.get("/feed?limit=10"); - //const response = await api.get("http://localhost:3001/api/feed?limit=10"); + const response = await api.get(url); console.log("Feed response:", response.data); const { posts: newPosts, nextCursor: newCursor } = response.data; setPosts((prev) => [...prev, ...newPosts]); @@ -48,16 +49,17 @@ function Feed() { useEffect(() => { const onScroll = () => { - const feed = feedRef.current; - if (!feed || loading || !hasMore) return; - if (feed.scrollTop + feed.clientHeight >= feed.scrollHeight - 100) { + if (loading || !hasMore) return; + if ( + window.innerHeight + window.scrollY >= + document.body.offsetHeight - 100 + ) { fetchPosts(); } }; - const feed = feedRef.current; - feed?.addEventListener("scroll", onScroll); + window.addEventListener("scroll", onScroll); return () => { - feed?.removeEventListener("scroll", onScroll); + window.removeEventListener("scroll", onScroll); }; }, [loading, hasMore, nextCursor]); diff --git a/code/frontend/src/components/feed/feed.css b/code/frontend/src/components/feed/feed.css index ed98bfc..1f18d11 100644 --- a/code/frontend/src/components/feed/feed.css +++ b/code/frontend/src/components/feed/feed.css @@ -13,6 +13,7 @@ flex-wrap: wrap; justify-content: center; gap: 1rem; + height: 80vh; } .loading { width: 100%;