2022-08-11 01:36:53 +03:00
|
|
|
import request from "./request.js";
|
2022-08-11 02:43:15 +03:00
|
|
|
|
2022-08-11 17:55:48 +03:00
|
|
|
const messages = document.getElementById("messages");
|
2022-08-24 23:10:29 +03:00
|
|
|
let messages_raw = [];
|
2022-08-11 17:55:48 +03:00
|
|
|
function renderMessage(message) {
|
|
|
|
const messageElement = document.createElement("div");
|
|
|
|
messageElement.classList.add("message");
|
|
|
|
messageElement.setAttribute("id", "message-" + message.id);
|
2022-08-11 02:43:15 +03:00
|
|
|
|
2022-08-11 17:55:48 +03:00
|
|
|
messageElement.innerHTML = `
|
|
|
|
|
|
|
|
<h3 style="float:right;">${new Date(message.time).toLocaleString()}</h3>
|
2022-08-11 02:43:15 +03:00
|
|
|
|
2022-08-11 17:55:48 +03:00
|
|
|
<h2>
|
|
|
|
<img class="circle" src=${message.author.avatar} alt=${message.author.name}>
|
|
|
|
<a href=${"/users/" + message.author.id}> ${message.author.name}</a>:
|
|
|
|
</h2>
|
2022-08-11 02:43:15 +03:00
|
|
|
|
2022-08-24 23:10:29 +03:00
|
|
|
<p>${message.content.replaceAll("\n", "<br>")}</p><br>
|
2022-08-11 17:55:48 +03:00
|
|
|
<div id="message-delete-${message.id}">
|
|
|
|
${!message.deleted ?
|
|
|
|
`<form style="display:inline;">
|
|
|
|
<button id="delete_message" value="${message.id}" type="submit">DELETE</button>
|
|
|
|
</form>` :
|
|
|
|
"<h3 style=\"display:inline;\">This message has been deleted</h3>"}
|
|
|
|
</div>
|
|
|
|
<div style="float: right;">
|
|
|
|
<h3 id="count${message.id}" style="display:inline;">${message.reactCount}</h3>
|
|
|
|
<button style="display:inline;" id="like" value="${message.id}">+🔼</button>
|
|
|
|
<button style="display:inline;" id="dislike" value="${message.id}" >-🔽</button>
|
|
|
|
</div>
|
|
|
|
`;
|
2022-08-11 02:43:15 +03:00
|
|
|
|
2022-08-11 17:55:48 +03:00
|
|
|
messages.appendChild(messageElement);
|
|
|
|
messages.innerHTML += "<br>";
|
|
|
|
};
|
2022-08-11 02:43:15 +03:00
|
|
|
|
2022-08-11 17:55:48 +03:00
|
|
|
/**
|
|
|
|
* Main Renderer
|
|
|
|
*/
|
|
|
|
(async () => {
|
|
|
|
|
2022-08-24 23:10:29 +03:00
|
|
|
messages_raw = await fetch(`/api/threads/${messages.getAttribute("value")}/messages/`).then(res => res.json());
|
|
|
|
if (messages_raw?.error) {
|
2022-08-11 02:43:15 +03:00
|
|
|
|
2022-08-11 17:55:48 +03:00
|
|
|
document.getElementById("messages").innerHTML
|
|
|
|
+= '<div class="message"><h1>THIS THREAD HAS NOT GOT ANY MESSAGE</h1></div>';
|
2022-08-11 01:36:53 +03:00
|
|
|
|
2022-08-11 17:55:48 +03:00
|
|
|
|
2022-08-24 23:10:29 +03:00
|
|
|
} else
|
|
|
|
for (const message of messages_raw)
|
2022-08-11 17:55:48 +03:00
|
|
|
renderMessage(message);
|
2022-08-24 23:10:29 +03:00
|
|
|
|
2022-08-11 18:15:27 +03:00
|
|
|
window.scrollTo(0, document.body.scrollHeight);
|
|
|
|
|
2022-08-11 17:55:48 +03:00
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Message Sender
|
|
|
|
*/
|
2022-08-24 22:27:23 +03:00
|
|
|
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-11 01:36:53 +03:00
|
|
|
if (!res) return;
|
2022-08-11 00:38:44 +03:00
|
|
|
form.reset();
|
2022-08-24 21:50:46 +03:00
|
|
|
res.reactCount = 0;
|
|
|
|
renderMessage(res);
|
2022-08-11 17:55:48 +03:00
|
|
|
});
|
|
|
|
});
|
2022-08-11 00:38:44 +03:00
|
|
|
|
|
|
|
|
2022-08-11 17:55:48 +03:00
|
|
|
/**
|
|
|
|
* Button Listener
|
|
|
|
*/
|
|
|
|
document.addEventListener("click", async e => {
|
2022-08-24 23:10:29 +03:00
|
|
|
let page = 1;
|
|
|
|
// e.preventDefault();
|
2022-08-11 17:55:48 +03:00
|
|
|
if (e.target.id === "delete_thread") {
|
|
|
|
const response = await request("/api/threads/" + e.target.value + "/delete");
|
2022-08-24 21:50:46 +03:00
|
|
|
if (response.deleted) {
|
2022-08-11 17:55:48 +03:00
|
|
|
alert("Thread deleted");
|
|
|
|
window.location.href = "/threads";
|
|
|
|
}
|
2022-08-11 00:38:44 +03:00
|
|
|
|
2022-08-11 17:55:48 +03:00
|
|
|
} else if (e.target.id === "delete_message") {
|
|
|
|
e.preventDefault();
|
|
|
|
const response = await request(`/api/messages/${e.target.value}/delete`);
|
2022-08-24 21:50:46 +03:00
|
|
|
if (response.deleted) {
|
2022-08-11 17:55:48 +03:00
|
|
|
alert("Message deleted");
|
2022-08-24 23:10:29 +03:00
|
|
|
document.getElementById("message-delete-" + e.target.value).innerHTML = "<h3 style=\"display:inline;\">This message has been deleted</h3>";
|
|
|
|
}
|
|
|
|
} else if (e.target.id === "left_page") {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
}else if (e.target.id === "right_page") {
|
|
|
|
e.preventDefault();
|
|
|
|
const response = await request(`/api/messages/${e.target.value}/delete`);
|
|
|
|
if (response.deleted) {
|
|
|
|
alert("Message deleted");
|
|
|
|
document.getElementById("message-delete-" + e.target.value).innerHTML = "<h3 style=\"display:inline;\">This message has been deleted</h3>";
|
2022-08-11 17:55:48 +03:00
|
|
|
}
|
2022-08-24 23:10:29 +03:00
|
|
|
}/*else if (e.target.id === "edit_thread") {
|
2022-08-11 17:55:48 +03:00
|
|
|
window.location.href = "/threads/<%= thread.id }/edit";
|
|
|
|
} */
|
2022-08-24 23:10:29 +03:00
|
|
|
|
2022-08-11 17:55:48 +03:00
|
|
|
if (!e.target.id.includes("like")) return;
|
|
|
|
const res = await request("/api/messages/" + e.target.value + "/react/" + e.target.id)
|
2022-08-11 00:38:44 +03:00
|
|
|
|
2022-08-24 21:50:46 +03:00
|
|
|
document.getElementById("count" + e.target.value).innerHTML = res.reactCount;
|
2022-08-11 00:38:44 +03:00
|
|
|
|
|
|
|
});
|