-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #269 from codestates-seb/feat/coment-haeun
[구현] 댓글 (comment) CRUD 구현 완료
- Loading branch information
Showing
9 changed files
with
280 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import tokenRequestApi from "./TokenRequestApi"; | ||
import { eduApi } from "./EduApi"; | ||
|
||
// ====================== 댓글 등록 (post) =========================== | ||
|
||
export interface CommentDto { | ||
content: string; | ||
studygroupId: number; | ||
commentId: number; | ||
nickName: string; | ||
} | ||
|
||
export const postComment = async (data: string) => { | ||
try { | ||
const jsonData = JSON.stringify({ content: data }); // 데이터를 JSON 문자열로 직렬화 | ||
await tokenRequestApi.post("/studygroup/31/comment", jsonData); | ||
} catch (error) { | ||
console.log(error); | ||
throw new Error("댓글 등록 실패"); | ||
} //31 -> 변수로 나중에 바꿔야 함 | ||
}; | ||
// ====================== 댓글 수정 (patch) =========================== | ||
export const patchComment = async ( | ||
studyGroupId: number, | ||
patchId: number, | ||
data: string | ||
) => { | ||
try { | ||
const jsonData = JSON.stringify({ content: data }); // 데이터를 JSON 문자열로 직렬화 | ||
await tokenRequestApi.patch( | ||
`/studygroup/${studyGroupId}/comment/${patchId}`, | ||
jsonData | ||
); | ||
} catch (error) { | ||
console.log(error); | ||
throw new Error("댓글 수정 실패"); | ||
} | ||
}; //31 -> 변수로 나중에 바꿔야 함 | ||
|
||
// ====================== 댓글 전부 조회 (get) =========================== | ||
export const getComments = async ( | ||
studyGroupId: number | ||
): Promise<CommentDto[]> => { | ||
try { | ||
const response = await eduApi.get<CommentDto[]>( | ||
`/studygroup/${studyGroupId}/comments` | ||
); //31 -> 변수로 나중에 바꿔야 함 | ||
return response.data; | ||
} catch (error) { | ||
console.log(error); | ||
throw new Error("댓글 전부 조회 실패"); | ||
} | ||
}; | ||
|
||
// ====================== 댓글 삭제 (DELETE) =========================== | ||
export const deleteComment = async (studyGroupId: number, patchId: number) => { | ||
try { | ||
const response = await tokenRequestApi.delete( | ||
`/studygroup/${studyGroupId}/comment/${patchId}` | ||
); | ||
console.log("댓글이 삭제되었습니다.", response); | ||
} catch (error) { | ||
console.error("댓글을 삭제하는데 실패했습니다. 권한을 확인하세요", error); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
import { useRecoilValue } from "recoil"; | ||
import { LogInState } from "../recoil/atoms/LogInState"; | ||
import { useEffect, useState } from "react"; | ||
import styled from "styled-components"; | ||
import { | ||
CommentDto, | ||
deleteComment, | ||
getComments, | ||
patchComment, | ||
} from "../apis/CommentApi"; | ||
import { validateEmptyInput } from "../pages/utils/loginUtils"; | ||
import { useNavigate } from "react-router-dom"; | ||
|
||
const StudyCommentList = ({}) => { | ||
const isLoggedIn = useRecoilValue(LogInState); | ||
const navigate = useNavigate(); | ||
|
||
const [comments, setComments] = useState<CommentDto[]>([]); | ||
const [comment, setComment] = useState(""); | ||
const [patchId, setPatchId] = useState<number | null>(null); | ||
const [isUpdateMode, setIsUpdateMode] = useState(false); | ||
|
||
const handleUpdate = (id: number, content: string) => { | ||
if (!isLoggedIn) navigate("/login"); | ||
setIsUpdateMode(!isUpdateMode); | ||
setPatchId(id); | ||
setComment(content); | ||
}; | ||
|
||
const handleDelete = async (patchId: number) => { | ||
if (!isLoggedIn) navigate("/login"); | ||
try { | ||
const studyGroupId = 31; | ||
await deleteComment(studyGroupId, patchId); | ||
} catch (error) { | ||
console.log("댓글 삭제 실패", error); | ||
} | ||
}; | ||
|
||
const handleComment = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
setComment(e.target.value); | ||
//console.log(id); | ||
}; | ||
|
||
const handleUpdateButton = async () => { | ||
if (!isLoggedIn) navigate("/login"); | ||
|
||
if (validateEmptyInput(comment)) { | ||
alert("댓글 내용을 입력해주세요."); | ||
} else { | ||
try { | ||
const studyGroupId = 31; | ||
if (patchId) { | ||
await patchComment(studyGroupId, patchId, comment); | ||
setIsUpdateMode(false); | ||
setPatchId(null); | ||
setComment(""); | ||
} | ||
} catch (error) { | ||
console.log("댓글 등록 실패", error); | ||
} | ||
} | ||
}; | ||
useEffect(() => { | ||
const fetchData = async () => { | ||
try { | ||
const studyGroupId = 31; | ||
const newComment = await getComments(studyGroupId); | ||
setComments(newComment); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
}; | ||
fetchData(); | ||
}, [!isUpdateMode]); // post시 바로 변경될 수 있도록 의존성 배열 추가 예정 | ||
return ( | ||
<> | ||
<ul> | ||
{comments.map((comment) => { | ||
return ( | ||
<CommentItemDiv key={comment.commentId}> | ||
<ContentItem> | ||
<p>{comment.nickName}</p> | ||
<> | ||
{isUpdateMode && patchId === comment.commentId ? ( | ||
<> | ||
<input | ||
defaultValue={comment.content} | ||
onChange={handleComment} | ||
></input> | ||
<button onClick={handleUpdateButton}>완료</button> | ||
</> | ||
) : ( | ||
<span>{comment.content}</span> | ||
)} | ||
</> | ||
</ContentItem> | ||
<ButtonDiv> | ||
<button | ||
onClick={() => | ||
handleUpdate(comment.commentId, comment.content) | ||
} | ||
> | ||
수정 | ||
</button> | ||
<button onClick={() => handleDelete(comment.commentId)}> | ||
삭제 | ||
</button> | ||
</ButtonDiv> | ||
</CommentItemDiv> | ||
); | ||
})} | ||
</ul> | ||
</> | ||
); | ||
}; | ||
|
||
const CommentItemDiv = styled.div` | ||
width: 80%; | ||
height: 70px; | ||
padding: 10px 10px 10px 10px; | ||
background-color: #ffffff; | ||
display: flex; | ||
justify-content: space-between; | ||
border-bottom: solid #e9e9e9; | ||
`; | ||
const ContentItem = styled.div` | ||
text-align: left; | ||
button { | ||
margin-left: 10px; | ||
background-color: #858da8; | ||
font-size: 13px; | ||
padding: 3px; | ||
color: #ffffff; | ||
} | ||
p { | ||
font-size: 16px; | ||
font-weight: bold; | ||
color: #2759a2; | ||
} | ||
span { | ||
font-size: 12px; | ||
} | ||
`; | ||
|
||
const ButtonDiv = styled.div` | ||
height: 100%; | ||
display: flex; | ||
align-items: flex-end; | ||
button { | ||
font-size: 12px; | ||
padding: 3px; | ||
color: #858da8; | ||
} | ||
`; | ||
export default StudyCommentList; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.