Skip to content

Commit

Permalink
Apply the same fixes for the messages table
Browse files Browse the repository at this point in the history
Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Jun 3, 2024
1 parent 420c5a8 commit edda66f
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions scripts/pi-hole/js/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* Please see LICENSE file for your rights under this license. */

/* global utils:false */
var table;
var table,
toasts = {};

$(function () {
var ignoreNonfatal = localStorage
Expand Down Expand Up @@ -147,26 +148,16 @@ $.fn.dataTable.Buttons.defaults.dom.container.className = "dt-buttons";

function deleteMessage() {
// Passes the button data-del-id attribute as ID
var ids = [parseInt($(this).attr("data-del-id"), 10)];

// Check input validity
if (!Array.isArray(ids)) return;

// Exploit prevention: Return early for non-numeric IDs
for (var id in ids) {
if (Object.hasOwnProperty.call(ids, id)) {
if (typeof ids[id] !== "number") return;
delMsg(ids);
}
}
delMsg($(this).attr("data-del-id"));
}

function delMsg(ids) {
function delMsg(id) {
id = parseInt(id, 10);
utils.disableAll();
utils.showAlert("info", "", "Deleting message...");
toasts[id] = utils.showAlert("info", "", "Deleting message...", null, null);

$.ajax({
url: "/api/info/messages/" + ids,
url: "/api/info/messages/" + id,
method: "DELETE",
})
.done(function (response) {
Expand All @@ -175,19 +166,21 @@ function delMsg(ids) {
utils.showAlert(
"success",
"far fa-trash-alt",
"Successfully deleted " + ids.length + " message" + (ids.length > 1 ? "s" : ""),
""
"Successfully deleted message",
"",
toasts[id]
);
// Loop over id in case of multiple IDs
for (var id in ids) {
if (Object.hasOwnProperty.call(ids, id)) {
table.row(id).remove();
}
}
table.row(id).remove();

table.draw(false).ajax.reload(null, false);
} else {
utils.showAlert("error", "", "Error while deleting message: " + ids, response.message);
utils.showAlert(
"error",
"",
"Error while deleting message: " + id,
response.message,
toasts[id]
);
}

// Clear selection after deletion
Expand All @@ -199,7 +192,13 @@ function delMsg(ids) {
)
.fail(function (jqXHR, exception) {
utils.enableAll();
utils.showAlert("error", "", "Error while deleting message: " + ids, jqXHR.responseText);
utils.showAlert(
"error",
"",
"Error while deleting message: " + id,
jqXHR.responseText,
toasts[id]
);
console.log(exception); // eslint-disable-line no-console
});
}

0 comments on commit edda66f

Please sign in to comment.