-
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.
feature: Connect posts fully with backend
Small enhancements: - Add infinite scroll - Add visible number of comments Refs: 869768m7p Signed-off-by: Patryk Kłosiński <[email protected]>
- Loading branch information
Showing
4 changed files
with
106 additions
and
34 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 |
---|---|---|
@@ -1,39 +1,13 @@ | ||
import {Box, Title} from "@mantine/core"; | ||
import {useEffect, useState} from "react"; | ||
import api from "../shared/services/api.ts"; | ||
import {Post} from "../shared/components/Cards/Post"; | ||
import {PostDTO} from "./types/Post.tsx"; | ||
import {Box, Center} from "@mantine/core"; | ||
import {InfiniteScroll} from "./components/InfiniteScroll"; | ||
|
||
export const MainPage = () => { | ||
const [posts, setPosts] = useState<PostDTO[]>([]); | ||
|
||
useEffect(() => { | ||
api.get<{ content: PostDTO[] }>("/api/posts").then((response) => { | ||
setPosts(response.data.content); | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<Box p={"md"}> | ||
<Title order={1}>Main page</Title> | ||
|
||
{/* Post rendering */} | ||
{posts.map((post) => ( | ||
<Post | ||
key={post.id} | ||
author={post.author} | ||
content={post.content} | ||
createdAt={post.createdAt} | ||
numberOfComments={post.numberOfComments} | ||
id={post.id} | ||
photosUrls={ | ||
// generate 100 random photos | ||
Array.from({length: 100}, () => { | ||
return "https://picsum.photos/seed/" + Math.random() + "/800/2200"; | ||
}) | ||
} | ||
/> | ||
))} | ||
<Center> | ||
{/* Post rendering */} | ||
<InfiniteScroll/> | ||
</Center> | ||
</Box> | ||
); | ||
}; |
80 changes: 80 additions & 0 deletions
80
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,80 @@ | ||
import {useCallback, useEffect, useState} from "react"; | ||
import api from "../../../shared/services/api.ts"; | ||
import {Group, Loader, ScrollArea, Text} from "@mantine/core"; | ||
import {Post} from "../../../shared/components/Cards/Post"; | ||
import {PostDTO} from "../../types/Post.tsx"; | ||
|
||
export const InfiniteScroll = () => { | ||
const [posts, setPosts] = useState<PostDTO[]>([]); | ||
const [loading, setLoading] = useState(false); | ||
const [hasMore, setHasMore] = useState(true); | ||
const [page, setPage] = useState(0); | ||
|
||
const loadPosts = useCallback(async () => { | ||
if (loading || !hasMore) return; | ||
|
||
setLoading(true); | ||
|
||
try { | ||
const response = await api.get(`/api/posts`, { | ||
params: {pageNo: page, pageSize: 10}, | ||
}); | ||
|
||
const newPosts = response.data.content; | ||
|
||
setPosts((prevPosts) => [...prevPosts, ...newPosts]); | ||
setHasMore(response.data.totalPages > page + 1); | ||
setPage((prevPage) => prevPage + 1); | ||
} catch (error) { | ||
console.error('Error loading posts:', error); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}, [loading, hasMore, page]); | ||
|
||
useEffect(() => { | ||
loadPosts(); | ||
}, [loadPosts]); | ||
|
||
// Funkcja do obsługi scrolla | ||
const handleScroll = (event: { currentTarget: any; }) => { | ||
const target = event.currentTarget; | ||
if (target.scrollHeight - target.scrollTop <= target.clientHeight + 10) { | ||
loadPosts(); | ||
} | ||
}; | ||
|
||
return ( | ||
<ScrollArea | ||
onScroll={handleScroll} | ||
offsetScrollbars | ||
> | ||
{posts.map((post) => ( | ||
<Post | ||
key={post.id} | ||
author={post.author} | ||
content={post.content} | ||
createdAt={post.createdAt} | ||
numberOfComments={post.numberOfComments} | ||
id={post.id} | ||
photosUrls={ | ||
// generate 100 random photos | ||
Array.from({length: 100}, () => { | ||
return "https://picsum.photos/seed/" + Math.random() + "/800/2200"; | ||
}) | ||
} | ||
/> | ||
))} | ||
{loading && ( | ||
<Group justify={"center"} mt="lg"> | ||
<Loader/> | ||
</Group> | ||
)} | ||
{!hasMore && !loading && ( | ||
<Text ta="center" c="dimmed" mt="lg"> | ||
No more posts to load | ||
</Text> | ||
)} | ||
</ScrollArea> | ||
); | ||
} |
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