
https://wootaepark.github.io/first-project-team3/
(배포 URL이 언제까지 살아있을진 미지수)





data-target을 이용하기 위해서 id 값도 부여를 함jQuery를 이용하여 열기 기능, 닫기 기능을 구현함jQuery 와 이전에 말한 data-target 을 이용하여 target 을 설정하고 그 값에 fadeIn(), fadeOut() 함수를 이용하여 해당 팝업을 보여주거나 숨길 수 있도록 구현하였다. // 팝업 열기 및 닫기 기능
$(document).ready(function () {
// 팝업 열기 버튼 클릭 시 해당 팝업 열기
$(".open-popup").click(function () {
var targetPopup = $(this).data("target"); // data-target 속성에서 타겟 팝업 ID를 가져옴
$("#" + targetPopup).fadeIn(); // 해당 팝업을 보여줌
});
// 팝업 닫기 버튼 클릭 시 해당 팝업 닫기
$(".close-popup").click(function () {
var targetPopup = $(this).data("target"); // data-target 속성에서 타겟 팝업 ID를 가져옴
$("#" + targetPopup).fadeOut(); // 해당 팝업을 숨김
});
});


firebase-firestore 로부터 addDoc, getDocs, deleteDoc 함수를 import 해와서 각각 댓글 생성, 댓글 조회, 댓글 삭제 기능을 구현함.confirm 함수를 이용하여 정말로 삭제 할 것인지 (확인, 취소) 버튼을 두어 사용자의 선택에 따라 결과가 정해지도록 하였다.firebase-firestore 에서 제공하는 query, orderBy 를 통해 사람들의 댓글이 작성자의 이름의 오름차순으로 정렬 되도록 구현하였다.
$("#postingbtn").click(async function () { // 데이터 삽입
let name = $('#name').val();
let comment = $('#comment').val();
if (!name || !comment) {
alert('작성자와 내용을 모두 적어주세요!');
return;
}
let doc = {
'name': name,
'comment': comment,
}; // 넣고 싶은 데이터
await addDoc(collection(db, "comments"), doc);
alert('작성 되었습니다.');
window.location.reload();
})
$(document).ready(async function () {
const q = query(collection(db, "comments"), orderBy("name", "asc"));
const querySnapshot = await getDocs(q);
$('#comments').empty();
let commentCount = 0;
querySnapshot.forEach((doc) => {
commentCount++;
let row = doc.data();
let name = row['name'];
let comment = row['comment'];
let docId = doc.id;
let temp_html = `
<tr id="comment-${docId}">
<td>${name}</td>
<td id="comment-area">${comment}
</td>
<td class="action-column"><button class="btn btn-danger" data-id="${docId}" id="delete-btn">삭제</button></td>
</tr>
`;
$('#comments').append(temp_html);
});
if (commentCount === 0) {
$('table').hide();
} else {
$('table').show();
} // 테이블에 내용이 없는 경우 테이블 모양 숨기기
});
// 삭제 기능 구현
$(document).on('click', '#delete-btn', async function () {
let answer = confirm('정말로 삭제하시겠습니까?');
if (answer) {
let docId = $(this).data('id');
await deleteDoc(doc(db, "comments", docId));
$(`comment-${docId}`).remove();
alert('삭제되었습니다.');
window.location.reload();
}
else {
alert('삭제를 취소합니다.');
}
});
