Skip to content

Commit

Permalink
fix: modify like counts by data
Browse files Browse the repository at this point in the history
  • Loading branch information
hyunn522 committed Aug 20, 2024
1 parent b53d096 commit d955991
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/apis/Board/BoardPostLikeApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createRequestOptionsJSON_AUTH, fetchApi } from "@/apis/_createRequestOp

interface PostLikeOnPostApiResponseType {
code: number;
status: number;
status: string;
message: string;
result: string;
}
Expand Down
58 changes: 29 additions & 29 deletions src/pages/BoardPage/BoardDetailpage/BoardDetailPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,39 +212,13 @@ const BoardDetailPage = () => {

const spaceId = localStorage.getItem("spaceId");

useEffect(() => {
if (spaceId !== null) {
getPostDetailApi(Number.parseInt(spaceId), Number.parseInt(id || "0"))
.then((res) => {
if (res === null) {
setPostsData(undefined);
} else {
setPostsData(res.result);
setIsLikeNew(res.result.like);
setLikeCountNew(res.result.likeCount);
}
})
.catch((err) => {
console.error(err);
setPostsData(undefined);
});
getPostCommentApi(Number.parseInt(spaceId), Number.parseInt(id || "0"))
.then((res) => {
if (res !== null) {
setCommentsData(res.result);
}
})
.catch((err) => console.log(err));
}
}, [newCommentCount]);

const handleLike = () => {
if (spaceId !== null && postsData !== undefined) {
if (postsData.like === true) {
if (isLikeNew === true) {
// ์ข‹์•„์š” ํ•ด์ œ
deleteLikeOnPostApi(Number.parseInt(spaceId), postsData.postId)
.then((res) => {
if (res !== null) {
if (res?.status === "OK") {
setIsLikeNew(false);
setLikeCountNew((prev) => prev - 1);
}
Expand All @@ -255,7 +229,7 @@ const BoardDetailPage = () => {
} else {
postLikeOnPostApi(Number.parseInt(spaceId), postsData.postId)
.then((res) => {
if (res !== null) {
if (res?.status === "OK") {
setIsLikeNew(true);
setLikeCountNew((prev) => prev + 1);
}
Expand All @@ -267,6 +241,32 @@ const BoardDetailPage = () => {
}
};

useEffect(() => {
if (spaceId !== null) {
getPostDetailApi(Number.parseInt(spaceId), Number.parseInt(id || "0"))
.then((res) => {
if (res === null) {
setPostsData(undefined);
} else {
setPostsData(res.result);
setIsLikeNew(res.result.like);
setLikeCountNew(res.result.likeCount);
}
})
.catch((err) => {
console.error(err);
setPostsData(undefined);
});
getPostCommentApi(Number.parseInt(spaceId), Number.parseInt(id || "0"))
.then((res) => {
if (res !== null) {
setCommentsData(res.result);
}
})
.catch((err) => console.log(err));
}
}, [newCommentCount]);

const handleRegisterComment = () => {
if (spaceId !== null && postsData !== undefined) {
createPostCommentApi(Number.parseInt(spaceId), postsData?.postId, commentValue)
Expand Down
8 changes: 4 additions & 4 deletions src/pages/BoardPage/BoardPostItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ const BoardPostItem = ({

const handleLike = () => {
if (spaceId !== null) {
if (isLike === true) {
if (isLikeNew === true) {
// ์ข‹์•„์š” ํ•ด์ œ
deleteLikeOnPostApi(Number.parseInt(spaceId), postId)
.then((res) => {
if (res !== null) {
if (res?.status === "OK") {
setIsLikeNew(false);
setLikeCountNew((prev) => prev - 1);
}
Expand All @@ -189,7 +189,7 @@ const BoardPostItem = ({
} else {
postLikeOnPostApi(Number.parseInt(spaceId), postId)
.then((res) => {
if (res !== null) {
if (res?.status === "OK") {
setIsLikeNew(true);
setLikeCountNew((prev) => prev + 1);
}
Expand Down Expand Up @@ -224,7 +224,7 @@ const BoardPostItem = ({
<div>
<BoardPostItemLikeBtn
className={isLikeNew ? "liked" : ""}
onClick={() => setIsLikeNew((prev) => !prev)}
// onClick={() => setIsLikeNew((prev) => !prev)}
>
<img src={isLikeNew ? heartLiked : heartUnliked} alt="์ข‹์•„์š”" onClick={handleLike} />
{likeCountNew}
Expand Down

0 comments on commit d955991

Please sign in to comment.