Skip to content

Commit

Permalink
Merge pull request #120 from KUIT-Space/bug/#119/게시판-좋아요-및-등록-버그
Browse files Browse the repository at this point in the history
Bug/#119: 게시판 좋아요 및 등록 버그
  • Loading branch information
YangJJune authored Aug 20, 2024
2 parents 6bf2c03 + d955991 commit 084aa1d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 48 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
27 changes: 13 additions & 14 deletions src/pages/BoardPage/BoardRegisterPage/BoardRegisterPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useEffect, useRef, useState } from "react";
import { useNavigate } from "react-router-dom";
import styled from "styled-components";

import { CreateBoardPostApi } from "@/apis/Board/BoardPostApi";
Expand Down Expand Up @@ -120,10 +121,9 @@ const BoardRegisterPage = () => {
const [isNotice, setIsNotice] = useState<boolean>(false);
const [selectedImages, setSelectedImages] = useState<File[]>([]);

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

// TODO: API 연동 시 수정 예정
const isManager = true;
const spaceId = localStorage.getItem("spaceId");

const inputRef = useRef<HTMLInputElement>(null);
const textareaRef = useRef<HTMLTextAreaElement>(null);
Expand Down Expand Up @@ -154,6 +154,7 @@ const BoardRegisterPage = () => {
).then((res) => {
if (res) {
console.log("생성 완료: ", res);
navigate("/board");
}
});
}
Expand All @@ -175,17 +176,15 @@ const BoardRegisterPage = () => {
}
></TopBarText>
<BoardRegisterContainer>
{isManager && (
<BoardRegisterManagerTitle>
<CheckBox
onClick={() => {
setIsNotice((prev) => !prev);
console.log(isNotice);
}}
/>
<span className={isNotice ? "board-register-manager-active" : ""}>공지로 등록하기</span>
</BoardRegisterManagerTitle>
)}
<BoardRegisterManagerTitle>
<CheckBox
onClick={() => {
setIsNotice((prev) => !prev);
console.log(isNotice);
}}
/>
<span className={isNotice ? "board-register-manager-active" : ""}>공지로 등록하기</span>
</BoardRegisterManagerTitle>
<BoardRegisterTitle
ref={inputRef}
placeholder="제목을 입력해주세요."
Expand Down

0 comments on commit 084aa1d

Please sign in to comment.