From 4d3c0cded9a71685cb68af1d09f924a3d0f9ba3e Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sun, 5 Nov 2023 21:01:11 +0200 Subject: [PATCH] Fix errors happening when editing groups/clients/domains/lists not being shown. Also fix logic of the strings "enabling/disabling" as the variable "enabled" already has the new (not the current state). This was correct in groups.js, but not the others Signed-off-by: DL6ER --- groups.lp | 1 + scripts/pi-hole/js/groups-clients.js | 21 +++++++++++++-------- scripts/pi-hole/js/groups-common.js | 20 +++++++++++++++++++- scripts/pi-hole/js/groups-domains.js | 13 ++++--------- scripts/pi-hole/js/groups-lists.js | 16 +++++----------- scripts/pi-hole/js/groups.js | 11 ++++++----- scripts/pi-hole/js/utils.js | 3 ++- 7 files changed, 50 insertions(+), 35 deletions(-) diff --git a/groups.lp b/groups.lp index 7ff09c85..c77f093a 100644 --- a/groups.lp +++ b/groups.lp @@ -81,6 +81,7 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r') + diff --git a/scripts/pi-hole/js/groups-clients.js b/scripts/pi-hole/js/groups-clients.js index bb9eed94..a0d8faea 100644 --- a/scripts/pi-hole/js/groups-clients.js +++ b/scripts/pi-hole/js/groups-clients.js @@ -5,7 +5,7 @@ * This file is copyright under the latest version of the EUPL. * Please see LICENSE file for your rights under this license. */ -/* global utils:false, groups:false,, apiFailure:false, updateFtlInfo:false, getGroups:false */ +/* global utils:false, groups:false,, apiFailure:false, updateFtlInfo:false, getGroups:false, processGroupResult:false */ var table; @@ -489,6 +489,16 @@ function editClient() { var done = "edited"; var notDone = "editing"; switch (elem) { + case "enabled_" + client: + if (!enabled) { + done = "disabled"; + notDone = "disabling"; + } else { + done = "enabled"; + notDone = "enabling"; + } + + break; case "multiselect_" + client: done = "edited groups of"; notDone = "editing groups of"; @@ -516,14 +526,9 @@ function editClient() { comment: comment, enabled: enabled, }), - success: function () { + success: function (data) { utils.enableAll(); - utils.showAlert( - "success", - "fas fa-pencil-alt", - "Successfully " + done + " client", - clientDecoded - ); + processGroupResult(data, "client", done, notDone); table.ajax.reload(null, false); }, error: function (data, exception) { diff --git a/scripts/pi-hole/js/groups-common.js b/scripts/pi-hole/js/groups-common.js index 24153fda..a0de484e 100644 --- a/scripts/pi-hole/js/groups-common.js +++ b/scripts/pi-hole/js/groups-common.js @@ -5,7 +5,7 @@ * This file is copyright under the latest version of the EUPL. * Please see LICENSE file for your rights under this license. */ -/* global apiFailure:false */ +/* global apiFailure:false, utils:false */ // eslint-disable-next-line no-unused-vars var groups = []; @@ -24,3 +24,21 @@ function getGroups() { }, }); } + +// eslint-disable-next-line no-unused-vars +function processGroupResult(data, type, done, notDone) { + // Loop over data.processed.success and show toasts + data.processed.success.forEach(function (item) { + utils.showAlert("success", "fas fa-pencil-alt", `Successfully ${done} ${type}`, item); + }); + // Loop over errors and display them + data.processed.errors.forEach(function (error) { + console.log(error); // eslint-disable-line no-console + utils.showAlert( + "error", + "", + `Error while ${notDone} ${type} ${utils.escapeHtml(error.item)}`, + error.error + ); + }); +} diff --git a/scripts/pi-hole/js/groups-domains.js b/scripts/pi-hole/js/groups-domains.js index 719a7f81..cff049d3 100644 --- a/scripts/pi-hole/js/groups-domains.js +++ b/scripts/pi-hole/js/groups-domains.js @@ -5,7 +5,7 @@ * This file is copyright under the latest version of the EUPL. * Please see LICENSE file for your rights under this license. */ -/* global utils:false, groups:false,, getGroups:false, updateFtlInfo:false, apiFailure:false */ +/* global utils:false, groups:false,, getGroups:false, updateFtlInfo:false, apiFailure:false, processGroupResult:false */ var table; var GETDict = {}; @@ -581,7 +581,7 @@ function editDomain() { var notDone = "editing"; switch (elem) { case "enabled_" + domain: - if (enabled) { + if (!enabled) { done = "disabled"; notDone = "disabling"; } else { @@ -626,14 +626,9 @@ function editDomain() { type: oldType, kind: oldKind, }), - success: function () { + success: function (data) { utils.enableAll(); - utils.showAlert( - "success", - "fas fa-pencil-alt", - "Successfully " + done + " domain", - domainDecoded - ); + processGroupResult(data, "domain", done, notDone); table.ajax.reload(null, false); }, error: function (data, exception) { diff --git a/scripts/pi-hole/js/groups-lists.js b/scripts/pi-hole/js/groups-lists.js index ff85ec24..cf859a9c 100644 --- a/scripts/pi-hole/js/groups-lists.js +++ b/scripts/pi-hole/js/groups-lists.js @@ -5,7 +5,7 @@ * This file is copyright under the latest version of the EUPL. * Please see LICENSE file for your rights under this license. */ -/* global utils:false, groups:false, apiFailure:false, updateFtlInfo:false, getGroups:false */ +/* global utils:false, groups:false, apiFailure:false, updateFtlInfo:false, getGroups:false, processGroupResult:false */ var table; var GETDict = {}; @@ -549,20 +549,19 @@ function editList() { const tr = $(this).closest("tr"); const type = tr.attr("data-type"); const address = tr.attr("data-id"); - const status = tr.find("#enabled_" + address).is(":checked"); + const enabled = tr.find("#enabled_" + address).is(":checked"); const comment = utils.escapeHtml(tr.find("#comment_" + address).val()); // Convert list of string integers to list of integers using map(Number) const groups = tr .find("#multiselect_" + address) .val() .map(Number); - const enabled = tr.find("#enabled_" + address).is(":checked"); var done = "edited"; var notDone = "editing"; switch (elem) { case "enabled_" + address: - if (status) { + if (!enabled) { done = "disabled"; notDone = "disabling"; } else { @@ -598,14 +597,9 @@ function editList() { enabled: enabled, type: type, }), - success: function () { + success: function (data) { utils.enableAll(); - utils.showAlert( - "success", - "fas fa-pencil-alt", - "Successfully " + done + " list", - addressDecoded - ); + processGroupResult(data, "list", done, notDone); table.ajax.reload(null, false); }, error: function (data, exception) { diff --git a/scripts/pi-hole/js/groups.js b/scripts/pi-hole/js/groups.js index f05d52bf..63047a4c 100644 --- a/scripts/pi-hole/js/groups.js +++ b/scripts/pi-hole/js/groups.js @@ -5,7 +5,7 @@ * This file is copyright under the latest version of the EUPL. * Please see LICENSE file for your rights under this license. */ -/* global utils:false, apiFailure:false, updateFtlInfo:false */ +/* global utils:false, apiFailure:false, updateFtlInfo:false, processGroupResult:false */ var table, idNames = {}; @@ -344,10 +344,10 @@ function editGroup() { var notDone = "editing"; switch (elem) { case "enabled_" + id: - if (enabled === false) { + if (!enabled) { done = "disabled"; notDone = "disabling"; - } else if (enabled === true) { + } else { done = "enabled"; notDone = "enabling"; } @@ -378,9 +378,10 @@ function editGroup() { comment: comment, enabled: enabled, }), - success: function () { + success: function (data) { utils.enableAll(); - utils.showAlert("success", "fas fa-pencil-alt", "Successfully " + done + " group", oldName); + processGroupResult(data, "group", done, notDone); + table.ajax.reload(null, false); }, error: function (data, exception) { apiFailure(data); diff --git a/scripts/pi-hole/js/utils.js b/scripts/pi-hole/js/utils.js index 3d78928f..e37771e1 100644 --- a/scripts/pi-hole/js/utils.js +++ b/scripts/pi-hole/js/utils.js @@ -112,7 +112,8 @@ function showAlert(type, icon, title, message) { break; case "error": options.icon = "fas fa-times"; - options.title = " Error, something went wrong!
"; + if (title.length === 0) + options.title = " Error, something went wrong!
"; settings.delay *= 2; // If the message is an API object, nicely format the error message