import UserContext from '@/contexts/UserContext'; import BreweryPostQueryResult from '@/services/posts/brewery-post/schema/BreweryPostQueryResult'; import { FC, MutableRefObject, useContext, useRef } from 'react'; import { z } from 'zod'; import useBreweryPostComments from '@/hooks/data-fetching/brewery-comments/useBreweryPostComments'; import { sendDeleteBreweryPostCommentRequest, sendEditBreweryPostCommentRequest, } from '@/requests/comments/brewery-comment'; import CommentLoadingComponent from '../Comments/CommentLoadingComponent'; import CommentsComponent from '../Comments/CommentsComponent'; import BreweryCommentForm from './BreweryCommentForm'; interface BreweryBeerSectionProps { breweryPost: z.infer; } const BreweryCommentsSection: FC = ({ breweryPost }) => { const { user } = useContext(UserContext); const PAGE_SIZE = 4; const { isLoading, setSize, size, isLoadingMore, isAtEnd, mutate, comments: breweryComments, } = useBreweryPostComments({ id: breweryPost.id, 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 sendDeleteBreweryPostCommentRequest({ breweryPostId: breweryPost.id, commentId: id, }); }} handleEditCommentRequest={(commentId, data) => { return sendEditBreweryPostCommentRequest({ breweryPostId: breweryPost.id, commentId, body: { content: data.content, rating: data.rating }, }); }} /> ) }
); }; export default BreweryCommentsSection;