signal backend, app cleanup

This commit is contained in:
j 2023-07-24 12:05:45 +01:00
commit 6f18890739
43 changed files with 695 additions and 124 deletions

View file

@ -1,3 +1,48 @@
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 = `
<h3>
@ -11,6 +56,14 @@ function renderComments(cdiv, data) {
`
const content = cdiv.querySelector('.comments-content')
comments.forEach(comment => {
var extra = ''
if (!comment.published) {
if (user.is_moderator) {
extra += `<button class="publish-comment">${icon.publishComment}</button>`
} else {
extra += '(under review)'
}
}
var c = document.createElement('div')
c.className = 'comment'
c.innerHTML = `
@ -19,8 +72,18 @@ function renderComments(cdiv, data) {
by <span class="name">${comment.name}</span>
on
<span class="date">${comment.date}</span>
${extra}
</div>
`
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')