mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
Continue work on brewery page, implement like system
This commit is contained in:
@@ -10,7 +10,7 @@ import useSWR from 'swr';
|
||||
* state of the request.
|
||||
*/
|
||||
|
||||
const useGetLikeCount = (beerPostId: string) => {
|
||||
const useGetBeerPostLikeCount = (beerPostId: string) => {
|
||||
const { error, mutate, data, isLoading } = useSWR(
|
||||
`/api/beers/${beerPostId}/like`,
|
||||
async (url) => {
|
||||
@@ -45,4 +45,4 @@ const useGetLikeCount = (beerPostId: string) => {
|
||||
};
|
||||
};
|
||||
|
||||
export default useGetLikeCount;
|
||||
export default useGetBeerPostLikeCount;
|
||||
62
src/hooks/useBreweryPosts.ts
Normal file
62
src/hooks/useBreweryPosts.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import BreweryPostQueryResult from '@/services/BreweryPost/types/BreweryPostQueryResult';
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import useSWRInfinite from 'swr/infinite';
|
||||
import { z } from 'zod';
|
||||
|
||||
/**
|
||||
* A custom hook using SWR to fetch brewery posts from the API.
|
||||
*
|
||||
* @param options The options to use when fetching brewery posts.
|
||||
* @param options.pageSize The number of brewery posts to fetch per page.
|
||||
* @returns An object containing the brewery posts, page count, and loading state.
|
||||
*/
|
||||
const useBreweryPosts = ({ pageSize }: { pageSize: number }) => {
|
||||
const fetcher = async (url: string) => {
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) {
|
||||
throw new Error(response.statusText);
|
||||
}
|
||||
|
||||
const json = await response.json();
|
||||
const count = response.headers.get('X-Total-Count');
|
||||
|
||||
const parsed = APIResponseValidationSchema.safeParse(json);
|
||||
if (!parsed.success) {
|
||||
throw new Error('API response validation failed');
|
||||
}
|
||||
|
||||
const parsedPayload = z.array(BreweryPostQueryResult).safeParse(parsed.data.payload);
|
||||
if (!parsedPayload.success) {
|
||||
throw new Error('API response validation failed');
|
||||
}
|
||||
|
||||
const pageCount = Math.ceil(parseInt(count as string, 10) / pageSize);
|
||||
return {
|
||||
breweryPosts: parsedPayload.data,
|
||||
pageCount,
|
||||
};
|
||||
};
|
||||
|
||||
const { data, error, isLoading, setSize, size } = useSWRInfinite(
|
||||
(index) => `/api/breweries?pageNum=${index + 1}&pageSize=${pageSize}`,
|
||||
fetcher,
|
||||
);
|
||||
|
||||
const breweryPosts = data?.flatMap((d) => d.breweryPosts) ?? [];
|
||||
const pageCount = data?.[0].pageCount ?? 0;
|
||||
const isLoadingMore = size > 0 && data && typeof data[size - 1] === 'undefined';
|
||||
const isAtEnd = !(size < data?.[0].pageCount!);
|
||||
|
||||
return {
|
||||
breweryPosts,
|
||||
pageCount,
|
||||
size,
|
||||
setSize,
|
||||
isLoading,
|
||||
isLoadingMore,
|
||||
isAtEnd,
|
||||
error: error as unknown,
|
||||
};
|
||||
};
|
||||
|
||||
export default useBreweryPosts;
|
||||
45
src/hooks/useCheckIfUserLikesBreweryPost.ts
Normal file
45
src/hooks/useCheckIfUserLikesBreweryPost.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import UserContext from '@/contexts/userContext';
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import { useContext } from 'react';
|
||||
import useSWR from 'swr';
|
||||
import { z } from 'zod';
|
||||
|
||||
const useCheckIfUserLikesBreweryPost = (breweryPostId: string) => {
|
||||
const { user } = useContext(UserContext);
|
||||
const { data, error, isLoading, mutate } = useSWR(
|
||||
`/api/breweries/${breweryPostId}/like/is-liked`,
|
||||
async () => {
|
||||
if (!user) {
|
||||
throw new Error('User is not logged in.');
|
||||
}
|
||||
|
||||
const response = await fetch(`/api/breweries/${breweryPostId}/like/is-liked`);
|
||||
const json = await response.json();
|
||||
const parsed = APIResponseValidationSchema.safeParse(json);
|
||||
|
||||
if (!parsed.success) {
|
||||
throw new Error('Invalid API response.');
|
||||
}
|
||||
|
||||
const { payload } = parsed.data;
|
||||
const parsedPayload = z.object({ isLiked: z.boolean() }).safeParse(payload);
|
||||
|
||||
if (!parsedPayload.success) {
|
||||
throw new Error('Invalid API response.');
|
||||
}
|
||||
|
||||
const { isLiked } = parsedPayload.data;
|
||||
|
||||
return isLiked;
|
||||
},
|
||||
);
|
||||
|
||||
return {
|
||||
isLiked: data,
|
||||
error: error as unknown,
|
||||
isLoading,
|
||||
mutate,
|
||||
};
|
||||
};
|
||||
|
||||
export default useCheckIfUserLikesBreweryPost;
|
||||
40
src/hooks/useGetBreweryPostLikeCount.ts
Normal file
40
src/hooks/useGetBreweryPostLikeCount.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import useSWR from 'swr';
|
||||
import { z } from 'zod';
|
||||
|
||||
const useGetBreweryPostLikeCount = (breweryPostId: string) => {
|
||||
const { error, mutate, data, isLoading } = useSWR(
|
||||
`/api/breweries/${breweryPostId}/like`,
|
||||
async (url) => {
|
||||
const response = await fetch(url);
|
||||
const json = await response.json();
|
||||
|
||||
const parsed = APIResponseValidationSchema.safeParse(json);
|
||||
|
||||
if (!parsed.success) {
|
||||
throw new Error('Failed to parse API response');
|
||||
}
|
||||
|
||||
const parsedPayload = z
|
||||
.object({
|
||||
likeCount: z.number(),
|
||||
})
|
||||
.safeParse(parsed.data.payload);
|
||||
|
||||
if (!parsedPayload.success) {
|
||||
throw new Error('Failed to parse API response payload');
|
||||
}
|
||||
|
||||
return parsedPayload.data.likeCount;
|
||||
},
|
||||
);
|
||||
|
||||
return {
|
||||
error: error as unknown,
|
||||
isLoading,
|
||||
mutate,
|
||||
likeCount: data as number | undefined,
|
||||
};
|
||||
};
|
||||
|
||||
export default useGetBreweryPostLikeCount;
|
||||
Reference in New Issue
Block a user