stop blank messages from being sent #5

Closed
carsand101 wants to merge 3 commits from main into main
2 changed files with 846 additions and 841 deletions

1504
main

File diff suppressed because it is too large Load Diff

View File

@ -1,90 +1,95 @@
let channelDiv = document.getElementById("channelDiv") let channelDiv = document.getElementById("channelDiv")
let messageDiv = document.getElementById("messageDiv") let messageDiv = document.getElementById("messageDiv")
let messageBox = document.getElementById("messageBox") let messageBox = document.getElementById("messageBox")
let statusMessage = document.getElementById("statusMessage") let statusMessage = document.getElementById("statusMessage")
let channelID = 0 let channelID = 0
async function updateMessages(id) { async function updateMessages(id) {
try { try {
let response = await fetch("/api/chat/getmessages/" + id); let response = await fetch("/api/chat/getmessages/" + id);
let messages = await response.json() let messages = await response.json()
statusMessage.innerText = "" statusMessage.innerText = ""
document.querySelectorAll(".messageParagraph").forEach(el => el.remove()); document.querySelectorAll(".messageParagraph").forEach(el => el.remove());
for (let i in messages) { for (let i in messages) {
let messageParagraph = document.createElement("p") let messageParagraph = document.createElement("p")
messageParagraph.appendChild(document.createTextNode(messages[i]["creator"]["username"] + ": " + messages[i]["content"])) messageParagraph.appendChild(document.createTextNode(messages[i]["creator"]["username"] + ": " + messages[i]["content"]))
messageParagraph.classList.add("messageParagraph") messageParagraph.classList.add("messageParagraph")
messageParagraph.id = "messageParagraph" + messages[i]["id"] messageParagraph.id = "messageParagraph" + messages[i]["id"]
messageDiv.append(messageParagraph) messageDiv.append(messageParagraph)
} }
} }
catch { catch {
statusMessage.innerText = "Not connected" statusMessage.innerText = "Not connected"
} }
} }
function selectChannel(id) { function selectChannel(id) {
channelID = id channelID = id
let selectedButton = channelDiv.querySelector(".selected"); let selectedButton = channelDiv.querySelector(".selected");
if (selectedButton) { if (selectedButton) {
selectedButton.classList.remove("selected"); selectedButton.classList.remove("selected");
} }
let channelButton = document.getElementById("channelButton" + id) let channelButton = document.getElementById("channelButton" + id)
if (channelButton) { if (channelButton) {
channelButton.classList.add("selected") channelButton.classList.add("selected")
} }
else { else {
console.log("channelButton not found") console.log("channelButton not found")
} }
updateMessages(id) updateMessages(id)
} }
async function updateRooms() { async function updateRooms() {
let response = await fetch("/api/chat/listrooms"); let response = await fetch("/api/chat/listrooms");
let rooms = await response.json() let rooms = await response.json()
for (let i in rooms) { for (let i in rooms) {
let channelButton = document.createElement("button") let channelButton = document.createElement("button")
channelButton.appendChild(document.createTextNode(rooms[i]["name"])) channelButton.appendChild(document.createTextNode(rooms[i]["name"]))
channelButton.id = "channelButton" + rooms[i]["id"] channelButton.id = "channelButton" + rooms[i]["id"]
channelButton.onclick = function () { selectChannel(rooms[i]["id"]) } channelButton.onclick = function () { selectChannel(rooms[i]["id"]) }
channelDiv.append(channelButton) channelDiv.append(channelButton)
} }
selectChannel(1) selectChannel(1)
} }
async function sendMessage(content, id) { async function sendMessage(content, id) {
fetch("/api/chat/send/" + String(id), { fetch("/api/chat/send/" + String(id), {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
content: content content: content
}), }),
headers: { headers: {
"Content-Type": "application/json" "Content-Type": "application/json"
} }
}) })
} }
messageBox.addEventListener("keyup", function onEvent(event) { messageBox.addEventListener("keyup", function onEvent(event) {
if (event.key === "Enter") { if (event.key === "Enter") {
sendMessage(messageBox.value, channelID) if (!messageBox.value.trim().length == 0) {
updateMessages(channelID) sendMessage(messageBox.value, channelID)
messageBox.value = "" updateMessages(channelID)
} messageBox.value = ""
}) }
else {
function update() { return;
updateMessages(channelID) }
}
setTimeout(update, 1500); })
}
function update() {
window.addEventListener("load", function () { updateMessages(channelID)
updateRooms()
update() setTimeout(update, 1500);
}
window.addEventListener("load", function () {
updateRooms()
update()
}) })