import UserContext from '@/contexts/UserContext'; import BeerPostQueryResult from '@/services/posts/beer-post/schema/BeerPostQueryResult'; import { FC, MutableRefObject, useContext, useRef } from 'react'; import { z } from 'zod'; import useBeerPostComments from '@/hooks/data-fetching/beer-comments/useBeerPostComments'; import { useRouter } from 'next/router'; import { deleteBeerPostCommentRequest, editBeerPostCommentRequest, } from '@/requests/comments/beer-comment'; import BeerCommentForm from './BeerCommentForm'; import CommentLoadingComponent from '../Comments/CommentLoadingComponent'; import CommentsComponent from '../Comments/CommentsComponent'; interface BeerPostCommentsSectionProps { beerPost: z.infer; } const BeerPostCommentsSection: FC = ({ beerPost }) => { const { user } = useContext(UserContext); const router = useRouter(); const pageNum = parseInt(router.query.comments_page as string, 10) || 1; const PAGE_SIZE = 15; const { comments, isLoading, mutate, setSize, size, isLoadingMore, isAtEnd } = useBeerPostComments({ id: beerPost.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 ? (
) : ( { return deleteBeerPostCommentRequest({ commentId: id, beerPostId: beerPost.id, }); }} handleEditCommentRequest={(id, data) => { return editBeerPostCommentRequest({ body: data, commentId: id, beerPostId: beerPost.id, }); }} /> ) }
); }; export default BeerPostCommentsSection;