-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from JimTheCat/CU-869768m7p_Connect-PostsMenu-…
…with-backend_Patryk-Kosiski feature: Connect posts fully with backend
- Loading branch information
Showing
12 changed files
with
184 additions
and
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,64 @@ | ||
import {Box, Title} from "@mantine/core"; | ||
import {useEffect, useState} from "react"; | ||
import {PostDTO} from "./types/Post.tsx"; | ||
import api from "../shared/services/api.ts"; | ||
import {Box, Stack} from "@mantine/core"; | ||
import {InfiniteScroll} from "./components/InfiniteScroll"; | ||
import {Post} from "../shared/components/Cards/Post"; | ||
import {PostDTO} from "./types/Post.tsx"; | ||
|
||
export const MainPage = () => { | ||
const [posts, setPosts] = useState<PostDTO[]>([]); | ||
const [page, setPage] = useState(0); | ||
const [hasMore, setHasMore] = useState(true); | ||
const [loading, setLoading] = useState(false); | ||
|
||
const loadMorePosts = async () => { | ||
if (loading || !hasMore) return; | ||
|
||
const [posts, setPosts] = useState<PostDTO[] | []>([]); | ||
setLoading(true); | ||
try { | ||
const response = await api.get(`/api/posts`, { | ||
params: {pageNo: page, pageSize: 3}, | ||
}); | ||
|
||
const data = response.data; | ||
|
||
setPosts((prevPosts) => [...prevPosts, ...data.content]); | ||
setHasMore(data.totalPages > page); | ||
setPage((prevPage) => prevPage + 1); | ||
} catch (error) { | ||
console.error("Error loading posts:", error); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
api.get<PostDTO[]>("/api/posts/get-all").then((response) => { | ||
setPosts(response.data); | ||
}); | ||
// Initial load | ||
loadMorePosts(); | ||
}, []); | ||
|
||
return ( | ||
<Box p={"md"}> | ||
<Title order={1}>Main page</Title> | ||
|
||
{/*Posts*/} | ||
{posts.map((post) => ( | ||
<Post key={post.ownerLogin} ownerLogin={post.ownerLogin} contentHtml={post.content} createdAt={post.createdAt} | ||
photosUrls={ | ||
// generate 100 random photos | ||
Array.from({length: 100}, () => { | ||
return "https://picsum.photos/seed/" + Math.random() + "/800/2200"; | ||
}) | ||
} | ||
/> | ||
))} | ||
</Box> | ||
) | ||
} | ||
return ( | ||
<Box p="lg"> | ||
<InfiniteScroll loadMore={loadMorePosts} hasMore={hasMore} loading={loading}> | ||
<Stack align={"center"} gap="lg"> | ||
{posts.map((post) => ( | ||
<Post | ||
key={post.id} | ||
id={post.id} | ||
author={post.author} | ||
content={post.content} | ||
createdAt={post.createdAt} | ||
numberOfComments={post.numberOfComments} | ||
photosUrls={ | ||
// generate 100 random photos | ||
Array.from({length: 100}, () => { | ||
return "https://picsum.photos/seed/" + Math.random() + "/800/2200"; | ||
}) | ||
} | ||
/> | ||
))} | ||
</Stack> | ||
</InfiniteScroll> | ||
</Box> | ||
); | ||
}; |
65 changes: 65 additions & 0 deletions
65
frontend/src/Features/MainPage/components/InfiniteScroll/InfiniteScroll.tsx
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,65 @@ | ||
import React, {useEffect, useRef} from "react"; | ||
import {Box, Loader, Text} from "@mantine/core"; | ||
|
||
interface InfiniteScrollProps { | ||
loadMore: () => Promise<void>; | ||
hasMore: boolean; | ||
loading: boolean; | ||
children: React.ReactNode; | ||
} | ||
|
||
export const InfiniteScroll: React.FC<InfiniteScrollProps> = ({ | ||
loadMore, | ||
hasMore, | ||
loading, | ||
children, | ||
}) => { | ||
const sentinelRef = useRef<HTMLDivElement | null>(null); | ||
const observer = useRef<IntersectionObserver | null>(null); | ||
|
||
useEffect(() => { | ||
// IntersectionObserver callback | ||
const handleIntersection: IntersectionObserverCallback = (entries) => { | ||
const [entry] = entries; | ||
if (entry.isIntersecting && hasMore && !loading) { | ||
loadMore(); | ||
} | ||
}; | ||
|
||
if (sentinelRef.current) { | ||
observer.current = new IntersectionObserver(handleIntersection, { | ||
root: null, | ||
rootMargin: "100px", // Load earlier when close to the bottom | ||
threshold: 0.1, // Trigger when sentinel is at least 10% visible | ||
}); | ||
|
||
observer.current.observe(sentinelRef.current); | ||
} | ||
|
||
return () => { | ||
if (observer.current && sentinelRef.current) { | ||
observer.current.unobserve(sentinelRef.current); | ||
} | ||
}; | ||
}, [hasMore, loading, loadMore]); | ||
|
||
return ( | ||
<Box style={{position: "relative"}}> | ||
{children} | ||
{/* Sentinel element */} | ||
<div ref={sentinelRef} style={{height: 1, visibility: "hidden"}}/> | ||
{/* Loading indicator */} | ||
{loading && ( | ||
<Box style={{textAlign: "center", margin: "20px 0"}}> | ||
<Loader size="md"/> | ||
</Box> | ||
)} | ||
{/* End of data */} | ||
{!hasMore && !loading && ( | ||
<Text ta="center" c="dimmed" mt="lg"> | ||
No more posts ಥ_ಥ | ||
</Text> | ||
)} | ||
</Box> | ||
); | ||
}; |
1 change: 1 addition & 0 deletions
1
frontend/src/Features/MainPage/components/InfiniteScroll/index.tsx
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 {InfiniteScroll} from './InfiniteScroll'; |
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,14 @@ | ||
export type PostDTO = { | ||
content: string; | ||
ownerLogin: string; | ||
createdAt: string; | ||
} | ||
id: string, | ||
content: string, | ||
createdAt: string, | ||
numberOfComments: number, | ||
author: { | ||
id: string; | ||
name: string; | ||
surname: string; | ||
login: string; | ||
profilePicture: string | null; | ||
}, | ||
photosUrls?: string[] | ||
}; |
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
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
4 changes: 2 additions & 2 deletions
4
frontend/src/Features/shared/components/Cards/Post/components/RemovePost/RemovePost.tsx
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
Oops, something went wrong.