From 79e6ab8ba78b63feea94d4a7eb65301b064eb9de Mon Sep 17 00:00:00 2001 From: Aaron William Po Date: Sat, 6 May 2023 00:09:02 -0400 Subject: [PATCH] Update: Refactor comments section to prop drill edit and delete handlers --- .../BeerBreweryComments/CommentCardBody.tsx | 37 ++++-- .../CommentCardDropdown.tsx | 9 +- .../CommentContentBody.tsx | 11 +- .../BeerBreweryComments/EditCommentBody.tsx | 71 +++++------- .../BeerById/BeerPostCommentsSection.tsx | 30 ++++- .../BreweryById/BreweryCommentsSection.tsx | 27 +++++ src/components/ui/CommentsComponent.tsx | 14 ++- src/pages/api/brewery-comments/[id].ts | 106 ++++++++++++++++++ src/pages/beers/[id]/edit.tsx | 2 +- src/pages/beers/create.tsx | 2 +- src/pages/user/current.tsx | 2 +- .../withPageAuthRequired.ts | 0 12 files changed, 241 insertions(+), 70 deletions(-) create mode 100644 src/pages/api/brewery-comments/[id].ts rename src/{getServerSideProps => util}/withPageAuthRequired.ts (100%) diff --git a/src/components/BeerBreweryComments/CommentCardBody.tsx b/src/components/BeerBreweryComments/CommentCardBody.tsx index 7e135be..2f6c606 100644 --- a/src/components/BeerBreweryComments/CommentCardBody.tsx +++ b/src/components/BeerBreweryComments/CommentCardBody.tsx @@ -3,6 +3,7 @@ import CommentQueryResult from '@/services/types/CommentSchema/CommentQueryResul import { FC, useState } from 'react'; import { useInView } from 'react-intersection-observer'; import { z } from 'zod'; +import CreateCommentValidationSchema from '@/services/types/CommentSchema/CreateCommentValidationSchema'; import CommentContentBody from './CommentContentBody'; import EditCommentBody from './EditCommentBody'; @@ -10,20 +11,36 @@ interface CommentCardProps { comment: z.infer; mutate: ReturnType['mutate']; ref?: ReturnType['ref']; + handleDeleteRequest: (id: string) => Promise; + handleEditRequest: ( + id: string, + data: z.infer, + ) => Promise; } -const CommentCardBody: FC = ({ comment, mutate, ref }) => { +const CommentCardBody: FC = ({ + comment, + mutate, + ref, + handleDeleteRequest, + handleEditRequest, +}) => { const [inEditMode, setInEditMode] = useState(false); - return !inEditMode ? ( - - ) : ( - + return ( +
+ {!inEditMode ? ( + + ) : ( + + )} +
); }; diff --git a/src/components/BeerBreweryComments/CommentCardDropdown.tsx b/src/components/BeerBreweryComments/CommentCardDropdown.tsx index 5d7f342..fe825d3 100644 --- a/src/components/BeerBreweryComments/CommentCardDropdown.tsx +++ b/src/components/BeerBreweryComments/CommentCardDropdown.tsx @@ -36,7 +36,14 @@ const CommentCardDropdown: FC = ({ Edit ) : ( - + )} diff --git a/src/components/BeerBreweryComments/CommentContentBody.tsx b/src/components/BeerBreweryComments/CommentContentBody.tsx index 6eca6f3..d7dcc98 100644 --- a/src/components/BeerBreweryComments/CommentContentBody.tsx +++ b/src/components/BeerBreweryComments/CommentContentBody.tsx @@ -4,26 +4,21 @@ import { format } from 'date-fns'; import { Dispatch, FC, SetStateAction, useContext } from 'react'; import { Link, Rating } from 'react-daisyui'; import CommentQueryResult from '@/services/types/CommentSchema/CommentQueryResult'; -import { useInView } from 'react-intersection-observer'; + import { z } from 'zod'; import CommentCardDropdown from './CommentCardDropdown'; interface CommentContentBodyProps { comment: z.infer; - ref: ReturnType['ref'] | undefined; setInEditMode: Dispatch>; } -const CommentContentBody: FC = ({ - comment, - ref, - setInEditMode, -}) => { +const CommentContentBody: FC = ({ comment, setInEditMode }) => { const { user } = useContext(UserContext); const timeDistance = useTimeDistance(new Date(comment.createdAt)); return ( -
+

diff --git a/src/components/BeerBreweryComments/EditCommentBody.tsx b/src/components/BeerBreweryComments/EditCommentBody.tsx index 32a6727..768ff25 100644 --- a/src/components/BeerBreweryComments/EditCommentBody.tsx +++ b/src/components/BeerBreweryComments/EditCommentBody.tsx @@ -1,38 +1,44 @@ import { zodResolver } from '@hookform/resolvers/zod'; -import { FC, useState, useEffect, Dispatch, SetStateAction } from 'react'; +import { FC, useState, Dispatch, SetStateAction } from 'react'; import { Rating } from 'react-daisyui'; import { useForm, SubmitHandler } from 'react-hook-form'; import { z } from 'zod'; import useBeerPostComments from '@/hooks/data-fetching/beer-comments/useBeerPostComments'; import CommentQueryResult from '@/services/types/CommentSchema/CommentQueryResult'; -import { useInView } from 'react-intersection-observer'; import CreateCommentValidationSchema from '@/services/types/CommentSchema/CreateCommentValidationSchema'; +import useBreweryPostComments from '@/hooks/data-fetching/brewery-comments/useBreweryPostComments'; import FormError from '../ui/forms/FormError'; import FormInfo from '../ui/forms/FormInfo'; import FormLabel from '../ui/forms/FormLabel'; import FormSegment from '../ui/forms/FormSegment'; import FormTextArea from '../ui/forms/FormTextArea'; -interface CommentCardDropdownProps { +interface EditCommentBodyProps { comment: z.infer; setInEditMode: Dispatch>; - ref: ReturnType['ref'] | undefined; - mutate: ReturnType['mutate']; + + mutate: ReturnType< + typeof useBeerPostComments | typeof useBreweryPostComments + >['mutate']; + handleDeleteRequest: (id: string) => Promise; + handleEditRequest: ( + id: string, + data: z.infer, + ) => Promise; } -const EditCommentBody: FC = ({ +const EditCommentBody: FC = ({ comment, setInEditMode, - ref, + mutate, + handleDeleteRequest, + handleEditRequest, }) => { const { register, handleSubmit, formState, setValue, watch } = useForm< z.infer >({ - defaultValues: { - content: comment.content, - rating: comment.rating, - }, + defaultValues: { content: comment.content, rating: comment.rating }, resolver: zodResolver(CreateCommentValidationSchema), }); @@ -40,49 +46,24 @@ const EditCommentBody: FC = ({ const [isDeleting, setIsDeleting] = useState(false); - useEffect(() => { - return () => { - setIsDeleting(false); - }; - }, []); - - const handleDelete = async () => { + const onDelete = async () => { setIsDeleting(true); - const response = await fetch(`/api/beer-comments/${comment.id}`, { - method: 'DELETE', - }); - - if (!response.ok) { - throw new Error('Failed to delete comment'); - } - + await handleDeleteRequest(comment.id); await mutate(); }; - const onSubmit: SubmitHandler> = async ( + const onEdit: SubmitHandler> = async ( data, ) => { - const response = await fetch(`/api/beer-comments/${comment.id}`, { - method: 'PUT', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - content: data.content, - rating: data.rating, - }), - }); - - if (!response.ok) { - throw new Error('Failed to update comment'); - } - + setInEditMode(true); + await handleEditRequest(comment.id, data); await mutate(); setInEditMode(false); }; + return ( -
-
+
+
Edit your comment @@ -142,7 +123,7 @@ const EditCommentBody: FC = ({