Give details about sucessfully added and failed items to domains, groups, lists and clients

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER
2023-10-08 10:55:28 +02:00
parent a6ba2a8eb8
commit d516aec5ce
5 changed files with 84 additions and 13 deletions

View File

@@ -525,14 +525,9 @@ function addAdlist(event) {
method: "post", method: "post",
dataType: "json", dataType: "json",
data: JSON.stringify({ address: addresses, comment: comment, type: type }), data: JSON.stringify({ address: addresses, comment: comment, type: type }),
success: function () { success: function (data) {
utils.enableAll(); utils.enableAll();
utils.showAlert( utils.listsAlert("list", addresses, data);
"success",
"fas fa-plus",
"Successfully added " + type + "list(s)",
addressestr
);
table.ajax.reload(null, false); table.ajax.reload(null, false);
table.rows().deselect(); table.rows().deselect();

View File

@@ -454,9 +454,9 @@ function addClient() {
method: "post", method: "post",
dataType: "json", dataType: "json",
data: JSON.stringify({ client: ips, comment: comment }), data: JSON.stringify({ client: ips, comment: comment }),
success: function () { success: function (data) {
utils.enableAll(); utils.enableAll();
utils.showAlert("success", "fas fa-plus", "Successfully added client(s)", ipStr); utils.listsAlert("client", ips, data);
reloadClientSuggestions(); reloadClientSuggestions();
table.ajax.reload(null, false); table.ajax.reload(null, false);
table.rows().deselect(); table.rows().deselect();

View File

@@ -541,9 +541,9 @@ function addDomain() {
type: type, type: type,
kind: kind, kind: kind,
}), }),
success: function () { success: function (data) {
utils.enableAll(); utils.enableAll();
utils.showAlert("success", "fas fa-plus", "Successfully added domain(s)", domainStr); utils.listsAlert("domain", domains, data);
table.ajax.reload(null, false); table.ajax.reload(null, false);
table.rows().deselect(); table.rows().deselect();

View File

@@ -310,9 +310,9 @@ function addGroup() {
comment: comment, comment: comment,
enabled: true, enabled: true,
}), }),
success: function () { success: function (data) {
utils.enableAll(); utils.enableAll();
utils.showAlert("success", "fas fa-plus", "Successfully added group(s)", groupStr); utils.listsAlert("group", names, data);
$("#new_name").val(""); $("#new_name").val("");
$("#new_comment").val(""); $("#new_comment").val("");
table.ajax.reload(); table.ajax.reload();

View File

@@ -536,6 +536,81 @@ function hexDecode(string) {
return back; 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 () { window.utils = (function () {
return { return {
escapeHtml: escapeHtml, escapeHtml: escapeHtml,
@@ -569,5 +644,6 @@ window.utils = (function () {
parseQueryString: parseQueryString, parseQueryString: parseQueryString,
hexEncode: hexEncode, hexEncode: hexEncode,
hexDecode: hexDecode, hexDecode: hexDecode,
listsAlert: listAlert,
}; };
})(); })();