phantasmobile/app/static/js/comments.js

81 lines
2.8 KiB
JavaScript
Raw Normal View History

2023-07-16 05:56:14 +00:00
function renderComments(cdiv, data) {
2023-07-15 07:30:36 +00:00
cdiv.innerHTML = `
<h3>
<span class="icon">${icon.down}</span>
Comments
</h3>
2023-07-16 05:56:14 +00:00
<div class="block">
<div class="comments-content">
</div>
</div>
2023-07-15 07:30:36 +00:00
`
2023-07-16 05:56:14 +00:00
const content = cdiv.querySelector('.comments-content')
2023-07-15 07:30:36 +00:00
comments.forEach(comment => {
var c = document.createElement('div')
c.className = 'comment'
c.innerHTML = `
2023-07-16 05:56:14 +00:00
<div class="text">${comment.text}</div>
<div class="meta">
by <span class="name">${comment.name}</span>
on
<span class="date">${comment.date}</span>
2023-07-15 07:30:36 +00:00
</div>
`
2023-07-16 05:56:14 +00:00
content.append(c)
2023-07-15 07:30:36 +00:00
})
var add = document.querySelector('.add-comment')
add.style.display = 'block'
2023-07-16 05:56:14 +00:00
cdiv.querySelector('.block').append(add)
cdiv.querySelector('h3').addEventListener("click", event => {
var img = cdiv.querySelector('h3 .icon')
if (cdiv.classList.contains("collapsed")) {
cdiv.classList.remove("collapsed")
img.innerHTML = icon.down
} else {
cdiv.classList.add("collapsed")
img.innerHTML = icon.right
}
})
2023-07-15 07:30:36 +00:00
}
document.querySelector('button#add-comment').addEventListener('click', event => {
var data = {}, csrf;
document.querySelector('.add-comment').querySelectorAll('input,textarea').forEach(input => {
if (input.name == 'csrfmiddlewaretoken') {
csrf = input.value.trim()
} else {
data[input.name] = input.value.trim()
if (!data[input.name]) {
delete data[input.name]
}
}
})
data.item = pandora.comment
fetch("/comment/", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': csrf
},
body: JSON.stringify(data)
}).then(response => {
return response.json()
}).then(response => {
2023-07-16 05:56:14 +00:00
var comment = document.createElement('div')
2023-07-15 07:30:36 +00:00
comment.classList.add('comment')
2023-07-16 05:56:14 +00:00
comment.innerHTML = `
<div class="text"></div>
<div class="meta">
by <span class="name"></span>
on
<span class="date"></span>
</div>
`
comment.querySelector('.name').innerText = response.name
comment.querySelector('.date').innerText = response.date
comment.querySelector('.text').innerText = response.text
document.querySelector('.comments .comments-content').append(comment)
2023-07-15 07:30:36 +00:00
document.querySelector('.add-comment textarea').value = ''
})
})