Skip to content

Commit

Permalink
Give details about sucessfully added and failed items to domains, gro…
Browse files Browse the repository at this point in the history
…ups, lists and clients

Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Oct 8, 2023
1 parent a6ba2a8 commit d516aec
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 13 deletions.
9 changes: 2 additions & 7 deletions scripts/pi-hole/js/groups-adlists.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,14 +525,9 @@ function addAdlist(event) {
method: "post",
dataType: "json",
data: JSON.stringify({ address: addresses, comment: comment, type: type }),
success: function () {
success: function (data) {
utils.enableAll();
utils.showAlert(
"success",
"fas fa-plus",
"Successfully added " + type + "list(s)",
addressestr
);
utils.listsAlert("list", addresses, data);
table.ajax.reload(null, false);
table.rows().deselect();

Expand Down
4 changes: 2 additions & 2 deletions scripts/pi-hole/js/groups-clients.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,9 @@ function addClient() {
method: "post",
dataType: "json",
data: JSON.stringify({ client: ips, comment: comment }),
success: function () {
success: function (data) {
utils.enableAll();
utils.showAlert("success", "fas fa-plus", "Successfully added client(s)", ipStr);
utils.listsAlert("client", ips, data);
reloadClientSuggestions();
table.ajax.reload(null, false);
table.rows().deselect();
Expand Down
4 changes: 2 additions & 2 deletions scripts/pi-hole/js/groups-domains.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,9 +541,9 @@ function addDomain() {
type: type,
kind: kind,
}),
success: function () {
success: function (data) {
utils.enableAll();
utils.showAlert("success", "fas fa-plus", "Successfully added domain(s)", domainStr);
utils.listsAlert("domain", domains, data);
table.ajax.reload(null, false);
table.rows().deselect();

Expand Down
4 changes: 2 additions & 2 deletions scripts/pi-hole/js/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,9 @@ function addGroup() {
comment: comment,
enabled: true,
}),
success: function () {
success: function (data) {
utils.enableAll();
utils.showAlert("success", "fas fa-plus", "Successfully added group(s)", groupStr);
utils.listsAlert("group", names, data);
$("#new_name").val("");
$("#new_comment").val("");
table.ajax.reload();
Expand Down
76 changes: 76 additions & 0 deletions scripts/pi-hole/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,81 @@ function hexDecode(string) {
return back;
}

function listAlert(type, items, data) {
// Show simple success message if there is no "processed" object in "data" or
// if all items were processed successfully
if (data.processed === undefined || data.processed.success.length === items.length) {
showAlert(
"success",
"fas fa-plus",
"Successfully added " + type + (items.length !== 1 ? "s" : ""),
items.join(", ")
);
return;
}

// Show a more detailed message if there is a "processed" object in "data" and
// not all items were processed successfully
let message = "";

// Show a list of successful items if there are any
if (data.processed.success.length > 0) {
message +=
"<strong>Successfully added " +
data.processed.success.length +
" " +
type +
(data.processed.success.length !== 1 ? "s" : "") +
":</strong>";

// Loop over data.processed.success and print "item"
for (const item in data.processed.success) {
if (Object.prototype.hasOwnProperty.call(data.processed.success, item)) {
message += "<br>- <strong>" + data.processed.success[item].item + "</strong>";
}
}
}

// Add a line break if there are both successful and failed items
if (data.processed.success.length > 0 && data.processed.errors.length > 0) {
message += "<br><br>";
}

// Show a list of failed items if there are any
if (data.processed.errors.length > 0) {
message +=
"<strong>Failed to add " +
data.processed.errors.length +
" " +
type +
(data.processed.errors.length !== 1 ? "s" : "") +
":</strong>\n";

// Loop over data.processed.errors and print "item: error"
for (const item in data.processed.errors) {
if (Object.prototype.hasOwnProperty.call(data.processed.errors, item)) {
let error = data.processed.errors[item].error;
if (error === "UNIQUE constraint failed: domainlist.domain, domainlist.type") {
// Replace the error message with a more user-friendly one
error = "Already present";
}

message += "<br>- <strong>" + data.processed.errors[item].item + "</strong>: " + error;
}
}
}

// Show the warning message
const total = data.processed.success.length + data.processed.errors.length;
const processed = "(" + total + " " + type + (total !== 1 ? "s" : "") + " processed)";
showAlert(
"warning",
"fas fa-exclamation-triangle",
"Some " + type + (items.length !== 1 ? "s" : "") + " could not be added " + processed,
message
);
}

window.utils = (function () {
return {
escapeHtml: escapeHtml,
Expand Down Expand Up @@ -569,5 +644,6 @@ window.utils = (function () {
parseQueryString: parseQueryString,
hexEncode: hexEncode,
hexDecode: hexDecode,
listsAlert: listAlert,
};
})();

0 comments on commit d516aec

Please sign in to comment.