mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
Feat: Implement infinite scrolling brewery comment section
Refactor beer comment schemas to work on brewery comments as well. Add robots.txt to block crawling for now.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import BeerCommentQueryResult from '@/services/BeerComment/schema/BeerCommentQueryResult';
|
||||
import CommentQueryResult from '@/services/types/CommentSchema/CommentQueryResult';
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import { z } from 'zod';
|
||||
import useSWRInfinite from 'swr/infinite';
|
||||
@@ -30,7 +30,7 @@ const useBeerPostComments = ({ id, pageSize }: UseBeerPostCommentsProps) => {
|
||||
if (!parsed.success) {
|
||||
throw new Error(parsed.error.message);
|
||||
}
|
||||
const parsedPayload = z.array(BeerCommentQueryResult).safeParse(parsed.data.payload);
|
||||
const parsedPayload = z.array(CommentQueryResult).safeParse(parsed.data.payload);
|
||||
|
||||
if (!parsedPayload.success) {
|
||||
throw new Error(parsedPayload.error.message);
|
||||
|
||||
71
src/hooks/useBreweryPostComments.ts
Normal file
71
src/hooks/useBreweryPostComments.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import CommentQueryResult from '@/services/types/CommentSchema/CommentQueryResult';
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import { z } from 'zod';
|
||||
import useSWRInfinite from 'swr/infinite';
|
||||
|
||||
interface UseBreweryPostCommentsProps {
|
||||
id: string;
|
||||
pageSize: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* A custom React hook that fetches comments for a specific brewery post.
|
||||
*
|
||||
* @param props - The props object.
|
||||
* @param props.pageNum - The page number of the comments to fetch.
|
||||
* @param props.id - The ID of the brewery post to fetch comments for.
|
||||
* @param props.pageSize - The number of comments to fetch per page.
|
||||
* @returns An object containing the fetched comments, the total number of comment pages,
|
||||
* a boolean indicating if the request is currently loading, and a function to mutate
|
||||
* the data.
|
||||
*/
|
||||
const useBreweryPostComments = ({ id, pageSize }: UseBreweryPostCommentsProps) => {
|
||||
const fetcher = async (url: string) => {
|
||||
const response = await fetch(url);
|
||||
const json = await response.json();
|
||||
const count = response.headers.get('X-Total-Count');
|
||||
const parsed = APIResponseValidationSchema.safeParse(json);
|
||||
|
||||
if (!parsed.success) {
|
||||
throw new Error(parsed.error.message);
|
||||
}
|
||||
const parsedPayload = z.array(CommentQueryResult).safeParse(parsed.data.payload);
|
||||
|
||||
if (!parsedPayload.success) {
|
||||
throw new Error(parsedPayload.error.message);
|
||||
}
|
||||
|
||||
const pageCount = Math.ceil(parseInt(count as string, 10) / pageSize);
|
||||
|
||||
return { comments: parsedPayload.data, pageCount };
|
||||
};
|
||||
|
||||
const { data, error, isLoading, mutate, size, setSize } = useSWRInfinite(
|
||||
(index) =>
|
||||
`/api/breweries/${id}/comments?page_num=${index + 1}&page_size=${pageSize}`,
|
||||
fetcher,
|
||||
{ parallel: true },
|
||||
);
|
||||
|
||||
const comments = data?.flatMap((d) => d.comments) ?? [];
|
||||
const pageCount = data?.[0].pageCount ?? 0;
|
||||
|
||||
const isLoadingMore =
|
||||
isLoading || (size > 0 && data && typeof data[size - 1] === 'undefined');
|
||||
|
||||
const isAtEnd = !(size < data?.[0].pageCount!);
|
||||
|
||||
return {
|
||||
comments,
|
||||
isLoading,
|
||||
error: error as undefined,
|
||||
mutate,
|
||||
size,
|
||||
setSize,
|
||||
isLoadingMore,
|
||||
isAtEnd,
|
||||
pageCount,
|
||||
};
|
||||
};
|
||||
|
||||
export default useBreweryPostComments;
|
||||
Reference in New Issue
Block a user