Files
the-biergarten-app/components/BeerById/BeerPostCommentsSection.tsx
Aaron William Po b69dbc95b4 Work on brewery page, refactors
Refactor query types to explicitly use z.infer
2023-03-31 21:13:35 -04:00

66 lines
2.1 KiB
TypeScript

import UserContext from '@/contexts/userContext';
import BeerCommentQueryResult from '@/services/BeerComment/schema/BeerCommentQueryResult';
import beerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult';
import { useRouter } from 'next/router';
import { FC, useContext } from 'react';
import { z } from 'zod';
import BeerCommentForm from './BeerCommentForm';
import BeerCommentsPaginationBar from './BeerPostCommentsPaginationBar';
import CommentCard from './CommentCard';
interface BeerPostCommentsSectionProps {
beerPost: z.infer<typeof beerPostQueryResult>;
comments: z.infer<typeof BeerCommentQueryResult>[];
commentsPageCount: number;
}
const BeerPostCommentsSection: FC<BeerPostCommentsSectionProps> = ({
beerPost,
comments,
commentsPageCount,
}) => {
const { user } = useContext(UserContext);
const router = useRouter();
const commentsPageNum = parseInt(router.query.comments_page as string, 10) || 1;
return (
<div className="w-full space-y-3 md:w-[60%]">
<div className="card h-96 bg-base-300">
<div className="card-body h-full">
{user ? (
<BeerCommentForm beerPost={beerPost} />
) : (
<div className="flex h-full flex-col items-center justify-center">
<span className="text-lg font-bold">Log in to leave a comment.</span>
</div>
)}
</div>
</div>
{comments.length ? (
<div className="card bg-base-300 pb-6">
{comments.map((comment) => (
<CommentCard key={comment.id} comment={comment} beerPostId={beerPost.id} />
))}
<BeerCommentsPaginationBar
commentsPageNum={commentsPageNum}
commentsPageCount={commentsPageCount}
beerPost={beerPost}
/>
</div>
) : (
<div className="card items-center bg-base-300">
<div className="card-body">
<span className="text-lg font-bold">No comments yet.</span>
</div>
</div>
)}
</div>
);
};
export default BeerPostCommentsSection;