async function publish_comment(id) {
var csrf
document.querySelector('.add-comment').querySelectorAll('input,textarea').forEach(input => {
if (input.name == 'csrfmiddlewaretoken') {
csrf = input.value.trim()
}
})
var data = {
"comment": id
}
return fetch("/comment/publish/", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': csrf
},
body: JSON.stringify(data)
}).then(response => {
return response.json()
})
}
async function login(username, password) {
var csrf
document.querySelector('.add-comment').querySelectorAll('input,textarea').forEach(input => {
if (input.name == 'csrfmiddlewaretoken') {
csrf = input.value.trim()
}
})
var data = {
"username": username,
"password": password,
}
return fetch("/login/", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': csrf
},
body: JSON.stringify(data)
}).then(response => {
return response.json()
})
}
function renderComments(cdiv, data) {
cdiv.innerHTML = `
${icon.down}
Comments
`
const content = cdiv.querySelector('.comments-content')
comments.forEach(comment => {
var extra = ''
if (!comment.published) {
if (user.is_moderator) {
extra += ``
} else {
extra += '(under review)'
}
}
var c = document.createElement('div')
c.className = 'comment'
c.innerHTML = `
${comment.text}
by ${comment.name}
on
${comment.date}
${extra}
`
c.querySelectorAll('button.publish-comment').forEach(button => {
button.title = "click to publish"
button.addEventListener("click", event => {
button.disabled = true
publish_comment(comment.id).then(response => {
button.remove()
})
})
})
content.append(c)
})
var add = document.querySelector('.add-comment')
add.style.display = 'block'
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
}
})
}
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 => {
var comment = document.createElement('div')
comment.classList.add('comment')
comment.innerHTML = `
by
on
`
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)
document.querySelector('.add-comment textarea').value = ''
})
})