Update: Refactor comments section to prop drill edit and delete handlers

This commit is contained in:
Aaron William Po
2023-05-06 00:09:02 -04:00
parent 2f8d7d6abb
commit 79e6ab8ba7
12 changed files with 241 additions and 70 deletions

View File

@@ -104,6 +104,31 @@ const BreweryCommentsSection: FC<BreweryBeerSectionProps> = ({ breweryPost }) =>
const commentSectionRef: MutableRefObject<HTMLDivElement | null> = useRef(null);
const handleDeleteRequest = async (commentId: string) => {
const response = await fetch(`/api/brewery-comments/${commentId}`, {
method: 'DELETE',
});
if (!response.ok) {
throw new Error(response.statusText);
}
};
const handleEditRequest = async (
commentId: string,
data: z.infer<typeof CreateCommentValidationSchema>,
) => {
const response = await fetch(`/api/brewery-comments/${commentId}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ content: data.content, rating: data.rating }),
});
if (!response.ok) {
throw new Error(response.statusText);
}
};
return (
<div className="w-full space-y-3" ref={commentSectionRef}>
<div className="card">
@@ -136,6 +161,8 @@ const BreweryCommentsSection: FC<BreweryBeerSectionProps> = ({ breweryPost }) =>
size={size}
commentSectionRef={commentSectionRef}
mutate={mutate}
handleDeleteRequest={handleDeleteRequest}
handleEditRequest={handleEditRequest}
/>
)
}