import UserContext from '@/contexts/userContext'; import beerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult'; import { FC, MutableRefObject, useContext, useRef } from 'react'; import { z } from 'zod'; import useBeerPostComments from '@/hooks/useBeerPostComments'; import { useRouter } from 'next/router'; import BeerCommentForm from './BeerCommentForm'; import LoadingComponent from './LoadingComponent'; import CommentsComponent from '../ui/CommentsComponent'; interface BeerPostCommentsSectionProps { beerPost: z.infer; } const BeerPostCommentsSection: FC = ({ beerPost }) => { const { user } = useContext(UserContext); const router = useRouter(); const { id } = beerPost; const pageNum = parseInt(router.query.comments_page as string, 10) || 1; const PAGE_SIZE = 4; const { comments, isLoading, mutate, setSize, size, isLoadingMore, isAtEnd } = useBeerPostComments({ id, pageNum, pageSize: PAGE_SIZE, }); const commentSectionRef: MutableRefObject = useRef(null); return (
{user ? ( ) : (
Log in to leave a comment.
)}
{ /** * If the comments are loading, show a loading component. Otherwise, show the * comments. */ isLoading ? (
) : ( ) }
); }; export default BeerPostCommentsSection;