akf-forum/public/js/thread.js

60 lines
2.1 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
2022-08-29 19:31:59 +03:00
window.edit_t = async function (id) {
const title = prompt("Enter new title!");
const res = await request(`/api/threads/${id}/edit`, "PATCH", { title });
2022-08-29 19:31:59 +03:00
if (res.error) return;
alert(`Thread updated`);
document.getElementById("title").innerHTML = title;
}
window.delete_thread = async function (id) {
const res = await request(`/api/threads/${id}/`, "DELETE");
if (res.error) return;
2022-08-29 19:31:59 +03:00
alert(`Thread deleted`);
location.reload();
}
2022-08-29 19:31:59 +03:00
window.undelete_thread = async function (id) {
const res = await request(`/api/threads/${id}/undelete`);
2022-08-29 19:31:59 +03:00
if (res.error) return;
alert(`Thread undeleted`);
2022-08-29 16:26:34 +03:00
location.reload();
2022-08-27 14:08:28 +03:00
}
2022-08-29 19:31:59 +03:00
window.edit_message = async function (id) {
const content = prompt("Enter new content!");
const res = await request(`/api/messages/${id}/`, "PATCH", { content });
2022-08-29 19:31:59 +03:00
if (res.error) return;
alert(`Message updated`);
document.getElementById("message-" + id).querySelector(".content").innerHTML = content;
}
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>
2022-08-29 19:31:59 +03:00
<a onclick="edit_message('${id}');">EDIT</a>`
2022-08-27 14:08:28 +03:00
}
2022-08-29 19:31:59 +03:00
2022-08-29 16:16:44 +03:00
window.delete_message = async function (id) {
const response = await request(`/api/messages/${id}/`,"DELETE");
2022-08-27 14:08:28 +03:00
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 19:31:59 +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
}