import UserContext from '@/contexts/UserContext'; import useBeerPostsByUser from '@/hooks/data-fetching/beer-posts/useBeerPostsByUser'; import { FC, useContext, MutableRefObject, useRef } from 'react'; import { FaArrowUp } from 'react-icons/fa'; import { useInView } from 'react-intersection-observer'; import BeerCard from '../BeerIndex/BeerCard'; import LoadingCard from '../ui/LoadingCard'; import Spinner from '../ui/Spinner'; const BeerPostsByUser: FC = () => { const { user } = useContext(UserContext); const pageRef: MutableRefObject = useRef(null); const PAGE_SIZE = 2; const { beerPosts, setSize, size, isLoading, isLoadingMore, isAtEnd } = useBeerPostsByUser({ pageSize: PAGE_SIZE, userId: user!.id }); const { ref: lastBeerPostRef } = useInView({ onChange: (visible) => { if (!visible || isAtEnd) return; setSize(size + 1); }, }); return (
{!!beerPosts.length && !isLoading && ( <> {beerPosts.map((beerPost, i) => { return (
); })} )} {isLoadingMore && ( <> {Array.from({ length: PAGE_SIZE }, (_, i) => ( ))} )}
{(isLoading || isLoadingMore) && (
)} {!!beerPosts.length && isAtEnd && !isLoading && (
)} {!beerPosts.length && !isLoading && (

No posts yet.

)}
); }; export default BeerPostsByUser;