조회, 등록
getReply.js
import { BASE_URL } from "./reply.js";
function getRelativeTime(createAt) {
const now = new Date();
const past = new Date(createAt);
const diff = now - past;
console.log(diff);
const seconds = Math.floor(diff / 1000);
const minutes = Math.floor(diff / 1000 / 60);
const hours = Math.floor(diff / 1000 / 60 / 60);
const days = Math.floor(diff / 1000 / 60 / 60 / 24);
const weeks = Math.floor(diff / 1000 / 60 / 60 / 24 / 7);
const years = Math.floor(diff / 1000 / 60 / 60 / 24 / 365);
if (seconds < 60) {
return '방금 전';
} else if (minutes < 60) {
return `${minutes}분 전`;
} else if (hours < 24) {
return `${hours}시간 전`;
} else if (days < 7) {
return `${days}일 전`;
} else if (weeks < 52) {
return `${weeks}주 전`;
} else {
return `${years}년 전`;
}
}
function renderPage({ begin, end, pageInfo, prev, next }) {
let tag = '';
if (prev) tag += `<li class='page-item'><a class='page-link page-active' href='${begin - 1}'>이전</a></li>`;
for (let i = begin; i <= end; i++) {
let active = '';
if (pageInfo.pageNo === i) active = 'p-active';
tag += `
<li class='page-item ${active}'>
<a class='page-link page-custom' href='${i}'>${i}</a>
</li>
`;
}
if (next) tag += `<li class='page-item'><a class='page-link page-active' href='${end + 1}'>다음</a></li>`;
const $pageUl = document.querySelector('.pagination');
$pageUl.innerHTML = tag;
}
export function renderReplies({ pageInfo, replies }) {
document.getElementById('replyCnt').textContent = pageInfo.totalCount;
let tag = '';
if (replies && replies.length > 0) {
replies.forEach(({ reply_no: rno, writer, text, createAt }) => {
tag += `
<div id='replyContent' class='card-body' data-reply-id='${rno}'>
<div class='row user-block'>
<span class='col-md-3'>
<b>${writer}</b>
</span>
<span class='offset-md-6 col-md-3 text-right'><b>${getRelativeTime(createAt)}</b></span>
</div><br>
<div class='row'>
<div class='col-md-9'>${text}</div>
<div class='col-md-3 text-right'>
<a id='replyModBtn' class='btn btn-sm btn-outline-dark' data-bs-toggle='modal' data-bs-target='#replyModifyModal'>수정</a>
<a id='replyDelBtn' class='btn btn-sm btn-outline-dark' href='#'>삭제</a>
</div>
</div>
</div>
`;
});
} else {
tag = `<div id='replyContent' class='card-body'>댓글이 아직 없습니다! ㅠㅠ</div>`;
}
document.getElementById('replyData').innerHTML = tag;
renderPage(pageInfo);
}
export async function fetchReplies(pageNo=1) {
const bno = document.getElementById('wrap').dataset.bno;
const res = await fetch(`${BASE_URL}/${bno}/page/${pageNo}`);
const replyResponse = await res.json();
renderReplies(replyResponse);
}
export function replyPageClickEvent() {
document.querySelector('.pagination').addEventListener('click', e => {
e.preventDefault();
fetchReplies(e.target.getAttribute('href'));
})
}
postReply.js
import { renderReplies } from "./getReply.js";
import { BASE_URL } from "./reply.js"
export const fetchReplyPost = async () => {
const textInput = document.getElementById('newReplyText');
const writerInput = document.getElementById('newReplyWriter');
const payload = {
text: textInput.value,
author: writerInput.value,
bno: document.getElementById("wrap").dataset.bno
}
console.log(payload);
const res = await fetch(`${BASE_URL}`, {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(payload)
});
const replies = await res.json();
textInput.value = '';
writerInput.value = '';
renderReplies(replies);
};
reply.js
import { fetchReplies, replyPageClickEvent } from "./getReply.js";
import { fetchReplyPost, DeleteReplyPost} from "./postReply.js";
export const BASE_URL = "http://localhost:8383/api/v1/replies";
fetchReplies();
document.getElementById('replyAddBtn').onclick = e => {
fetchReplyPost();
}
replyPageClickEvent();
document.getElementById('replyDelBtn').closest('row').onclick = e => {
DeleteReplyPost();
}