let comments = [];
loadComments();
document.getElementById("comment-add").onclick = function () {
event.preventDefault();
let commentName = document.getElementById("comment-name");
let commentBody = document.getElementById("comment-body");
let comment = {
name: commentName.value,
body: commentBody.value,
time: Math.floor(Date.now()/1000)
};
commentName.value = "";
commentBody.value = "";
comments.push(comment);
saveComments();
showComments();
};
function saveComments() {
localStorage.setItem("comments", JSON.stringify(comments))
}
function loadComments() {
if (localStorage.getItem('comments')) comments =JSON.parse(localStorage.getItem('comments'));
showComments();
}
function showComments() {
let commentField =document.getElementById("comment-field")
let out = "";
comments.forEach(function (item) {
out += `<div class="comment-header"><p class="comment-name">${item.name}</p><p class="comment-time">${timeConverter(item.time)}</p></div>`;
out += `<div class="comment-text">${item.body}</div>`
});
commentField.innerHTML = out
}