Refactor: update comment components

This commit is contained in:
Aaron William Po
2023-12-25 21:04:15 -05:00
parent 623855682b
commit 2b90bb2e5d
20 changed files with 331 additions and 189 deletions

View File

@@ -10,7 +10,7 @@ import CreateCommentValidationSchema from '@/services/schema/CommentSchema/Creat
import toast from 'react-hot-toast'; import toast from 'react-hot-toast';
import createErrorToast from '@/util/createErrorToast'; import createErrorToast from '@/util/createErrorToast';
import { sendCreateBeerCommentRequest } from '@/requests/comments/beer-comment'; import { sendCreateBeerCommentRequest } from '@/requests/comments/beer-comment';
import CommentForm from '../ui/CommentForm'; import CommentForm from '../Comments/CommentForm';
interface BeerCommentFormProps { interface BeerCommentFormProps {
beerPost: z.infer<typeof BeerPostQueryResult>; beerPost: z.infer<typeof BeerPostQueryResult>;

View File

@@ -6,15 +6,15 @@ import { FC, MutableRefObject, useContext, useRef } from 'react';
import { z } from 'zod'; import { z } from 'zod';
import useBeerPostComments from '@/hooks/data-fetching/beer-comments/useBeerPostComments'; import useBeerPostComments from '@/hooks/data-fetching/beer-comments/useBeerPostComments';
import { useRouter } from 'next/router'; import { useRouter } from 'next/router';
import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema';
import { import {
deleteBeerPostCommentRequest, deleteBeerPostCommentRequest,
editBeerPostCommentRequest, editBeerPostCommentRequest,
} from '@/requests/comments/beer-comment'; } from '@/requests/comments/beer-comment';
import BeerCommentForm from './BeerCommentForm'; import BeerCommentForm from './BeerCommentForm';
import LoadingComponent from './LoadingComponent'; import CommentLoadingComponent from '../Comments/CommentLoadingComponent';
import CommentsComponent from '../ui/CommentsComponent'; import CommentsComponent from '../Comments/CommentsComponent';
interface BeerPostCommentsSectionProps { interface BeerPostCommentsSectionProps {
beerPost: z.infer<typeof BeerPostQueryResult>; beerPost: z.infer<typeof BeerPostQueryResult>;
@@ -32,21 +32,6 @@ const BeerPostCommentsSection: FC<BeerPostCommentsSectionProps> = ({ beerPost })
const commentSectionRef: MutableRefObject<HTMLDivElement | null> = useRef(null); const commentSectionRef: MutableRefObject<HTMLDivElement | null> = useRef(null);
const handleDeleteRequest = async (id: string) => {
await deleteBeerPostCommentRequest({ commentId: id, beerPostId: beerPost.id });
};
const handleEditRequest = async (
id: string,
data: z.infer<typeof CreateCommentValidationSchema>,
) => {
await editBeerPostCommentRequest({
body: data,
commentId: id,
beerPostId: beerPost.id,
});
};
return ( return (
<div className="w-full space-y-3" ref={commentSectionRef}> <div className="w-full space-y-3" ref={commentSectionRef}>
<div className="card bg-base-300"> <div className="card bg-base-300">
@@ -68,7 +53,7 @@ const BeerPostCommentsSection: FC<BeerPostCommentsSectionProps> = ({ beerPost })
*/ */
isLoading ? ( isLoading ? (
<div className="card bg-base-300 pb-6"> <div className="card bg-base-300 pb-6">
<LoadingComponent length={PAGE_SIZE} /> <CommentLoadingComponent length={PAGE_SIZE} />
</div> </div>
) : ( ) : (
<CommentsComponent <CommentsComponent
@@ -80,8 +65,19 @@ const BeerPostCommentsSection: FC<BeerPostCommentsSectionProps> = ({ beerPost })
setSize={setSize} setSize={setSize}
size={size} size={size}
mutate={mutate} mutate={mutate}
handleDeleteRequest={handleDeleteRequest} handleDeleteCommentRequest={(id) => {
handleEditRequest={handleEditRequest} return deleteBeerPostCommentRequest({
commentId: id,
beerPostId: beerPost.id,
});
}}
handleEditCommentRequest={(id, data) => {
return editBeerPostCommentRequest({
body: data,
commentId: id,
beerPostId: beerPost.id,
});
}}
/> />
) )
} }

View File

@@ -11,7 +11,7 @@ import createErrorToast from '@/util/createErrorToast';
import BeerStyleQueryResult from '@/services/posts/beer-style-post/schema/BeerStyleQueryResult'; import BeerStyleQueryResult from '@/services/posts/beer-style-post/schema/BeerStyleQueryResult';
import useBeerStyleComments from '@/hooks/data-fetching/beer-style-comments/useBeerStyleComments'; import useBeerStyleComments from '@/hooks/data-fetching/beer-style-comments/useBeerStyleComments';
import { sendCreateBeerStyleCommentRequest } from '@/requests/comments/beer-style-comment'; import { sendCreateBeerStyleCommentRequest } from '@/requests/comments/beer-style-comment';
import CommentForm from '../ui/CommentForm'; import CommentForm from '../Comments/CommentForm';
interface BeerCommentFormProps { interface BeerCommentFormProps {
beerStyle: z.infer<typeof BeerStyleQueryResult>; beerStyle: z.infer<typeof BeerStyleQueryResult>;

View File

@@ -3,17 +3,16 @@ import UserContext from '@/contexts/UserContext';
import { FC, MutableRefObject, useContext, useRef } from 'react'; import { FC, MutableRefObject, useContext, useRef } from 'react';
import { z } from 'zod'; import { z } from 'zod';
import { useRouter } from 'next/router'; import { useRouter } from 'next/router';
import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema';
import BeerStyleQueryResult from '@/services/posts/beer-style-post/schema/BeerStyleQueryResult'; import BeerStyleQueryResult from '@/services/posts/beer-style-post/schema/BeerStyleQueryResult';
import useBeerStyleComments from '@/hooks/data-fetching/beer-style-comments/useBeerStyleComments'; import useBeerStyleComments from '@/hooks/data-fetching/beer-style-comments/useBeerStyleComments';
import LoadingComponent from '../BeerById/LoadingComponent';
import CommentsComponent from '../ui/CommentsComponent';
import BeerStyleCommentForm from './BeerStyleCommentForm';
import { import {
sendDeleteBeerStyleCommentRequest, sendDeleteBeerStyleCommentRequest,
sendEditBeerStyleCommentRequest, sendEditBeerStyleCommentRequest,
} from '@/requests/comments/beer-style-comment'; } from '@/requests/comments/beer-style-comment';
import CommentLoadingComponent from '../Comments/CommentLoadingComponent';
import CommentsComponent from '../Comments/CommentsComponent';
import BeerStyleCommentForm from './BeerStyleCommentForm';
interface BeerStyleCommentsSectionProps { interface BeerStyleCommentsSectionProps {
beerStyle: z.infer<typeof BeerStyleQueryResult>; beerStyle: z.infer<typeof BeerStyleQueryResult>;
@@ -31,21 +30,6 @@ const BeerStyleCommentsSection: FC<BeerStyleCommentsSectionProps> = ({ beerStyle
const commentSectionRef: MutableRefObject<HTMLDivElement | null> = useRef(null); const commentSectionRef: MutableRefObject<HTMLDivElement | null> = useRef(null);
const handleDeleteRequest = async (id: string) => {
await sendDeleteBeerStyleCommentRequest({ beerStyleId: beerStyle.id, commentId: id });
};
const handleEditRequest = async (
id: string,
data: z.infer<typeof CreateCommentValidationSchema>,
) => {
await sendEditBeerStyleCommentRequest({
beerStyleId: beerStyle.id,
commentId: id,
body: data,
});
};
return ( return (
<div className="w-full space-y-3" ref={commentSectionRef}> <div className="w-full space-y-3" ref={commentSectionRef}>
<div className="card bg-base-300"> <div className="card bg-base-300">
@@ -67,7 +51,7 @@ const BeerStyleCommentsSection: FC<BeerStyleCommentsSectionProps> = ({ beerStyle
*/ */
isLoading ? ( isLoading ? (
<div className="card bg-base-300 pb-6"> <div className="card bg-base-300 pb-6">
<LoadingComponent length={PAGE_SIZE} /> <CommentLoadingComponent length={PAGE_SIZE} />
</div> </div>
) : ( ) : (
<CommentsComponent <CommentsComponent
@@ -79,8 +63,19 @@ const BeerStyleCommentsSection: FC<BeerStyleCommentsSectionProps> = ({ beerStyle
setSize={setSize} setSize={setSize}
size={size} size={size}
mutate={mutate} mutate={mutate}
handleDeleteRequest={handleDeleteRequest} handleDeleteCommentRequest={(id) => {
handleEditRequest={handleEditRequest} return sendDeleteBeerStyleCommentRequest({
beerStyleId: beerStyle.id,
commentId: id,
});
}}
handleEditCommentRequest={(id, data) => {
return sendEditBeerStyleCommentRequest({
beerStyleId: beerStyle.id,
commentId: id,
body: data,
});
}}
/> />
) )
} }

View File

@@ -8,7 +8,7 @@ import toast from 'react-hot-toast';
import { z } from 'zod'; import { z } from 'zod';
import sendCreateBreweryCommentRequest from '@/requests/comments/brewery-comment/sendCreateBreweryCommentRequest'; import sendCreateBreweryCommentRequest from '@/requests/comments/brewery-comment/sendCreateBreweryCommentRequest';
import createErrorToast from '@/util/createErrorToast'; import createErrorToast from '@/util/createErrorToast';
import CommentForm from '../ui/CommentForm'; import CommentForm from '../Comments/CommentForm';
interface BreweryCommentFormProps { interface BreweryCommentFormProps {
breweryPost: z.infer<typeof BreweryPostQueryResult>; breweryPost: z.infer<typeof BreweryPostQueryResult>;

View File

@@ -2,11 +2,14 @@ import UserContext from '@/contexts/UserContext';
import BreweryPostQueryResult from '@/services/posts/brewery-post/schema/BreweryPostQueryResult'; import BreweryPostQueryResult from '@/services/posts/brewery-post/schema/BreweryPostQueryResult';
import { FC, MutableRefObject, useContext, useRef } from 'react'; import { FC, MutableRefObject, useContext, useRef } from 'react';
import { z } from 'zod'; import { z } from 'zod';
import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema';
import useBreweryPostComments from '@/hooks/data-fetching/brewery-comments/useBreweryPostComments'; import useBreweryPostComments from '@/hooks/data-fetching/brewery-comments/useBreweryPostComments';
import LoadingComponent from '../BeerById/LoadingComponent'; import {
import CommentsComponent from '../ui/CommentsComponent'; sendDeleteBreweryPostCommentRequest,
sendEditBreweryPostCommentRequest,
} from '@/requests/comments/brewery-comment';
import CommentLoadingComponent from '../Comments/CommentLoadingComponent';
import CommentsComponent from '../Comments/CommentsComponent';
import BreweryCommentForm from './BreweryCommentForm'; import BreweryCommentForm from './BreweryCommentForm';
interface BreweryBeerSectionProps { interface BreweryBeerSectionProps {
@@ -30,35 +33,6 @@ const BreweryCommentsSection: FC<BreweryBeerSectionProps> = ({ breweryPost }) =>
const commentSectionRef: MutableRefObject<HTMLDivElement | null> = useRef(null); const commentSectionRef: MutableRefObject<HTMLDivElement | null> = useRef(null);
const handleDeleteRequest = async (commentId: string) => {
const response = await fetch(
`/api/breweries/${breweryPost.id}/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/breweries/${breweryPost.id}/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 ( return (
<div className="w-full space-y-3" ref={commentSectionRef}> <div className="w-full space-y-3" ref={commentSectionRef}>
<div className="card"> <div className="card">
@@ -79,7 +53,7 @@ const BreweryCommentsSection: FC<BreweryBeerSectionProps> = ({ breweryPost }) =>
*/ */
isLoading ? ( isLoading ? (
<div className="card pb-6"> <div className="card pb-6">
<LoadingComponent length={PAGE_SIZE} /> <CommentLoadingComponent length={PAGE_SIZE} />
</div> </div>
) : ( ) : (
<CommentsComponent <CommentsComponent
@@ -91,8 +65,19 @@ const BreweryCommentsSection: FC<BreweryBeerSectionProps> = ({ breweryPost }) =>
size={size} size={size}
commentSectionRef={commentSectionRef} commentSectionRef={commentSectionRef}
mutate={mutate} mutate={mutate}
handleDeleteRequest={handleDeleteRequest} handleDeleteCommentRequest={(id) => {
handleEditRequest={handleEditRequest} return sendDeleteBreweryPostCommentRequest({
breweryPostId: breweryPost.id,
commentId: id,
});
}}
handleEditCommentRequest={(commentId, data) => {
return sendEditBreweryPostCommentRequest({
breweryPostId: breweryPost.id,
commentId,
body: { content: data.content, rating: data.rating },
});
}}
/> />
) )
} }

View File

@@ -3,28 +3,26 @@ import CommentQueryResult from '@/services/schema/CommentSchema/CommentQueryResu
import { FC, useState } from 'react'; import { FC, useState } from 'react';
import { useInView } from 'react-intersection-observer'; import { useInView } from 'react-intersection-observer';
import { z } from 'zod'; import { z } from 'zod';
import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema';
import CommentContentBody from './CommentContentBody'; import CommentContentBody from './CommentContentBody';
import EditCommentBody from './EditCommentBody'; import EditCommentBody from './EditCommentBody';
import UserAvatar from '../Account/UserAvatar'; import UserAvatar from '../Account/UserAvatar';
import { HandleDeleteCommentRequest, HandleEditCommentRequest } from './types';
interface CommentCardProps { interface CommentCardProps {
comment: z.infer<typeof CommentQueryResult>; comment: z.infer<typeof CommentQueryResult>;
mutate: ReturnType<typeof useBeerPostComments>['mutate']; mutate: ReturnType<typeof useBeerPostComments>['mutate'];
ref?: ReturnType<typeof useInView>['ref']; ref?: ReturnType<typeof useInView>['ref'];
handleDeleteRequest: (id: string) => Promise<void>; handleDeleteCommentRequest: HandleDeleteCommentRequest;
handleEditRequest: ( handleEditCommentRequest: HandleEditCommentRequest;
id: string,
data: z.infer<typeof CreateCommentValidationSchema>,
) => Promise<void>;
} }
const CommentCardBody: FC<CommentCardProps> = ({ const CommentCardBody: FC<CommentCardProps> = ({
comment, comment,
mutate, mutate,
ref, ref,
handleDeleteRequest, handleDeleteCommentRequest,
handleEditRequest, handleEditCommentRequest,
}) => { }) => {
const [inEditMode, setInEditMode] = useState(false); const [inEditMode, setInEditMode] = useState(false);
@@ -44,8 +42,8 @@ const CommentCardBody: FC<CommentCardProps> = ({
comment={comment} comment={comment}
mutate={mutate} mutate={mutate}
setInEditMode={setInEditMode} setInEditMode={setInEditMode}
handleDeleteRequest={handleDeleteRequest} handleDeleteCommentRequest={handleDeleteCommentRequest}
handleEditRequest={handleEditRequest} handleEditCommentRequest={handleEditCommentRequest}
/> />
)} )}
</div> </div>

View File

@@ -8,12 +8,12 @@ import type {
UseFormSetValue, UseFormSetValue,
UseFormWatch, UseFormWatch,
} from 'react-hook-form'; } from 'react-hook-form';
import FormError from './forms/FormError'; import FormError from '../ui/forms/FormError';
import FormInfo from './forms/FormInfo'; import FormInfo from '../ui/forms/FormInfo';
import FormLabel from './forms/FormLabel'; import FormLabel from '../ui/forms/FormLabel';
import FormSegment from './forms/FormSegment'; import FormSegment from '../ui/forms/FormSegment';
import FormTextArea from './forms/FormTextArea'; import FormTextArea from '../ui/forms/FormTextArea';
import Button from './forms/Button'; import Button from '../ui/forms/Button';
interface Comment { interface Comment {
content: string; content: string;

View File

@@ -1,12 +1,12 @@
import { FC } from 'react'; import { FC } from 'react';
import Spinner from '../ui/Spinner'; import Spinner from '../ui/Spinner';
import CommentLoadingCardBody from '../BeerBreweryComments/CommentLoadingCardBody'; import CommentLoadingCardBody from './CommentLoadingCardBody';
interface LoadingComponentProps { interface CommentLoadingComponentProps {
length: number; length: number;
} }
const LoadingComponent: FC<LoadingComponentProps> = ({ length }) => { const CommentLoadingComponent: FC<CommentLoadingComponentProps> = ({ length }) => {
return ( return (
<> <>
{Array.from({ length }).map((_, i) => ( {Array.from({ length }).map((_, i) => (
@@ -19,4 +19,4 @@ const LoadingComponent: FC<LoadingComponentProps> = ({ length }) => {
); );
}; };
export default LoadingComponent; export default CommentLoadingComponent;

View File

@@ -7,39 +7,33 @@ import useBeerPostComments from '@/hooks/data-fetching/beer-comments/useBeerPost
import useBreweryPostComments from '@/hooks/data-fetching/brewery-comments/useBreweryPostComments'; import useBreweryPostComments from '@/hooks/data-fetching/brewery-comments/useBreweryPostComments';
import useBeerStyleComments from '@/hooks/data-fetching/beer-style-comments/useBeerStyleComments'; import useBeerStyleComments from '@/hooks/data-fetching/beer-style-comments/useBeerStyleComments';
import NoCommentsCard from '../BeerById/NoCommentsCard'; import NoCommentsCard from './NoCommentsCard';
import LoadingComponent from '../BeerById/LoadingComponent'; import CommentLoadingComponent from './CommentLoadingComponent';
import CommentCardBody from '../BeerBreweryComments/CommentCardBody'; import CommentCardBody from './CommentCardBody';
import { HandleDeleteCommentRequest, HandleEditCommentRequest } from './types';
type HookReturnType = ReturnType< type HookReturnType = ReturnType<
typeof useBeerPostComments | typeof useBreweryPostComments | typeof useBeerStyleComments typeof useBeerPostComments | typeof useBreweryPostComments | typeof useBeerStyleComments
>; >;
type HandleDeleteRequest = (id: string) => Promise<void>;
type HandleEditRequest = (
id: string,
data: { content: string; rating: number },
) => Promise<void>;
interface CommentsComponentProps { interface CommentsComponentProps {
comments: HookReturnType['comments']; comments: HookReturnType['comments'];
commentSectionRef: MutableRefObject<HTMLDivElement | null>;
handleDeleteRequest: HandleDeleteRequest;
handleEditRequest: HandleEditRequest;
isAtEnd: HookReturnType['isAtEnd']; isAtEnd: HookReturnType['isAtEnd'];
isLoadingMore: HookReturnType['isLoadingMore']; isLoadingMore: HookReturnType['isLoadingMore'];
mutate: HookReturnType['mutate']; mutate: HookReturnType['mutate'];
pageSize: number;
setSize: HookReturnType['setSize']; setSize: HookReturnType['setSize'];
size: HookReturnType['size']; size: HookReturnType['size'];
commentSectionRef: MutableRefObject<HTMLDivElement | null>;
handleDeleteCommentRequest: HandleDeleteCommentRequest;
handleEditCommentRequest: HandleEditCommentRequest;
pageSize: number;
} }
const CommentsComponent: FC<CommentsComponentProps> = ({ const CommentsComponent: FC<CommentsComponentProps> = ({
comments, comments,
commentSectionRef, commentSectionRef,
handleDeleteRequest, handleDeleteCommentRequest,
handleEditRequest, handleEditCommentRequest,
isAtEnd, isAtEnd,
isLoadingMore, isLoadingMore,
mutate, mutate,
@@ -50,8 +44,8 @@ const CommentsComponent: FC<CommentsComponentProps> = ({
const { ref: penultimateCommentRef } = useInView({ const { ref: penultimateCommentRef } = useInView({
threshold: 0.1, threshold: 0.1,
/** /**
* When the last comment comes into view, call setSize from useBeerPostComments to * When the last comment comes into view, call setSize from the comment fetching hook
* load more comments. * to load more comments.
*/ */
onChange: (visible) => { onChange: (visible) => {
if (!visible || isAtEnd) return; if (!visible || isAtEnd) return;
@@ -78,8 +72,8 @@ const CommentsComponent: FC<CommentsComponentProps> = ({
<CommentCardBody <CommentCardBody
comment={comment} comment={comment}
mutate={mutate} mutate={mutate}
handleDeleteRequest={handleDeleteRequest} handleDeleteCommentRequest={handleDeleteCommentRequest}
handleEditRequest={handleEditRequest} handleEditCommentRequest={handleEditCommentRequest}
/> />
</div> </div>
); );
@@ -90,7 +84,7 @@ const CommentsComponent: FC<CommentsComponentProps> = ({
* If there are more comments to load, show a loading component with a * If there are more comments to load, show a loading component with a
* skeleton loader and a loading spinner. * skeleton loader and a loading spinner.
*/ */
!!isLoadingMore && <LoadingComponent length={pageSize} /> !!isLoadingMore && <CommentLoadingComponent length={pageSize} />
} }
{ {

View File

@@ -14,6 +14,7 @@ import FormInfo from '../ui/forms/FormInfo';
import FormLabel from '../ui/forms/FormLabel'; import FormLabel from '../ui/forms/FormLabel';
import FormSegment from '../ui/forms/FormSegment'; import FormSegment from '../ui/forms/FormSegment';
import FormTextArea from '../ui/forms/FormTextArea'; import FormTextArea from '../ui/forms/FormTextArea';
import { HandleDeleteCommentRequest, HandleEditCommentRequest } from './types';
interface EditCommentBodyProps { interface EditCommentBodyProps {
comment: z.infer<typeof CommentQueryResult>; comment: z.infer<typeof CommentQueryResult>;
@@ -22,19 +23,16 @@ interface EditCommentBodyProps {
mutate: ReturnType< mutate: ReturnType<
typeof useBeerPostComments | typeof useBreweryPostComments typeof useBeerPostComments | typeof useBreweryPostComments
>['mutate']; >['mutate'];
handleDeleteRequest: (id: string) => Promise<void>; handleDeleteCommentRequest: HandleDeleteCommentRequest;
handleEditRequest: ( handleEditCommentRequest: HandleEditCommentRequest;
id: string,
data: z.infer<typeof CreateCommentValidationSchema>,
) => Promise<void>;
} }
const EditCommentBody: FC<EditCommentBodyProps> = ({ const EditCommentBody: FC<EditCommentBodyProps> = ({
comment, comment,
setInEditMode, setInEditMode,
mutate, mutate,
handleDeleteRequest, handleDeleteCommentRequest,
handleEditRequest, handleEditCommentRequest,
}) => { }) => {
const { register, handleSubmit, formState, setValue, watch } = useForm< const { register, handleSubmit, formState, setValue, watch } = useForm<
z.infer<typeof CreateCommentValidationSchema> z.infer<typeof CreateCommentValidationSchema>
@@ -51,7 +49,7 @@ const EditCommentBody: FC<EditCommentBodyProps> = ({
const loadingToast = toast.loading('Deleting comment...'); const loadingToast = toast.loading('Deleting comment...');
setIsDeleting(true); setIsDeleting(true);
try { try {
await handleDeleteRequest(comment.id); await handleDeleteCommentRequest(comment.id);
await mutate(); await mutate();
toast.remove(loadingToast); toast.remove(loadingToast);
toast.success('Deleted comment.'); toast.success('Deleted comment.');
@@ -68,7 +66,7 @@ const EditCommentBody: FC<EditCommentBodyProps> = ({
try { try {
setInEditMode(true); setInEditMode(true);
await handleEditRequest(comment.id, data); await handleEditCommentRequest(comment.id, data);
await mutate(); await mutate();
toast.remove(loadingToast); toast.remove(loadingToast);
toast.success('Comment edits submitted successfully.'); toast.success('Comment edits submitted successfully.');
@@ -80,6 +78,8 @@ const EditCommentBody: FC<EditCommentBodyProps> = ({
} }
}; };
const disableForm = isSubmitting || isDeleting;
return ( return (
<div className="py-4 pr-3 animate-in fade-in-10"> <div className="py-4 pr-3 animate-in fade-in-10">
<form onSubmit={handleSubmit(onEdit)} className="space-y-3"> <form onSubmit={handleSubmit(onEdit)} className="space-y-3">
@@ -95,7 +95,7 @@ const EditCommentBody: FC<EditCommentBodyProps> = ({
placeholder="Comment" placeholder="Comment"
rows={2} rows={2}
error={!!errors.content?.message} error={!!errors.content?.message}
disabled={isSubmitting || isDeleting} disabled={disableForm}
/> />
</FormSegment> </FormSegment>
<div className="flex flex-row items-center justify-between"> <div className="flex flex-row items-center justify-between">
@@ -114,8 +114,8 @@ const EditCommentBody: FC<EditCommentBodyProps> = ({
<Rating.Item <Rating.Item
name="rating-1" name="rating-1"
className="mask mask-star cursor-default" className="mask mask-star cursor-default"
disabled={isSubmitting || isDeleting} disabled={disableForm}
aria-disabled={isSubmitting || isDeleting} aria-disabled={disableForm}
key={index} key={index}
/> />
))} ))}
@@ -125,7 +125,7 @@ const EditCommentBody: FC<EditCommentBodyProps> = ({
<button <button
type="button" type="button"
className="btn join-item btn-xs lg:btn-sm" className="btn join-item btn-xs lg:btn-sm"
disabled={isSubmitting || isDeleting} disabled={disableForm}
onClick={() => { onClick={() => {
setInEditMode(false); setInEditMode(false);
}} }}
@@ -134,7 +134,7 @@ const EditCommentBody: FC<EditCommentBodyProps> = ({
</button> </button>
<button <button
type="submit" type="submit"
disabled={isSubmitting || isDeleting} disabled={disableForm}
className="btn join-item btn-xs lg:btn-sm" className="btn join-item btn-xs lg:btn-sm"
> >
Save Save
@@ -143,7 +143,7 @@ const EditCommentBody: FC<EditCommentBodyProps> = ({
type="button" type="button"
className="btn join-item btn-xs lg:btn-sm" className="btn join-item btn-xs lg:btn-sm"
onClick={onDelete} onClick={onDelete}
disabled={isDeleting || formState.isSubmitting} disabled={disableForm}
> >
Delete Delete
</button> </button>

View File

@@ -0,0 +1,12 @@
import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { z } from 'zod';
type APIResponse = z.infer<typeof APIResponseValidationSchema>;
export type HandleEditCommentRequest = (
id: string,
data: z.infer<typeof CreateCommentValidationSchema>,
) => Promise<APIResponse>;
export type HandleDeleteCommentRequest = (id: string) => Promise<APIResponse>;

View File

@@ -11,9 +11,11 @@ import {
* *
* @param params - The parameters for the request. * @param params - The parameters for the request.
* @param params.body - The body of the request. * @param params.body - The body of the request.
* @param params.body.content - The content of the comment.
* @param params.body.rating - The rating of the beer.
* @param params.commentId - The id of the comment to edit. * @param params.commentId - The id of the comment to edit.
* @param params.beerPostId - The id of the beer post the comment belongs to. * @param params.beerPostId - The id of the beer post the comment belongs to.
* @returns The edited comment. * @returns The JSON response from the server.
* @throws An error if the request fails or the response is invalid. * @throws An error if the request fails or the response is invalid.
*/ */
export const editBeerPostCommentRequest: SendEditBeerPostCommentRequest = async ({ export const editBeerPostCommentRequest: SendEditBeerPostCommentRequest = async ({
@@ -47,7 +49,7 @@ export const editBeerPostCommentRequest: SendEditBeerPostCommentRequest = async
* @param params - The parameters for the request. * @param params - The parameters for the request.
* @param params.commentId - The id of the comment to delete. * @param params.commentId - The id of the comment to delete.
* @param params.beerPostId - The id of the beer post the comment belongs to. * @param params.beerPostId - The id of the beer post the comment belongs to.
* @returns The deleted comment. * @returns The JSON response from the server.
* @throws An error if the request fails or the response is invalid. * @throws An error if the request fails or the response is invalid.
*/ */
export const deleteBeerPostCommentRequest: SendDeleteBeerPostCommentRequest = async ({ export const deleteBeerPostCommentRequest: SendDeleteBeerPostCommentRequest = async ({

View File

@@ -6,6 +6,88 @@ import {
SendEditBeerStyleCommentRequest, SendEditBeerStyleCommentRequest,
} from './types'; } from './types';
/**
* Sends an api request to edit a beer style comment.
*
* @param params - The parameters for the request.
* @param params.body - The body of the request.
* @param params.body.content - The content of the comment.
* @param params.body.rating - The rating of the beer.
* @param params.beerStyleId - The id of the beer style the comment belongs to.
* @param params.commentId - The id of the comment to edit.
* @returns The JSON response from the server.
* @throws An error if the request fails or the response is invalid.
*/
export const sendEditBeerStyleCommentRequest: SendEditBeerStyleCommentRequest = async ({
commentId,
body: { content, rating },
beerStyleId,
}) => {
const response = await fetch(`/api/beers/styles/${beerStyleId}/comments/${commentId}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ content, rating }),
});
if (!response.ok) {
throw new Error(response.statusText);
}
const json = await response.json();
const parsed = APIResponseValidationSchema.safeParse(json);
if (!parsed.success) {
throw new Error('Invalid API response');
}
return parsed.data;
};
/**
* Sends an api request to delete a beer style comment.
*
* @param params - The parameters for the request.
* @param params.beerStyleId - The id of the beer style the comment belongs to.
* @param params.commentId - The id of the comment to delete.
* @returns The json response from the server.
* @throws An error if the request fails or the response is invalid.
*/
export const sendDeleteBeerStyleCommentRequest: SendDeleteBeerStyleCommentRequest =
async ({ beerStyleId, commentId }) => {
const response = await fetch(
`/api/beers/styles/${beerStyleId}/comments/${commentId}`,
{
method: 'DELETE',
},
);
if (!response.ok) {
throw new Error(response.statusText);
}
const json = await response.json();
const parsed = APIResponseValidationSchema.safeParse(json);
if (!parsed.success) {
throw new Error('Invalid API response');
}
return parsed.data;
};
/**
* Sends an api request to create a beer style comment.
*
* @param params - The parameters for the request.
* @param params.body - The body of the request.
* @param params.body.content - The content of the comment.
* @param params.body.rating - The rating of the beer.
* @param params.beerStyleId - The id of the beer style the comment belongs to.
* @returns The created comment.
* @throws An error if the request fails or the response is invalid.
*/
export const sendCreateBeerStyleCommentRequest: SendCreateBeerStyleCommentRequest = export const sendCreateBeerStyleCommentRequest: SendCreateBeerStyleCommentRequest =
async ({ beerStyleId, body: { content, rating } }) => { async ({ beerStyleId, body: { content, rating } }) => {
const response = await fetch(`/api/beers/styles/${beerStyleId}/comments`, { const response = await fetch(`/api/beers/styles/${beerStyleId}/comments`, {
@@ -33,53 +115,3 @@ export const sendCreateBeerStyleCommentRequest: SendCreateBeerStyleCommentReques
return parsedPayload.data; return parsedPayload.data;
}; };
export const sendEditBeerStyleCommentRequest: SendEditBeerStyleCommentRequest = async ({
commentId,
body: { content, rating },
beerStyleId,
}) => {
const response = await fetch(`/api/beers/styles/${beerStyleId}/comments/${commentId}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ content, rating }),
});
if (!response.ok) {
throw new Error(response.statusText);
}
const json = await response.json();
const parsed = APIResponseValidationSchema.safeParse(json);
if (!parsed.success) {
throw new Error('Invalid API response');
}
return parsed.data;
};
export const sendDeleteBeerStyleCommentRequest: SendDeleteBeerStyleCommentRequest =
async ({ beerStyleId, commentId }) => {
const response = await fetch(
`/api/beers/styles/${beerStyleId}/comments/${commentId}`,
{
method: 'DELETE',
},
);
if (!response.ok) {
throw new Error(response.statusText);
}
const json = await response.json();
const parsed = APIResponseValidationSchema.safeParse(json);
if (!parsed.success) {
throw new Error('Invalid API response');
}
return parsed.data;
};

View File

@@ -0,0 +1,109 @@
import CommentQueryResult from '@/services/schema/CommentSchema/CommentQueryResult';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import {
SendCreateBreweryPostCommentRequest,
SendDeleteBreweryPostCommentRequest,
SendEditBreweryPostCommentRequest,
} from './types';
/**
* Sends an api request to edit a brewery comment.
*
* @param params - The parameters for the request.
* @param params.breweryId - The id of the brewery the comment belongs to.
* @param params.commentId - The id of the comment to edit.
* @param params.body - The body of the request.
* @param params.body.content - The content of the comment.
* @param params.body.rating - The rating of the beer.
*/
export const sendEditBreweryPostCommentRequest: SendEditBreweryPostCommentRequest =
async ({ body: { content, rating }, breweryPostId, commentId }) => {
const response = await fetch(
`/api/breweries/${breweryPostId}/comments/${commentId}`,
{
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ content, rating }),
},
);
if (!response.ok) {
throw new Error(response.statusText);
}
const json = await response.json();
const parsed = APIResponseValidationSchema.safeParse(json);
if (!parsed.success) {
throw new Error(parsed.error.message);
}
return parsed.data;
};
/**
* Sends an api request to delete a brewery comment.
*
* @param params - The parameters for the request.
* @param params.breweryId - The id of the brewery the comment belongs to.
* @param params.commentId - The id of the comment to delete.
* @returns The JSON response from the server.
* @throws An error if the request fails or the response is invalid.
*/
export const sendDeleteBreweryPostCommentRequest: SendDeleteBreweryPostCommentRequest =
async ({ breweryPostId, commentId }) => {
const response = await fetch(
`/api/breweries/${breweryPostId}/comments/${commentId}`,
{
method: 'DELETE',
},
);
if (!response.ok) {
throw new Error(response.statusText);
}
const json = await response.json();
const parsed = APIResponseValidationSchema.safeParse(json);
if (!parsed.success) {
throw new Error(parsed.error.message);
}
return parsed.data;
};
/**
* Sends an api request to create a brewery comment.
*
* @param params - The parameters for the request.
* @param params.body - The body of the request.
* @param params.body.content - The content of the comment.
* @param params.body.rating - The rating of the beer.
* @param params.breweryId - The id of the brewery the comment belongs to.
* @returns The created comment.
* @throws An error if the request fails or the response is invalid.
*/
export const sendCreateBreweryCommentRequest: SendCreateBreweryPostCommentRequest =
async ({ body: { content, rating }, breweryPostId }) => {
const response = await fetch(`/api/breweries/${breweryPostId}/comments`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ content, rating }),
});
if (!response.ok) {
throw new Error(response.statusText);
}
const data = await response.json();
const parsedResponse = APIResponseValidationSchema.safeParse(data);
if (!parsedResponse.success) {
throw new Error('Invalid API response');
}
const parsedPayload = CommentQueryResult.safeParse(parsedResponse.data.payload);
if (!parsedPayload.success) {
throw new Error('Invalid API response payload');
}
return parsedPayload.data;
};

View File

@@ -0,0 +1,19 @@
import CommentQueryResult from '@/services/schema/CommentSchema/CommentQueryResult';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { z } from 'zod';
export type SendEditBreweryPostCommentRequest = (args: {
body: { content: string; rating: number };
commentId: string;
breweryPostId: string;
}) => Promise<z.infer<typeof APIResponseValidationSchema>>;
export type SendDeleteBreweryPostCommentRequest = (args: {
commentId: string;
breweryPostId: string;
}) => Promise<z.infer<typeof APIResponseValidationSchema>>;
export type SendCreateBreweryPostCommentRequest = (args: {
breweryPostId: string;
body: { content: string; rating: number };
}) => Promise<z.infer<typeof CommentQueryResult>>;