akf-forum/public/js/thread.js

52 lines
1.7 KiB
JavaScript
Raw Normal View History

2022-08-11 01:36:53 +03:00
import request from "./request.js";
2022-08-11 17:55:48 +03:00
/**
* Message Sender
*/
document.getElementById("send")?.addEventListener("submit", async e => {
2022-08-11 00:38:44 +03:00
e.preventDefault();
const form = e.target;
const data = new FormData(form);
2022-08-11 01:36:53 +03:00
request("/api/messages", "POST", { threadID: data.get("threadID"), content: data.get("content") })
2022-08-11 00:38:44 +03:00
.then(res => {
2022-08-29 16:16:44 +03:00
if (res) location.href = `/messages/${res.id}`;
2022-08-11 17:55:48 +03:00
});
});
2022-08-11 00:38:44 +03:00
2022-08-11 17:55:48 +03:00
/**
2022-08-27 14:08:28 +03:00
* OTHER FUNCTIONS
2022-08-11 17:55:48 +03:00
*/
2022-08-29 16:26:34 +03:00
window.thread = async function (id, un= "") {
await request(`/api/threads/${id}/${un}delete`);
alert(`Thread ${un}deleted`);
location.reload();
2022-08-27 14:08:28 +03:00
}
2022-08-29 16:16:44 +03:00
window.undelete_message = async function (id) {
2022-08-27 14:08:28 +03:00
const response = await request(`/api/messages/${id}/undelete`);
2022-08-29 16:16:44 +03:00
if (response.deleted) return;
document.getElementById("deleted-" + id).remove();
document.getElementById("dot-" + id).innerHTML = `
<a onclick="delete_message('${id}');">DELETE</a>
<a onclick="edit_message('${id}');">EDIT</a>
`
2022-08-27 14:08:28 +03:00
}
2022-08-29 16:16:44 +03:00
window.delete_message = async function (id) {
2022-08-27 14:08:28 +03:00
const response = await request(`/api/messages/${id}/delete`);
if (response.deleted) {
alert("Message deleted");
2022-08-29 16:16:44 +03:00
document.getElementById("dots-" + id).innerHTML = `
<i class='bx bx-trash bx-sm' id="deleted-${id}" style="color: RED;"></i>
`+ document.getElementById("dots-" + id).innerHTML;
2022-08-29 16:26:34 +03:00
document.getElementById("dot-" + id).innerHTML = `<a onclick="undelete_message('${id}');">UNDELETE</a>`;
2022-08-27 14:08:28 +03:00
}
}
2022-08-29 16:16:44 +03:00
window.react = async function (id, type) {
2022-08-27 14:08:28 +03:00
const res = await request(`/api/messages/${id}/react/${type}`)
2022-08-29 16:16:44 +03:00
document.getElementById(`like-${id}`).innerHTML = res.react.like.length;
document.getElementById(`dislike-${id}`).innerHTML = res.react.dislike.length;
2022-08-27 14:08:28 +03:00
}