Infinite scrolling setup
This commit is contained in:
parent
3bb21f67e9
commit
c36e4a7f7d
@ -1,17 +1,13 @@
|
|||||||
import { ImageIcon } from "@radix-ui/react-icons";
|
import { ImageIcon } from "@radix-ui/react-icons";
|
||||||
import { Box, Card, Heading } from "@radix-ui/themes";
|
import { Box, Card, Heading } from "@radix-ui/themes";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import { GetRandomPostsRow } from "../../../@types/PostTypes";
|
import { Post } from "../../../@types/PostTypes";
|
||||||
|
|
||||||
type TPostCard = {
|
export default function PostCard(props: Post) {
|
||||||
post: GetRandomPostsRow;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default function PostCard({ post }: TPostCard) {
|
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
|
|
||||||
const clickHandler = () => {
|
const clickHandler = () => {
|
||||||
navigate(`/posts/${post.post_id.toString()}`)
|
navigate(`/posts/${props.post_id.toString()}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -22,7 +18,7 @@ export default function PostCard({ post }: TPostCard) {
|
|||||||
</Box>
|
</Box>
|
||||||
|
|
||||||
<Box className="px-4 pt-2">
|
<Box className="px-4 pt-2">
|
||||||
<Heading>{post.title}</Heading>
|
<Heading>{props.title}</Heading>
|
||||||
</Box>
|
</Box>
|
||||||
</Box>
|
</Box>
|
||||||
</Card>
|
</Card>
|
||||||
|
|||||||
@ -1,10 +1,10 @@
|
|||||||
import {
|
import {
|
||||||
Box,
|
Box,
|
||||||
Container,
|
|
||||||
Flex,
|
Flex,
|
||||||
Heading,
|
Heading,
|
||||||
ScrollArea,
|
ScrollArea,
|
||||||
Separator,
|
Separator,
|
||||||
|
Text,
|
||||||
} from "@radix-ui/themes";
|
} from "@radix-ui/themes";
|
||||||
import { useInfiniteQuery } from "@tanstack/react-query";
|
import { useInfiniteQuery } from "@tanstack/react-query";
|
||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
@ -14,7 +14,7 @@ import { SelectedPostsResponse } from "../../@types/PostTypes";
|
|||||||
import { axiosLocalhost } from "../../api/axios/axios";
|
import { axiosLocalhost } from "../../api/axios/axios";
|
||||||
import PostCard from "./PostCard/PostCard";
|
import PostCard from "./PostCard/PostCard";
|
||||||
|
|
||||||
const LIMIT = 3;
|
const LIMIT = 5;
|
||||||
|
|
||||||
export default function RandomPostsPage() {
|
export default function RandomPostsPage() {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
@ -37,33 +37,15 @@ export default function RandomPostsPage() {
|
|||||||
lastPage.next_page_index < 0 ? undefined : lastPage.next_page_index,
|
lastPage.next_page_index < 0 ? undefined : lastPage.next_page_index,
|
||||||
});
|
});
|
||||||
|
|
||||||
// const { data, refetch } = useQuery({
|
|
||||||
// queryKey: ["random_posts_key"],
|
|
||||||
// queryFn: async () => {
|
|
||||||
// try {
|
|
||||||
// const response = await axiosLocalhost.get(
|
|
||||||
// `/posts/random?limit=${LIMIT}&offset=0`
|
|
||||||
// );
|
|
||||||
|
|
||||||
// return response.data as GetRandomPostsRow[];
|
|
||||||
// } catch (error) {
|
|
||||||
// console.log(`Something went wrong`);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return [];
|
|
||||||
// },
|
|
||||||
// });
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (inView) {
|
if (inView) {
|
||||||
console.log(`Loading more posts...`);
|
|
||||||
if (hasNextPage) fetchNextPage();
|
if (hasNextPage) fetchNextPage();
|
||||||
}
|
}
|
||||||
}, [inView]);
|
}, [inView]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Flex direction={"column"} className="mx-auto overflow-hidden">
|
<Flex direction={"column"} className="w-full mx-auto overflow-hidden max-w-pc-width ">
|
||||||
<Heading size={"9"} weight={"regular"} className="text-center">
|
<Heading size={"9"} weight={"regular"} className="text-center">
|
||||||
{t("discover")}
|
{t("discover")}
|
||||||
</Heading>
|
</Heading>
|
||||||
@ -72,18 +54,31 @@ export default function RandomPostsPage() {
|
|||||||
<ScrollArea>
|
<ScrollArea>
|
||||||
{data?.pages.map((post, i) => {
|
{data?.pages.map((post, i) => {
|
||||||
return (
|
return (
|
||||||
<>
|
<div key={i}>
|
||||||
{post.selected_posts.map((post) => {
|
{post.selected_posts.map((post, i) => {
|
||||||
return (
|
return (
|
||||||
<Container size={"3"} key={`post${i}`}>
|
<div key={`${i}${post.post_id}`}>
|
||||||
<PostCard post={post} />
|
<PostCard {...post} />
|
||||||
</Container>
|
</div>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
</>
|
</div>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
<Box ref={ref}>qqqqqqqqqqqq</Box>
|
<Box ref={ref} className="w-full mb-4 text-center">
|
||||||
|
{isFetching ? (
|
||||||
|
<Text>Loading more...</Text>
|
||||||
|
) : hasNextPage ? (
|
||||||
|
<Text
|
||||||
|
className="cursor-pointer"
|
||||||
|
onClick={() => fetchNextPage()}
|
||||||
|
>
|
||||||
|
Load more posts
|
||||||
|
</Text>
|
||||||
|
) : (
|
||||||
|
<Text>No more posts to load</Text>
|
||||||
|
)}
|
||||||
|
</Box>
|
||||||
</ScrollArea>
|
</ScrollArea>
|
||||||
</Flex>
|
</Flex>
|
||||||
</>
|
</>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user