mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
Refactor: change directory structure of hooks to organize by task
This commit is contained in:
48
src/hooks/data-fetching/beer-likes/useBeerPostLikeCount.ts
Normal file
48
src/hooks/data-fetching/beer-likes/useBeerPostLikeCount.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import { z } from 'zod';
|
||||
import useSWR from 'swr';
|
||||
|
||||
/**
|
||||
* Custom hook to fetch the like count for a beer post from the server.
|
||||
*
|
||||
* @param beerPostId - The ID of the beer post to fetch the like count for.
|
||||
* @returns An object with the current like count, as well as metadata about the current
|
||||
* state of the request.
|
||||
*/
|
||||
|
||||
const useGetBeerPostLikeCount = (beerPostId: string) => {
|
||||
const { error, mutate, data, isLoading } = useSWR(
|
||||
`/api/beers/${beerPostId}/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 useGetBeerPostLikeCount;
|
||||
@@ -0,0 +1,56 @@
|
||||
import UserContext from '@/contexts/userContext';
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import { useContext } from 'react';
|
||||
import useSWR from 'swr';
|
||||
import { z } from 'zod';
|
||||
|
||||
/**
|
||||
* A custom React hook that checks if the current user has liked a beer post by fetching
|
||||
* data from the server.
|
||||
*
|
||||
* @param beerPostId The ID of the beer post to check for likes.
|
||||
* @returns An object containing a boolean indicating if the user has liked the beer post,
|
||||
* an error object if an error occurred during the request, and a boolean indicating if
|
||||
* the request is currently loading.
|
||||
* @throws When the user is not logged in, the server returns an error status code, or if
|
||||
* the response data fails to validate against the expected schema.
|
||||
*/
|
||||
const useCheckIfUserLikesBeerPost = (beerPostId: string) => {
|
||||
const { user } = useContext(UserContext);
|
||||
const { data, error, isLoading, mutate } = useSWR(
|
||||
`/api/beers/${beerPostId}/like/is-liked`,
|
||||
async () => {
|
||||
if (!user) {
|
||||
throw new Error('User is not logged in.');
|
||||
}
|
||||
|
||||
const response = await fetch(`/api/beers/${beerPostId}/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 useCheckIfUserLikesBeerPost;
|
||||
Reference in New Issue
Block a user