Skip to content

Commit

Permalink
feat: 새로운 댓글이 있는지 조회하는 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Doeunnkimm committed Mar 25, 2024
1 parent 0a73c45 commit 961f66b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
7 changes: 6 additions & 1 deletion src/features/comment/CommentsBottomSheet.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { useOverlay } from '@toss/use-overlay';
import { AnimatePresence } from 'framer-motion';
import { useAtomValue } from 'jotai';

import { Comment } from '@/components';
import { useGetComment } from '@/hooks/reactQuery/comment';
import { useGetComment, useGetHasNewComment } from '@/hooks/reactQuery/comment';

import { isMyGoalAtom } from '../goal/atoms';

import { CommentBottomSheetLayout } from './CommentBottomSheetLayout';
import { DeleteCommentBottomSheet } from './DeleteCommentBottomSheet';
Expand All @@ -15,8 +18,10 @@ interface CommentsBottomSheetProps {
}

export const CommentsBottomSheet = ({ goalId, ...props }: CommentsBottomSheetProps) => {
const isMyGoal = useAtomValue(isMyGoalAtom);
const { open } = useOverlay();
const { data } = useGetComment({ goalId });
useGetHasNewComment({ goalId, isMyGoal });

const handleDelete = (commentId: number) => () => {
open(({ isOpen, close }) => (
Expand Down
15 changes: 8 additions & 7 deletions src/features/goal/components/detail/comment/AddCommentButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import { useAtomValue } from 'jotai';

import BlueCommentIcon from '@/assets/icons/blue-comment-icon.svg';
import { CommentsBottomSheet } from '@/features/comment';
import { goalIdAtom } from '@/features/goal/atoms';
import { goalIdAtom, isMyGoalAtom } from '@/features/goal/atoms';
import { useGetHasNewComment } from '@/hooks/reactQuery/comment';

interface AddCommentButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
hasUnreadComments?: boolean;
}

export const AddCommentButton = ({ hasUnreadComments, ...props }: AddCommentButtonProps) => {
export const AddCommentButton = (props: ButtonHTMLAttributes<HTMLButtonElement>) => {
const { open } = useOverlay();
const goalId = useAtomValue(goalIdAtom);
const isMyGoal = useAtomValue(isMyGoalAtom);
const { data: hasNewComments } = useGetHasNewComment({ goalId, isMyGoal });

const handleOpenComments = () => {
open(({ isOpen, close }) => <CommentsBottomSheet open={isOpen} onClose={close} goalId={goalId} />);
Expand All @@ -27,7 +26,9 @@ export const AddCommentButton = ({ hasUnreadComments, ...props }: AddCommentButt
>
<BlueCommentIcon />
</button>
{hasUnreadComments && <span className="absolute top-0 right-0 w-[14px] h-[14px] rounded-full bg-red-30" />}
{isMyGoal && hasNewComments && (
<span className="absolute top-0 right-0 w-[14px] h-[14px] rounded-full bg-red-30" />
)}
</div>
);
};
1 change: 1 addition & 0 deletions src/hooks/reactQuery/comment/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { useCreateComment } from './useCreateComment';
export { useDeleteComment } from './useDeleteComment';
export { useGetComment } from './useGetComment';
export { useGetHasNewComment } from './useGetHasNewComment';
1 change: 1 addition & 0 deletions src/hooks/reactQuery/comment/useGetComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ export const useGetComment = ({ goalId }: CommentRequestParams) => {
return useQuery<CommentResponse>({
queryKey: ['comment', goalId],
queryFn: () => api.get<CommentResponse>(`/goal/${goalId}/comment`),
staleTime: 1000,
});
};
19 changes: 19 additions & 0 deletions src/hooks/reactQuery/comment/useGetHasNewComment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useQuery } from '@tanstack/react-query';

import { api } from '@/apis';

type CommentRequestParams = {
isMyGoal: boolean;
goalId: number;
};

type CommentResponse = boolean;

export const useGetHasNewComment = ({ goalId, isMyGoal }: CommentRequestParams) => {
return useQuery<CommentResponse>({
queryKey: ['comment', 'new', goalId],
queryFn: () => api.get<CommentResponse>(`/goal/${goalId}/comment/new`),
enabled: goalId !== -1 || isMyGoal,
staleTime: 1000,
});
};

0 comments on commit 961f66b

Please sign in to comment.