use filter to search for relevant messages

This commit is contained in:
simon 2021-12-05 20:26:17 +07:00
parent 67bde4c7ec
commit 5b47c9cde7
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4

View File

@ -39,17 +39,18 @@ function getMessages(dataOrigin) {
// make div for all messages, return relevant // make div for all messages, return relevant
function buildMessage(responseData, dataOrigin) { function buildMessage(responseData, dataOrigin) {
var messages = responseData["messages"]; // filter relevan messages
var notificationDiv = document.getElementById("notifications") var allMessages = responseData["messages"];
var messages = allMessages.filter(function(value) {
return messageTypes[dataOrigin].includes(value["status"])
}, dataOrigin);
// build divs
var notificationDiv = document.getElementById("notifications");
var nots = notificationDiv.childElementCount; var nots = notificationDiv.childElementCount;
notificationDiv.innerHTML = ""; notificationDiv.innerHTML = "";
for (let i = 0; i < messages.length; i++) { for (let i = 0; i < messages.length; i++) {
var messageData = messages[i]; var messageData = messages[i];
var messageStatus = messageData["status"]; var messageStatus = messageData["status"];
if (! messageTypes[dataOrigin].includes(messageStatus)) {
messages.splice(i);
continue
};
var messageBox = document.createElement("div"); var messageBox = document.createElement("div");
var title = document.createElement("h3"); var title = document.createElement("h3");
title.innerHTML = messageData["title"]; title.innerHTML = messageData["title"];
@ -63,8 +64,8 @@ function buildMessage(responseData, dataOrigin) {
checkDownloadIcons(); checkDownloadIcons();
}; };
}; };
// reload on download page when no more notifications // reload page when no more notifications
if (nots > 0 && messages.length === 0 && dataOrigin === "download") { if (nots > 0 && messages.length === 0) {
location.reload(); location.reload();
}; };
return messages return messages