mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
Update beer like button to use custom hook to load like status
This commit is contained in:
45
hooks/useCheckIfUserLikesBeerPost.ts
Normal file
45
hooks/useCheckIfUserLikesBeerPost.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 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