From e6dc3a02878627f569a6a6f4aaf1d6eac9c01d3b Mon Sep 17 00:00:00 2001 From: RD WebDesign Date: Mon, 16 May 2022 01:39:22 -0300 Subject: [PATCH 1/5] Using global `utils.showAlert` function Signed-off-by: RD WebDesign --- cname_records.php | 22 ++---------- dns_records.php | 22 ++---------- scripts/pi-hole/js/customcname.js | 56 +++++++++++------------------- scripts/pi-hole/js/customdns.js | 57 +++++++++++-------------------- 4 files changed, 46 insertions(+), 111 deletions(-) diff --git a/cname_records.php b/cname_records.php index a6a89ab1d..17b51b69f 100644 --- a/cname_records.php +++ b/cname_records.php @@ -1,12 +1,13 @@ - @@ -50,23 +51,6 @@ - - - - -
diff --git a/dns_records.php b/dns_records.php index 1782d1463..dcf9cf25e 100644 --- a/dns_records.php +++ b/dns_records.php @@ -1,12 +1,13 @@ - @@ -54,23 +55,6 @@
- - - - -
diff --git a/scripts/pi-hole/js/customcname.js b/scripts/pi-hole/js/customcname.js index 7645ebdc4..de0e1dab6 100644 --- a/scripts/pi-hole/js/customcname.js +++ b/scripts/pi-hole/js/customcname.js @@ -10,35 +10,6 @@ var table; var token = $("#token").text(); -function showAlert(type, message) { - var alertElement = null; - var messageElement = null; - - switch (type) { - case "info": - alertElement = $("#alInfo"); - break; - case "success": - alertElement = $("#alSuccess"); - break; - case "warning": - alertElement = $("#alWarning"); - messageElement = $("#warn"); - break; - case "error": - alertElement = $("#alFailure"); - messageElement = $("#err"); - break; - default: - return; - } - - if (messageElement !== null) messageElement.html(message); - - alertElement.fadeIn(200); - alertElement.delay(8000).fadeOut(2000); -} - $(function () { $("#btnAdd").on("click", addCustomCNAME); @@ -99,19 +70,26 @@ function addCustomCNAME() { var domain = utils.escapeHtml($("#domain").val()); var target = utils.escapeHtml($("#target").val()); - showAlert("info"); + utils.disableAll(); + utils.showAlert("info", "", "Adding custom CNAME record...", ""); + $.ajax({ url: "scripts/pi-hole/php/customcname.php", method: "post", dataType: "json", data: { action: "add", domain: domain, target: target, token: token }, success: function (response) { + utils.enableAll(); if (response.success) { - showAlert("success"); + utils.showAlert("success", "far fa-check-circle", "Custom CNAME added", domain + ": " + target); table.ajax.reload(); - } else showAlert("error", response.message); + } else { + utils.showAlert("error", "fas fa-times", "Failure! Something went wrong", response.message); + } }, error: function () { + utils.enableAll(); + utils.showAlert("error", "fas fa-times", "Error while adding custom CNAME record", ""); showAlert("error", "Error while adding this custom CNAME record"); }, }); @@ -121,20 +99,26 @@ function deleteCustomCNAME() { var domain = $(this).attr("data-domain"); var target = $(this).attr("data-target"); - showAlert("info"); + utils.disableAll(); + utils.showAlert("info", "", "Deleting custom CNAME record...", ""); + $.ajax({ url: "scripts/pi-hole/php/customcname.php", method: "post", dataType: "json", data: { action: "delete", domain: domain, target: target, token: token }, success: function (response) { + utils.enableAll(); if (response.success) { - showAlert("success"); + utils.showAlert("success", "far fa-check-circle", "Custom CNAME deleted", domain + ": " + target); table.ajax.reload(); - } else showAlert("error", response.message); + } else { + utils.showAlert("error", "fas fa-times", "Failure! Something went wrong", response.message); + } }, error: function (jqXHR, exception) { - showAlert("error", "Error while deleting this custom CNAME record"); + utils.enableAll(); + utils.showAlert("error", "fas fa-times", "Error while deleting custom CNAME record", ""); console.log(exception); // eslint-disable-line no-console }, }); diff --git a/scripts/pi-hole/js/customdns.js b/scripts/pi-hole/js/customdns.js index c0e1d9f77..5043520b5 100644 --- a/scripts/pi-hole/js/customdns.js +++ b/scripts/pi-hole/js/customdns.js @@ -10,35 +10,6 @@ var table; var token = $("#token").text(); -function showAlert(type, message) { - var alertElement = null; - var messageElement = null; - - switch (type) { - case "info": - alertElement = $("#alInfo"); - break; - case "success": - alertElement = $("#alSuccess"); - break; - case "warning": - alertElement = $("#alWarning"); - messageElement = $("#warn"); - break; - case "error": - alertElement = $("#alFailure"); - messageElement = $("#err"); - break; - default: - return; - } - - if (messageElement !== null) messageElement.html(message); - - alertElement.fadeIn(200); - alertElement.delay(8000).fadeOut(2000); -} - $(function () { $("#btnAdd").on("click", addCustomDNS); @@ -98,20 +69,26 @@ function addCustomDNS() { var ip = utils.escapeHtml($("#ip").val()); var domain = utils.escapeHtml($("#domain").val()); - showAlert("info"); + utils.disableAll(); + utils.showAlert("info", "", "Adding custom DNS entry...", ""); + $.ajax({ url: "scripts/pi-hole/php/customdns.php", method: "post", dataType: "json", data: { action: "add", ip: ip, domain: domain, token: token }, success: function (response) { + utils.enableAll(); if (response.success) { - showAlert("success"); + utils.showAlert("success", "far fa-check-circle", "Custom DNS added", domain + ": " + ip); table.ajax.reload(); - } else showAlert("error", response.message); + } else { + utils.showAlert("error", "fas fa-times", "Failure! Something went wrong", response.message); + } }, error: function () { - showAlert("error", "Error while adding this custom DNS entry"); + utils.enableAll(); + utils.showAlert("error", "fas fa-times", "Error while adding custom DNS entry", ""); }, }); } @@ -120,20 +97,26 @@ function deleteCustomDNS() { var ip = $(this).attr("data-ip"); var domain = $(this).attr("data-domain"); - showAlert("info"); + utils.disableAll(); + utils.showAlert("info", "", "Deleting custom DNS entry...", ""); + $.ajax({ url: "scripts/pi-hole/php/customdns.php", method: "post", dataType: "json", data: { action: "delete", domain: domain, ip: ip, token: token }, success: function (response) { + utils.enableAll(); if (response.success) { - showAlert("success"); + utils.showAlert("success", "far fa-check-circle", "Custom DNS deleted", domain + ": " + ip); table.ajax.reload(); - } else showAlert("error", response.message); + } else { + utils.showAlert("error", "fas fa-times", "Failure! Something went wrong", response.message); + } }, error: function (jqXHR, exception) { - showAlert("error", "Error while deleting this custom DNS entry"); + utils.enableAll(); + utils.showAlert("error", "fas fa-times", "Error while deleting custom DNS entry", ""); console.log(exception); // eslint-disable-line no-console }, }); From d0b9588fff250e2b5062e32acfabd2e412c57fb3 Mon Sep 17 00:00:00 2001 From: RD WebDesign Date: Mon, 16 May 2022 01:50:05 -0300 Subject: [PATCH 2/5] Fix prettier Signed-off-by: RD WebDesign --- scripts/pi-hole/js/customcname.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/scripts/pi-hole/js/customcname.js b/scripts/pi-hole/js/customcname.js index de0e1dab6..0248ab5c9 100644 --- a/scripts/pi-hole/js/customcname.js +++ b/scripts/pi-hole/js/customcname.js @@ -81,7 +81,12 @@ function addCustomCNAME() { success: function (response) { utils.enableAll(); if (response.success) { - utils.showAlert("success", "far fa-check-circle", "Custom CNAME added", domain + ": " + target); + utils.showAlert( + "success", + "far fa-check-circle", + "Custom CNAME added", + domain + ": " + target + ); table.ajax.reload(); } else { utils.showAlert("error", "fas fa-times", "Failure! Something went wrong", response.message); @@ -110,7 +115,12 @@ function deleteCustomCNAME() { success: function (response) { utils.enableAll(); if (response.success) { - utils.showAlert("success", "far fa-check-circle", "Custom CNAME deleted", domain + ": " + target); + utils.showAlert( + "success", + "far fa-check-circle", + "Custom CNAME deleted", + domain + ": " + target + ); table.ajax.reload(); } else { utils.showAlert("error", "fas fa-times", "Failure! Something went wrong", response.message); From 7cb2231bdd2a1d0dff44eca7998fdc64fc3db8b4 Mon Sep 17 00:00:00 2001 From: RD WebDesign Date: Mon, 16 May 2022 01:53:11 -0300 Subject: [PATCH 3/5] Removing old function call Signed-off-by: RD WebDesign --- scripts/pi-hole/js/customcname.js | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/pi-hole/js/customcname.js b/scripts/pi-hole/js/customcname.js index 0248ab5c9..09556695d 100644 --- a/scripts/pi-hole/js/customcname.js +++ b/scripts/pi-hole/js/customcname.js @@ -95,7 +95,6 @@ function addCustomCNAME() { error: function () { utils.enableAll(); utils.showAlert("error", "fas fa-times", "Error while adding custom CNAME record", ""); - showAlert("error", "Error while adding this custom CNAME record"); }, }); } From d12ec5f88537e8e8724208eddc6929df59cb3a3f Mon Sep 17 00:00:00 2001 From: RD WebDesign Date: Tue, 17 May 2022 15:54:01 -0300 Subject: [PATCH 4/5] Clean text fields after success. Signed-off-by: RD WebDesign --- scripts/pi-hole/js/customcname.js | 8 ++++++++ scripts/pi-hole/js/customdns.js | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/scripts/pi-hole/js/customcname.js b/scripts/pi-hole/js/customcname.js index 09556695d..e78e4a7ea 100644 --- a/scripts/pi-hole/js/customcname.js +++ b/scripts/pi-hole/js/customcname.js @@ -87,6 +87,10 @@ function addCustomCNAME() { "Custom CNAME added", domain + ": " + target ); + + // Clean up field values and reload table data + $("#domain").val(""); + $("#target").val(""); table.ajax.reload(); } else { utils.showAlert("error", "fas fa-times", "Failure! Something went wrong", response.message); @@ -120,6 +124,10 @@ function deleteCustomCNAME() { "Custom CNAME deleted", domain + ": " + target ); + + // Clean up field values and reload table data + $("#domain").val(""); + $("#target").val(""); table.ajax.reload(); } else { utils.showAlert("error", "fas fa-times", "Failure! Something went wrong", response.message); diff --git a/scripts/pi-hole/js/customdns.js b/scripts/pi-hole/js/customdns.js index 5043520b5..865f54b02 100644 --- a/scripts/pi-hole/js/customdns.js +++ b/scripts/pi-hole/js/customdns.js @@ -81,6 +81,10 @@ function addCustomDNS() { utils.enableAll(); if (response.success) { utils.showAlert("success", "far fa-check-circle", "Custom DNS added", domain + ": " + ip); + + // Clean up field values and reload table data + $("#domain").val(""); + $("#id").val(""); table.ajax.reload(); } else { utils.showAlert("error", "fas fa-times", "Failure! Something went wrong", response.message); @@ -109,6 +113,10 @@ function deleteCustomDNS() { utils.enableAll(); if (response.success) { utils.showAlert("success", "far fa-check-circle", "Custom DNS deleted", domain + ": " + ip); + + // Clean up field values and reload table data + $("#domain").val(""); + $("#id").val(""); table.ajax.reload(); } else { utils.showAlert("error", "fas fa-times", "Failure! Something went wrong", response.message); From e9f638a888752ae6612ea32258b4542dcb40f2a2 Mon Sep 17 00:00:00 2001 From: RD WebDesign Date: Tue, 17 May 2022 16:33:44 -0300 Subject: [PATCH 5/5] Keep field values when deleting + Fix typo Signed-off-by: RD WebDesign --- scripts/pi-hole/js/customcname.js | 4 ---- scripts/pi-hole/js/customdns.js | 6 +----- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/scripts/pi-hole/js/customcname.js b/scripts/pi-hole/js/customcname.js index e78e4a7ea..e0f317602 100644 --- a/scripts/pi-hole/js/customcname.js +++ b/scripts/pi-hole/js/customcname.js @@ -124,10 +124,6 @@ function deleteCustomCNAME() { "Custom CNAME deleted", domain + ": " + target ); - - // Clean up field values and reload table data - $("#domain").val(""); - $("#target").val(""); table.ajax.reload(); } else { utils.showAlert("error", "fas fa-times", "Failure! Something went wrong", response.message); diff --git a/scripts/pi-hole/js/customdns.js b/scripts/pi-hole/js/customdns.js index 865f54b02..b87828835 100644 --- a/scripts/pi-hole/js/customdns.js +++ b/scripts/pi-hole/js/customdns.js @@ -84,7 +84,7 @@ function addCustomDNS() { // Clean up field values and reload table data $("#domain").val(""); - $("#id").val(""); + $("#ip").val(""); table.ajax.reload(); } else { utils.showAlert("error", "fas fa-times", "Failure! Something went wrong", response.message); @@ -113,10 +113,6 @@ function deleteCustomDNS() { utils.enableAll(); if (response.success) { utils.showAlert("success", "far fa-check-circle", "Custom DNS deleted", domain + ": " + ip); - - // Clean up field values and reload table data - $("#domain").val(""); - $("#id").val(""); table.ajax.reload(); } else { utils.showAlert("error", "fas fa-times", "Failure! Something went wrong", response.message);