feat: add beer style likes

This commit is contained in:
Aaron William Po
2023-10-23 22:50:43 -04:00
parent c8e8207e30
commit 6bd2d4713e
11 changed files with 383 additions and 8 deletions

View File

@@ -0,0 +1,52 @@
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { z } from 'zod';
import useSWR from 'swr';
/**
* Custom hook to fetch the like count for a beer style from the server.
*
* @param beerStyleId - The ID of the beer style to fetch the like count for.
* @returns An object with the following properties:
*
* - `error`: The error that occurred while fetching the like count.
* - `isLoading`: A boolean indicating whether the like count is being fetched.
* - `mutate`: A function to mutate the like count.
* - `likeCount`: The like count for the beer style.
*/
const useGetBeerStyleLikeCount = (beerStyleId: string) => {
const { error, mutate, data, isLoading } = useSWR(
`/api/beers/styles/${beerStyleId}/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 useGetBeerStyleLikeCount;

View File

@@ -0,0 +1,57 @@
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 style by fetching
* data from the server.
*
* @param beerStyleId The ID of the beer style to check for likes.
* @returns An object with the following properties:
*
* - `error`: The error that occurred while fetching the data.
* - `isLoading`: A boolean indicating whether the data is being fetched.
* - `mutate`: A function to mutate the data.
* - `isLiked`: A boolean indicating whether the current user has liked the beer style.
*/
const useCheckIfUserLikesBeerStyle = (beerStyleId: string) => {
const { user } = useContext(UserContext);
const { data, error, isLoading, mutate } = useSWR(
`/api/beers/styles/${beerStyleId}/like/is-liked`,
async (url) => {
if (!user) {
throw new Error('User is not logged in.');
}
const response = await fetch(url);
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 useCheckIfUserLikesBeerStyle;