Skip to content

Commit

Permalink
refactor: 댓글 불러올 때 api 요청 수 줄이기
Browse files Browse the repository at this point in the history
  • Loading branch information
wildcatco committed Aug 22, 2023
1 parent df48ad7 commit fdeba79
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
7 changes: 1 addition & 6 deletions src/features/comments/components/CommentItem/CommentItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { useEffect, useRef } from 'react';
import { useNavigate } from 'react-router-dom';
import { commentStateAtom } from '@/features/comments/states/comment';
import { useUser } from '@/features/user/queries/useUser';
import { useGetMyChoice } from '@/features/vote/queries';
import CommentContent from './CommentContent';
import CommentFooter from './CommentFooter';
import CommentHeader from './CommentHeader';
Expand Down Expand Up @@ -37,10 +36,6 @@ export function CommentItem({
scrollEnabled,
}: CommentItemProps) {
const [commentState, setCommentState] = useAtom(commentStateAtom);
const { data: choice } = useGetMyChoice({
userId: user.id,
postId: user.choiceLabel ? undefined : commentState.postId || undefined,
});
const navigate = useNavigate();
const { user: currentUser } = useUser();
const ref = useRef<HTMLDivElement | null>(null);
Expand Down Expand Up @@ -111,7 +106,7 @@ export function CommentItem({
email={user.email}
nickname={user.nickname}
age={user.age}
choice={user.choiceLabel || choice?.label || null}
choice={user.choiceLabel}
isWriter={isWriter}
isMyComment={user.id === currentUser?.id}
commentId={commentId}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function CommentList({ writerId, comments }: CommentListProps) {
email: reply.user.email,
nickname: reply.user.nickname,
age: calculateAgeFromBirthDate(reply.user.birthDate),
choiceLabel: null, // TODO: api 응답 변경
choiceLabel: reply.user.choice?.label || null,
}}
isWriter={writerId === reply.user.id}
content={reply.content}
Expand Down
7 changes: 7 additions & 0 deletions src/features/comments/queries/useGetComments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ export function useGetComments(postId: number) {
profileImage: user.profileImageUrl,
nickname: user.nickname,
birthDate: user.birthDate,
worryChoice: user.worryChoice
? {
id: user.worryChoice.id,
label: user.worryChoice.label,
isAbstained: user.worryChoice.isAbstained === 'yes',
}
: null,
},
}),
) || [],
Expand Down
5 changes: 5 additions & 0 deletions src/features/comments/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export interface Comment {
profileImage: string | null;
nickname: string;
birthDate: string;
choice: {
id: number;
label: string;
isAbstained: boolean;
} | null;
};
}[];
}

0 comments on commit fdeba79

Please sign in to comment.