Diogovski
06/15/2022, 3:20 PMimport { useQuery } from '@apollo/client'
import React, { useEffect, useState } from 'react'
import InfiniteScroll from 'react-infinite-scroll-component'
import { GET_ALL_POSTS, GET_ALL_POSTS_BY_TOPICO } from '../../graphql/queries'
import { Post_Subforum } from '../../typings'
import Post from './Post'
type Props = {
topico?: string
}
function Feed({ topico }: Props) {
const [length, setLength] = useState(0);
const [hasMore, setHasMore] = useState(true);
const { data, error } = !topico ?
useQuery(GET_ALL_POSTS, {
variables: {
limit: 3,
after: length,
}
})
: useQuery(GET_ALL_POSTS_BY_TOPICO, {
variables: {
limit: 3,
after: length,
topico: topico,
}
})
const [posts, setPosts] = useState<Post_Subforum[]>([]);
console.log(length)
useEffect(() => {
setPosts(!topico ? data?.getPostList : data?.getPostListByTopico);
setHasMore(length >= data?.getPostList.length ? false : true)
})
console.log(hasMore)
const getMorePosts = () => {
setLength(length + 3);
const newPosts = [...posts, !topico ? data?.getPostList : data?.getPostListByTopico];
setPosts(newPosts);
};
return (
<div className="mt-5 space-y-4">
<InfiniteScroll
dataLength={length}
next={getMorePosts}
hasMore={hasMore}
loader={<h4>Loading...</h4>}
endMessage={
<p style={{ textAlign: "center" }}>
<b>Yay! You have seen it all</b>
</p>
}
>
{posts?.map((post) => (
<Post key={post.id} post={post} />
))}
</InfiniteScroll>
</div>
)
}
export default Feed
Needle
06/15/2022, 3:20 PM