Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new POST :batchDelete callbacks #2893

Merged
merged 2 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 5 additions & 56 deletions scripts/pi-hole/js/groups-clients.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, processGroupResult:false */
/* global utils:false, groups:false,, apiFailure:false, updateFtlInfo:false, getGroups:false, processGroupResult:false, delGroupItems:false */
/* exported initTable */

var table;
Expand Down Expand Up @@ -277,10 +277,10 @@ function initTable() {
var ids = [];
$("tr.selected").each(function () {
// ... add the row identified by "data-id".
ids.push($(this).attr("data-id"));
ids.push({ item: $(this).attr("data-id") });
});
// Delete all selected rows at once
delItems(ids);
delGroupItems("client", ids, table);
},
},
],
Expand Down Expand Up @@ -347,59 +347,8 @@ $.fn.dataTable.Buttons.defaults.dom.container.className = "dt-buttons";

function deleteClient() {
// Passes the button data-id attribute as ID
const ids = [$(this).attr("data-id")];
delItems(ids);
}

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

// Get first element from array
const clientRaw = ids[0];
const client = utils.hexDecode(clientRaw);

// Remove first element from array
ids.shift();

utils.disableAll();
const idstring = ids.join(", ");
utils.showAlert("info", "", "Deleting client...", client);

$.ajax({
url: "/api/clients/" + encodeURIComponent(client),
method: "delete",
})
.done(function () {
utils.enableAll();
utils.showAlert("success", "far fa-trash-alt", "Successfully deleted client: ", client);
table.row(clientRaw).remove().draw(false);
if (ids.length > 0) {
// Recursively delete all remaining items
delItems(ids);
return;
}

table.ajax.reload(null, false);

// Clear selection after deletion
table.rows().deselect();
utils.changeBulkDeleteStates(table);

// Update number of clients in the sidebar
updateFtlInfo();
})
.fail(function (data, exception) {
apiFailure(data);
utils.enableAll();
utils.showAlert(
"error",
"",
"Error while deleting client(s): " + idstring,
data.responseText
);
console.log(exception); // eslint-disable-line no-console
});
const ids = [{ item: $(this).attr("data-id") }];
delGroupItems("client", ids, table);
}

function addClient() {
Expand Down
50 changes: 49 additions & 1 deletion scripts/pi-hole/js/groups-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, utils:false, initTable:false */
/* global apiFailure:false, utils:false, initTable:false, updateFtlInfo:false */

// eslint-disable-next-line no-unused-vars
var groups = [];
Expand Down Expand Up @@ -39,3 +39,51 @@ function processGroupResult(data, type, done, notDone) {
utils.showAlert("error", "", `Error while ${notDone} ${type} ${error.item}`, error.error);
});
}

// eslint-disable-next-line no-unused-vars
function delGroupItems(type, ids, table) {
// Check input validity
if (!Array.isArray(ids)) return;

const url = "/api/" + type + "s:batchDelete";

// use utils.hexDecode() to decode all clients
let idstring = "";
for (var i = 0; i < ids.length; i++) {
ids[i].item = utils.hexDecode(ids[i].item);
idstring += ids[i].item + ", ";
}

// Remove last comma and space from idstring
idstring = idstring.substring(0, idstring.length - 2);

// Append "s" to type if more than one item is deleted
type += ids.length > 1 ? "s" : "";

utils.disableAll();
utils.showAlert("info", "", "Deleting " + ids.length + " " + type + "...", idstring);

$.ajax({
url: url,
data: JSON.stringify(ids),
method: "POST",
})
.done(function () {
utils.enableAll();
utils.showAlert("success", "far fa-trash-alt", "Successfully deleted " + type, idstring);
table.ajax.reload(null, false);

// Clear selection after deletion
table.rows().deselect();
utils.changeBulkDeleteStates(table);

// Update number of <type> items in the sidebar
updateFtlInfo();
})
.fail(function (data, exception) {
apiFailure(data);
utils.enableAll();
utils.showAlert("error", "", "Error while deleting " + type, data.responseText);
console.log(exception); // eslint-disable-line no-console
});
}
68 changes: 15 additions & 53 deletions scripts/pi-hole/js/groups-domains.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, processGroupResult:false */
/* global utils:false, groups:false,, getGroups:false, updateFtlInfo:false, apiFailure:false, processGroupResult:false, delGroupItems:false */
/* exported initTable */

var table;
Expand Down Expand Up @@ -333,7 +333,7 @@ function initTable() {
ids.push($(this).attr("data-id"));
});
// Delete all selected rows at once
delItems(ids);
deleteDomains(ids);
},
},
],
Expand Down Expand Up @@ -430,60 +430,22 @@ $.fn.dataTable.Buttons.defaults.dom.container.className = "dt-buttons";

function deleteDomain() {
// Passes the button data-id attribute as ID
const ids = [$(this).attr("data-id")];
delItems(ids);
deleteDomains([$(this).attr("data-id")]);
}

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

// Get first element from array
const domainRaw = ids[0];
const domain = utils.hexDecode(domainRaw.split("_")[0]);
const typestr = $("#old_type_" + domainRaw).val();

// Remove first element from array
ids.shift();

utils.disableAll();
const idstring = ids.join(", ");
utils.showAlert("info", "", "Deleting domain...", domain);

$.ajax({
url: "/api/domains/" + typestr + "/" + encodeURIComponent(domain),
method: "delete",
})
.done(function () {
utils.enableAll();
utils.showAlert("success", "far fa-trash-alt", "Successfully deleted domain: ", domain);
table.row(domainRaw).remove().draw(false);
if (ids.length > 0) {
// Recursively delete all remaining items
delItems(ids);
return;
}

table.ajax.reload(null, false);

// Clear selection after deletion
table.rows().deselect();
utils.changeBulkDeleteStates(table);
function deleteDomains(encodedIds) {
const decodedIds = [];
for (let i = 0; i < encodedIds.length; i++) {
// Decode domain, type, and kind and add to array
const parts = encodedIds[i].split("_");
decodedIds[i] = {
item: parts[0],
type: parts[1],
kind: parts[2],
};
}

// Update number of lists in the sidebar
updateFtlInfo();
})
.fail(function (data, exception) {
apiFailure(data);
utils.enableAll();
utils.showAlert(
"error",
"",
"Error while deleting domain(s): " + idstring,
data.responseText
);
console.log(exception); // eslint-disable-line no-console
});
delGroupItems("domain", decodedIds, table);
}

function addDomain() {
Expand Down
56 changes: 5 additions & 51 deletions scripts/pi-hole/js/groups-lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, processGroupResult:false */
/* global utils:false, groups:false, apiFailure:false, updateFtlInfo:false, getGroups:false, processGroupResult:false, delGroupItems:false */
/* exported initTable */

var table;
Expand Down Expand Up @@ -397,10 +397,10 @@ function initTable() {
var ids = [];
$("tr.selected").each(function () {
// ... add the row identified by "data-id".
ids.push($(this).attr("data-id"));
ids.push({ item: $(this).attr("data-id") });
});
// Delete all selected rows at once
delItems(ids);
delGroupItems("list", ids, table);
},
},
],
Expand Down Expand Up @@ -485,54 +485,8 @@ $.fn.dataTable.Buttons.defaults.dom.container.className = "dt-buttons";

function deleteList() {
// Passes the button data-id attribute as ID
const ids = [$(this).attr("data-id")];
delItems(ids);
}

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

// Get first element from array
const addressRaw = ids[0];
const address = utils.hexDecode(addressRaw);

// Remove first element from array
ids.shift();

utils.disableAll();
const idstring = ids.join(", ");
utils.showAlert("info", "", "Deleting list(s) ...", address);

$.ajax({
url: "/api/lists/" + encodeURIComponent(address),
method: "delete",
})
.done(function () {
utils.enableAll();
utils.showAlert("success", "far fa-trash-alt", "Successfully deleted list: ", address);
table.row(addressRaw).remove().draw(false);
if (ids.length > 0) {
// Recursively delete all remaining items
delItems(ids);
return;
}

table.ajax.reload(null, false);

// Clear selection after deletion
table.rows().deselect();
utils.changeBulkDeleteStates(table);

// Update number of lists in the sidebar
updateFtlInfo();
})
.fail(function (data, exception) {
apiFailure(data);
utils.enableAll();
utils.showAlert("error", "", "Error while deleting list(s): " + idstring, data.responseText);
console.log(exception); // eslint-disable-line no-console
});
const ids = [{ item: $(this).attr("data-id") }];
delGroupItems("list", ids, table);
}

function addList(event) {
Expand Down
Loading