대댓글완료

This commit is contained in:
kimdaae328 2025-02-14 03:01:30 +09:00
parent 04ab0325b7
commit 737481cbae
4 changed files with 95 additions and 79 deletions

View File

@ -76,7 +76,7 @@ const comment = ref('');
const password = ref('');
const isCheck = ref(false);
const emit = defineEmits(['submit']);
const emit = defineEmits(['submitComment']);
watch(() => props.unknown, (newVal) => {
if (!newVal) {
@ -85,7 +85,7 @@ watch(() => props.unknown, (newVal) => {
});
function handleCommentSubmit() {
emit('submit', {
emit('submitComment', {
comment: comment.value,
password: password.value,
});

View File

@ -5,16 +5,22 @@
<p class="m-0">{{ comment.content }}</p>
</div>
<PlusButton v-if="isPlusButton" @click="toggleComment" class="mt-6"/>
<BoardComentArea v-if="isComment" @submit="submitComment"/>
<BoardComentArea v-if="isComment" @submitComment="submitComment"/>
<!-- 대댓글 -->
<ul v-if="comment.children && comment.children.length" class="list-unstyled">
<li
v-for="child in comment.children"
:key="child.id"
class="pt-8 ps-10"
class="mt-8 pt-6 p
s-10 border-top"
>
<BoardComment :comment="child" :isPlusButton="false" :isChild="true" @submitComment="addChildComment" />
<BoardComment
:comment="child"
:isPlusButton="false"
:isChild="true"
@submitComment="submitComment"
/>
</li>
</ul>
<!-- <ul class="list-unstyled twoDepth">
@ -32,10 +38,10 @@
</template>
<script setup>
import { defineProps, defineEmits, ref } from 'vue';
import BoardProfile from './BoardProfile.vue';
import BoardComentArea from './BoardComentArea.vue';
import PlusButton from '../button/PlusBtn.vue';
import { defineProps, defineEmits, ref } from 'vue';
const props = defineProps({
comment: {
@ -62,33 +68,11 @@ const toggleComment = () => {
};
//
const addChildComment = (newComment) => {
console.log(" 대댓글 추가됨:", newComment);
if (!props.comment.children) {
props.comment.children = []; //
}
props.comment.children.push(newComment); // children
const submitComment = (newComment) => {
emit('submitComment', { parentId: props.comment.id, ...newComment });
isComment.value = false;
};
</script>
<style scoped>
/* .twoDepth {
margin-top: 10px;
padding-left: 40px;
}
.list-unstyled > li ~ li {
margin-top: 10px;
}
.btn-text-primary {
padding-left: 0;
}
.btn-text-primary:hover,
.btn-text-primary:active,
.btn-text-primary:focus {
background-color: transparent
} */
</style>
</script>

View File

@ -3,11 +3,11 @@
<li
v-for="comment in comments"
:key="comment.id"
class="mt-8"
class="mt-6 border-bottom pb-6"
>
<BoardComment
:comment="comment"
@submitComment="addComment"
@submitComment="submitComment"
/>
</li>
</ul>
@ -25,10 +25,10 @@ const props = defineProps({
}
});
const emit = defineEmits(['reply']);
const emit = defineEmits(['submitComment']);
const addComment = (replyData) => {
emit('reply', replyData);
const submitComment = (replyData) => {
emit('submitComment', replyData);
};
</script>

View File

@ -67,13 +67,13 @@
</ul> -->
<!-- 댓글 입력 영역 -->
<BoardComentArea :profileName="profileName" :unknown="unknown" @submit="handleCommentSubmit"/>
<BoardComentArea :profileName="profileName" :unknown="unknown" @submitComment="handleCommentSubmit"/>
<!-- <BoardComentArea :profileName="profileName" :unknown="unknown" /> -->
</div>
<!-- 댓글 목록 -->
<div class="card-footer">
<BoardCommentList :comments="comments" />
<BoardCommentList :comments="comments" @submitComment="handleCommentReply"/>
<Pagination/>
</div>
@ -173,20 +173,54 @@ const handleUpdateReaction = async ({ boardId, commentId, isLike, isDislike }) =
}
};
//
const fetchComments = async () => {
try {
const response = await axios.get(`board/${currentBoardId.value}/comments`, {
params: { LOCBRDSEQ: currentBoardId.value }
});
console.log("📥 API 응답 데이터:", response.data);
let allComments = response.data.data.map(comment => ({
id: comment.LOCCMTSEQ, // id
parentId: comment.LOCCMTPNT, // id
author: comment.MEMBERSEQ || "익명 사용자", //
content: comment.LOCCMTRPY, //
createdAt: formattedDate(comment.LOCCMTRDT), //
children: []
}));
allComments.sort((a, b) => b.id - a.id);
let commentMap = {};
let rootComments = [];
allComments.forEach(comment => {
commentMap[comment.id] = comment; //
});
allComments.forEach(comment => {
if (comment.parentId && commentMap[comment.parentId]) {
// , children
commentMap[comment.parentId].children.push(comment);
} else {
// ID
rootComments.push(comment);
}
});
comments.value = rootComments;
// console.log(" comments :", comments.value);
} catch (error) {
console.error('댓글 목록 불러오기 오류:', error);
}
};
//
const handleCommentSubmit = async ({ comment, password }) => {
try {
// const payload = {
// LOCBRDSEQ: currentBoardId.value,
// LOCCMTRPY: comment,
// LOCCMTPWD: password || null,
// LOCCMTPNT: 1
// };
// alert(` : ${comment}`);
// console.log('📤 :', payload); //
// const response = await axios.post(`board/${currentBoardId.value}/comment`, payload);
try {
const response = await axios.post(`board/${currentBoardId.value}/comment`, {
LOCBRDSEQ: currentBoardId.value,
LOCCMTRPY: comment,
@ -207,35 +241,33 @@ const handleCommentSubmit = async ({ comment, password }) => {
}
};
//
const fetchComments = async () => {
try {
const response = await axios.get(`board/${currentBoardId.value}/comments`, {
params: { LOCBRDSEQ: currentBoardId.value }
const handleCommentReply = async (reply) => {
// console.log("BoardView ~~~~~~~~~~~~~~~~:", reply);
//
// const parentComment = comments.value.find(comment => comment.id === reply.parentId);
// console.log(parentComment)
// if (parentComment) {
// console.log('??')
const response = await axios.post(`board/${currentBoardId.value}/comment`, {
LOCBRDSEQ: currentBoardId.value,
LOCCMTRPY: reply.comment,
LOCCMTPWD: reply.password || null,
LOCCMTPNT: reply.parentId
});
// console.log("📥 API :", response.data);
comments.value = response.data.data.map(comment => ({
id: comment.LOCCMTSEQ, // ID
author: comment.MEMBERSEQ || "익명 사용자", //
content: comment.LOCCMTRPY, //
createdAt: formattedDate(comment.LOCCMTRDT), //
children: comment.children ? comment.children.map(child => ({
id: child.LOCCMTSEQ,
author: child.MEMBERSEQ || "익명 사용자",
content: child.LOCCMTRPY,
createdAt: formattedDate(child.LOCCMTRDT),
})) : []
}));
comments.value.sort((a, b) => b.id - a.id);
console.log("📌 변환된 comments 데이터:", comments.value);
} catch (error) {
console.error('❌ 댓글 목록 불러오기 오류:', error);
}
};
if (response.status === 200) {
console.log('대댓글 작성 성공!!!!!!:', response.data.message);
await fetchComments();
} else {
console.error('대댓글 작성 실패ㅜㅜㅜ:', response.data.message);
}
// } else {
// console.error(` : ${reply.parentId}`);
// }
}
//
const formattedDate = (dateString) => {