Skip to content

Commit

Permalink
Merge pull request #611 from pearpearB/Fix/게시글좋아요누를때마다새로고침-610
Browse files Browse the repository at this point in the history
[Fix] 게시글 댓글 좋아요 누를 때마다 새로고침 되는 문제 해결 Close #610
  • Loading branch information
pearpearB authored Apr 10, 2022
2 parents ebccda6 + 23954f6 commit 83656b1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
6 changes: 4 additions & 2 deletions client/src/components/Comments/Comments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function Comments({ roleType, comment, commentsUserList }: GreetingProps) {
recomments,
} = comment;
const [boxState, setBoxState] = useState<boolean>(isLiked);
const [usersLikeCnt, setUsersLikeCnt] = useState(likeCnt);
const [openEditor, setOpenEditor] = useState<boolean>(false);
const [openReCmt, setOpenReCmt] = useState<boolean>(false);
const writer = whoIsWriter(isAuthor, commentsUserList, authorId);
Expand Down Expand Up @@ -157,7 +158,8 @@ function Comments({ roleType, comment, commentsUserList }: GreetingProps) {
},
{
onSuccess: () => {
queryClient.invalidateQueries(['comment_key']);
if (boxState) setUsersLikeCnt(usersLikeCnt - 1);
else setUsersLikeCnt(usersLikeCnt + 1);
setBoxState(!boxState);
},
onError: () => {
Expand Down Expand Up @@ -228,7 +230,7 @@ function Comments({ roleType, comment, commentsUserList }: GreetingProps) {
<div>
<GrLike />
</div>
<div>{likeCnt}</div>
<div>{usersLikeCnt}</div>
</LikesBox>
</CommentBottom>
</CommentWrap>
Expand Down
6 changes: 4 additions & 2 deletions client/src/components/Comments/ReComments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function ReComments({ roleType, recomment, commentsUserList }: GreetingProps) {
targetAuthorId,
} = recomment;
const [boxState, setBoxState] = useState<boolean>(isLiked);
const [usersLikeCnt, setUsersLikeCnt] = useState(likeCnt);
const [openEditor, setOpenEditor] = useState<boolean>(false);
const [openReReCmt, setOpenReReCmt] = useState<boolean>(false);
const writer = whoIsWriter(isAuthor, commentsUserList, authorId);
Expand Down Expand Up @@ -154,7 +155,8 @@ function ReComments({ roleType, recomment, commentsUserList }: GreetingProps) {
},
{
onSuccess: () => {
queryClient.invalidateQueries(['comment_key']);
if (boxState) setUsersLikeCnt(usersLikeCnt - 1);
else setUsersLikeCnt(usersLikeCnt + 1);
setBoxState(!boxState);
},
onError: () => {
Expand Down Expand Up @@ -241,7 +243,7 @@ function ReComments({ roleType, recomment, commentsUserList }: GreetingProps) {
<div>
<GrLike />
</div>
<div>{likeCnt}</div>
<div>{usersLikeCnt}</div>
</LikesBox>
</CommentBottom>
</ReCommentWrap>
Expand Down
13 changes: 9 additions & 4 deletions client/src/pages/Detail/Detail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ export default function Detail() {
const [commentData, setCommentData] = useState([]);
const [commentsUserList, setCommentsUserList] = useState([-1]);
const [commentTotalCnt, setCommentTotalCnt] = useState(0);
const [boxState, setBoxState] = useState(isLiked);
const [boxState, setBoxState] = useState(false);
const [usersLikeCnt, setUsersLikeCnt] = useState(0);
const [openEditor, setOpenEditor] = useState(false);
const currentUrl = window.location.href;
const postUrl = currentUrl.split('postId=')[1];
Expand All @@ -102,10 +103,12 @@ export default function Detail() {
);
const results = useQueries([
{
queryKey: ['detail_key', postUrl, boxState],
queryKey: ['detail_key', postUrl],
queryFn: () => {
instance.get(`/post?postId=${postUrl}`).then((res) => {
setDetailData(res.data);
setBoxState(res.data.isLiked);
setUsersLikeCnt(res.data.likeCnt);
});
},
retry: 0,
Expand Down Expand Up @@ -219,6 +222,8 @@ export default function Detail() {
{ path: `/post/like?postId=${postUrl}`, data: undefined },
{
onSuccess: () => {
if (boxState) setUsersLikeCnt(usersLikeCnt - 1);
else setUsersLikeCnt(usersLikeCnt + 1);
setBoxState(!boxState);
},
onError: () => {
Expand Down Expand Up @@ -302,11 +307,11 @@ export default function Detail() {
</ContentWrap>
)}
<LikeWrap>
<LikesBox boxState={isLiked} onClick={likeBoxHandler}>
<LikesBox boxState={boxState} onClick={likeBoxHandler}>
<div>
<GrLike />
</div>
<div>{likeCnt}</div>
<div>{usersLikeCnt}</div>
</LikesBox>
</LikeWrap>
<CommentContainer>
Expand Down

0 comments on commit 83656b1

Please sign in to comment.