From 87ee45f0b11aa5b820ddba1eec77beddc1ccdc63 Mon Sep 17 00:00:00 2001 From: Katharina Bogad Date: Sun, 20 Jun 2021 11:10:38 +0200 Subject: [PATCH] viewjs: Move delete confirmation box to frontendhelper More code deduplication. --- js/helpers/frontend.js | 85 +++++++++++++++++++++++++++++++--- js/viewjs/choresoverview.js | 46 ++++-------------- js/viewjs/equipment.js | 44 ++++-------------- js/viewjs/locations.js | 44 ++++-------------- js/viewjs/manageapikeys.js | 44 ++++-------------- js/viewjs/productgroups.js | 44 ++++-------------- js/viewjs/products.js | 52 ++++++--------------- js/viewjs/quantityunits.js | 45 ++++-------------- js/viewjs/recipes.js | 46 ++++-------------- js/viewjs/shoppinglocations.js | 44 ++++-------------- js/viewjs/taskcategories.js | 44 ++++-------------- js/viewjs/tasks.js | 55 ++++++---------------- js/viewjs/userentities.js | 44 ++++-------------- js/viewjs/userfields.js | 44 ++++-------------- js/viewjs/userobjects.js | 43 ++++------------- js/viewjs/users.js | 44 ++++-------------- 16 files changed, 210 insertions(+), 558 deletions(-) diff --git a/js/helpers/frontend.js b/js/helpers/frontend.js index ba59b6c7..aea7c43b 100644 --- a/js/helpers/frontend.js +++ b/js/helpers/frontend.js @@ -198,7 +198,11 @@ class GrocyFrontendHelpers } MakeStatusFilter(dataTable, column) { - $("#status-filter").on("change", function() + return this.MakeValueFilter("status", column, dataTable) + } + MakeValueFilter(key, column, dataTable, resetValue = "all") + { + $("#" + key + "-filter").on("change", function() { var value = $(this).val(); if (value === "all") @@ -212,17 +216,84 @@ class GrocyFrontendHelpers dataTable.column(column).search(value).draw(); }); - $(".status-filter-message").on("click", function() + $("." + key + "-filter-message").on("click", function() { - var value = $(this).data("status-filter"); - $("#status-filter").val(value); - $("#status-filter").trigger("change"); + var value = $(this).data(key + "-filter"); + $("#" + key + "-filter").val(value); + $("#" + key + "-filter").trigger("change"); }); $("#clear-filter-button").on("click", function() { - $("#status-filter").val("all"); - $("#status-filter").trigger("change"); + $("#" + key + "-filter").val(resetValue); + $("#" + key + "-filter").trigger("change"); + }); + } + + MakeDeleteConfirmBox(message, selector, attrName, attrId, apiEndpoint, redirectUrl) + { + if (!apiEndpoint.endsWith('/')) + { + apiEndpoint += '/'; + } + if (redirectUrl instanceof String && !redirectUrl.startsWith('/')) + { + redirectUrl = '/' + redirectUrl; + } + + var self = this; + $(document).on('click', selector, function(e) + { + var target = $(e.currentTarget); + var objectName = target.attr(attrName); + var objectId = target.attr(attrId); + + if (message instanceof Function) + { + message = message(objectId, objectName); + } + else + { + message = self.Grocy.translate(message, objectName) + } + + bootbox.confirm({ + message: message, + closeButton: false, + buttons: { + confirm: { + label: self.Grocy.translate('Yes'), + className: 'btn-success' + }, + cancel: { + label: self.Grocy.translate('No'), + className: 'btn-danger' + } + }, + callback: function(result) + { + if (result === true) + { + self.Api.Delete(apiEndpoint + objectId, {}, + function(result) + { + if (redirectUrl instanceof Function) + { + redirectUrl(result, objectId, objectName); + } + else + { + window.location.href = self.Grocy.FormatUrl(redirectUrl); + } + }, + function(xhr) + { + console.error(xhr); + } + ); + } + } + }); }); } } diff --git a/js/viewjs/choresoverview.js b/js/viewjs/choresoverview.js index 409d75b5..99f124ea 100644 --- a/js/viewjs/choresoverview.js +++ b/js/viewjs/choresoverview.js @@ -9,49 +9,21 @@ ].concat($.fn.dataTable.defaults.columnDefs) }); $('#chores-overview-table tbody').removeClass("d-none"); -Grocy.FrontendHelpers.InitDataTable(choresOverviewTable, null, function() -{ - $("#search").val(""); - $("#user-filter").val("all"); - choresOverviewTable.column(6).search("").draw(); - choresOverviewTable.search("").draw(); - RemoveUriParam("user"); -}); - - -Grocy.FrontendHelpers.MakeFilterForColumn("#status-filter", 5, choresOverviewTable, null, true); +Grocy.FrontendHelpers.InitDataTable(choresOverviewTable); +Grocy.FrontendHelpers.MakeValueFilter("status", 5, choresOverviewTable); +Grocy.FrontendHelpers.MakeValueFilter("user", 6, choresOverviewTable, ""); $("#user-filter").on("change", function() { - var value = $(this).val(); - if (value === "all") - { - value = ""; - } - - // Transfer CSS classes of selected element to dropdown element (for background) - $(this).attr("class", $("#" + $(this).attr("id") + " option[value='" + value + "']").attr("class") + " form-control"); - - choresOverviewTable.column(6).search(value).draw(); - - if (!value.isEmpty()) + var user = $(this).val(); + if (user !== null && !user.isEmpty()) { UpdateUriParam("user", $("#user-filter option:selected").data("user-id")); } -}); - -$(".status-filter-message").on("click", function() -{ - var value = $(this).data("status-filter"); - $("#status-filter").val(value); - $("#status-filter").trigger("change"); -}); - -$(".user-filter-message").on("click", function() -{ - var value = $(this).data("user-filter"); - $("#user-filter").val(value); - $("#user-filter").trigger("change"); + else + { + RemoveUriParam("user") + } }); $(document).on('click', '.track-chore-button', function(e) diff --git a/js/viewjs/equipment.js b/js/viewjs/equipment.js index d2066c92..a069affa 100644 --- a/js/viewjs/equipment.js +++ b/js/viewjs/equipment.js @@ -65,42 +65,14 @@ function DisplayEquipment(id) ); } -$(document).on('click', '.equipment-delete-button', function(e) -{ - var objectName = $(e.currentTarget).attr('data-equipment-name'); - var objectId = $(e.currentTarget).attr('data-equipment-id'); - - bootbox.confirm({ - message: __t('Are you sure to delete equipment "%s"?', objectName), - closeButton: false, - buttons: { - confirm: { - label: __t('Yes'), - className: 'btn-success' - }, - cancel: { - label: __t('No'), - className: 'btn-danger' - } - }, - callback: function(result) - { - if (result === true) - { - Grocy.Api.Delete('objects/equipment/' + objectId, {}, - function(result) - { - window.location.href = U('/equipment'); - }, - function(xhr) - { - console.error(xhr); - } - ); - } - } - }); -}); +Grocy.FrontendHelpers.MakeDeleteConfirmBox( + 'Are you sure to delete equipment "%s"?', + '.equipment-delete-button', + 'data-equipment-name', + 'data-equipment-id', + 'objects/equipment/', + '/equipment' +); $("#selectedEquipmentInstructionManualToggleFullscreenButton").on('click', function(e) { diff --git a/js/viewjs/locations.js b/js/viewjs/locations.js index c214f398..c17299b1 100644 --- a/js/viewjs/locations.js +++ b/js/viewjs/locations.js @@ -8,39 +8,11 @@ $('#locations-table tbody').removeClass("d-none"); Grocy.FrontendHelpers.InitDataTable(locationsTable); -$(document).on('click', '.location-delete-button', function(e) -{ - var objectName = $(e.currentTarget).attr('data-location-name'); - var objectId = $(e.currentTarget).attr('data-location-id'); - - bootbox.confirm({ - message: __t('Are you sure to delete location "%s"?', objectName), - closeButton: false, - buttons: { - confirm: { - label: __t('Yes'), - className: 'btn-success' - }, - cancel: { - label: __t('No'), - className: 'btn-danger' - } - }, - callback: function(result) - { - if (result === true) - { - Grocy.Api.Delete('objects/locations/' + objectId, {}, - function(result) - { - window.location.href = U('/locations'); - }, - function(xhr) - { - console.error(xhr); - } - ); - } - } - }); -}); +Grocy.FrontendHelpers.MakeDeleteConfirmBox( + 'Are you sure to delete location "%s"?', + '.location-delete-button', + 'data-location-name', + 'data-location-id', + 'objects/locations/', + '/locations' +); \ No newline at end of file diff --git a/js/viewjs/manageapikeys.js b/js/viewjs/manageapikeys.js index ed6c812c..d3ab97c6 100644 --- a/js/viewjs/manageapikeys.js +++ b/js/viewjs/manageapikeys.js @@ -16,42 +16,14 @@ if (createdApiKeyId !== undefined) animateCSS("#apiKeyRow_" + createdApiKeyId, "pulse"); } -$(document).on('click', '.apikey-delete-button', function(e) -{ - var objectName = $(e.currentTarget).attr('data-apikey-apikey'); - var objectId = $(e.currentTarget).attr('data-apikey-id'); - - bootbox.confirm({ - message: __t('Are you sure to delete API key "%s"?', objectName), - closeButton: false, - buttons: { - confirm: { - label: __t('Yes'), - className: 'btn-success' - }, - cancel: { - label: __t('No'), - className: 'btn-danger' - } - }, - callback: function(result) - { - if (result === true) - { - Grocy.Api.Delete('objects/api_keys/' + objectId, {}, - function(result) - { - window.location.href = U('/manageapikeys'); - }, - function(xhr) - { - console.error(xhr); - } - ); - } - } - }); -}); +Grocy.FrontendHelpers.MakeDeleteConfirmBox( + 'Are you sure to delete API key "%s"?', + '.apikey-delete-button', + 'data-apikey-apikey', + 'data-apikey-id', + 'objects/api_keys/', + '/manageapikeys' +); function QrCodeForApiKey(apiKeyType, apiKey) { diff --git a/js/viewjs/productgroups.js b/js/viewjs/productgroups.js index 28b82d28..cf4a3975 100644 --- a/js/viewjs/productgroups.js +++ b/js/viewjs/productgroups.js @@ -7,43 +7,15 @@ }); $('#productgroups-table tbody').removeClass("d-none"); Grocy.FrontendHelpers.InitDataTable(groupsTable); +Grocy.FrontendHelpers.MakeDeleteConfirmBox( + 'Are you sure to delete product group "%s"?', + '.product-group-delete-button', + 'data-group-name', + 'data-group-id', + 'objects/product_groups/', + '/productgroups' +); -$(document).on('click', '.product-group-delete-button', function(e) -{ - var objectName = $(e.currentTarget).attr('data-group-name'); - var objectId = $(e.currentTarget).attr('data-group-id'); - - bootbox.confirm({ - message: __t('Are you sure to delete product group "%s"?', objectName), - closeButton: false, - buttons: { - confirm: { - label: __t('Yes'), - className: 'btn-success' - }, - cancel: { - label: __t('No'), - className: 'btn-danger' - } - }, - callback: function(result) - { - if (result === true) - { - Grocy.Api.Delete('objects/product_groups/' + objectId, {}, - function(result) - { - window.location.href = U('/productgroups'); - }, - function(xhr) - { - console.error(xhr); - } - ); - } - } - }); -}); $(window).on("message", function(e) { var data = e.originalEvent.data; diff --git a/js/viewjs/products.js b/js/viewjs/products.js index c3dd5f12..2fe575af 100644 --- a/js/viewjs/products.js +++ b/js/viewjs/products.js @@ -17,51 +17,25 @@ Grocy.FrontendHelpers.InitDataTable(productsTable, null, function() }) Grocy.FrontendHelpers.MakeFilterForColumn("#product-group-filter", 6, productsTable); - if (typeof GetUriParam("product-group") !== "undefined") { $("#product-group-filter").val(GetUriParam("product-group")); $("#product-group-filter").trigger("change"); } -$(document).on('click', '.product-delete-button', function(e) -{ - var objectName = $(e.currentTarget).attr('data-product-name'); - var objectId = $(e.currentTarget).attr('data-product-id'); - - bootbox.confirm({ - message: __t('Are you sure to delete product "%s"?', objectName) + '

' + __t('This also removes any stock amount, the journal and all other references of this product - consider disabling it instead, if you want to keep that and just hide the product.'), - closeButton: false, - buttons: { - confirm: { - label: __t('Yes'), - className: 'btn-success' - }, - cancel: { - label: __t('No'), - className: 'btn-danger' - } - }, - callback: function(result) - { - if (result === true) - { - var jsonData = {}; - jsonData.active = 0; - Grocy.Api.Delete('objects/products/' + objectId, {}, - function(result) - { - window.location.href = U('/products'); - }, - function(xhr) - { - console.error(xhr); - } - ); - } - } - }); -}); +Grocy.FrontendHelpers.MakeDeleteConfirmBox( + (objectId, objectName) => + { + return __t('Are you sure to delete product "%s"?', objectName) + + '

' + + __t('This also removes any stock amount, the journal and all other references of this product - consider disabling it instead, if you want to keep that and just hide the product.'); + }, + '.product-delete-button', + 'data-product-name', + 'data-product-id', + 'objects/products/', + '/products' +); $("#show-disabled").change(function() { diff --git a/js/viewjs/quantityunits.js b/js/viewjs/quantityunits.js index 42641be6..46f4002b 100644 --- a/js/viewjs/quantityunits.js +++ b/js/viewjs/quantityunits.js @@ -7,40 +7,11 @@ }); $('#quantityunits-table tbody').removeClass("d-none"); Grocy.FrontendHelpers.InitDataTable(quantityUnitsTable); - -$(document).on('click', '.quantityunit-delete-button', function(e) -{ - var objectName = $(e.currentTarget).attr('data-quantityunit-name'); - var objectId = $(e.currentTarget).attr('data-quantityunit-id'); - - bootbox.confirm({ - message: __t('Are you sure to delete quantity unit "%s"?', objectName), - closeButton: false, - buttons: { - confirm: { - label: 'Yes', - className: 'btn-success' - }, - cancel: { - label: 'No', - className: 'btn-danger' - } - }, - callback: function(result) - { - if (result === true) - { - Grocy.Api.Delete('objects/quantity_units/' + objectId, {}, - function(result) - { - window.location.href = U('/quantityunits'); - }, - function(xhr) - { - console.error(xhr); - } - ); - } - } - }); -}); +Grocy.FrontendHelpers.MakeDeleteConfirmBox( + 'Are you sure to delete quantity unit "%s"?', + '.quantityunit-delete-button', + 'data-quantityunit-name', + 'data-quantityunit-id', + 'objects/quantity_units/', + '/quantityunits' +); \ No newline at end of file diff --git a/js/viewjs/recipes.js b/js/viewjs/recipes.js index 50a881bc..7efbaf4e 100644 --- a/js/viewjs/recipes.js +++ b/js/viewjs/recipes.js @@ -99,44 +99,14 @@ $("#status-filter").on("change", function() } }); -$(".recipe-delete").on('click', function(e) -{ - e.preventDefault(); - - var objectName = $(e.currentTarget).attr('data-recipe-name'); - var objectId = $(e.currentTarget).attr('data-recipe-id'); - - bootbox.confirm({ - message: __t('Are you sure to delete recipe "%s"?', objectName), - closeButton: false, - buttons: { - confirm: { - label: __t('Yes'), - className: 'btn-success' - }, - cancel: { - label: __t('No'), - className: 'btn-danger' - } - }, - callback: function(result) - { - if (result === true) - { - Grocy.Api.Delete('objects/recipes/' + objectId, {}, - function(result) - { - window.location.href = U('/recipes'); - }, - function(xhr) - { - console.error(xhr); - } - ); - } - } - }); -}); +Grocy.FrontendHelpers.MakeDeleteConfirmBox( + 'Are you sure to delete recipe "%s"?', + '.recipe-delete', + 'data-recipe-name', + 'data-recipe-id', + 'objects/recipes/', + '/recipes' +); $(document).on('click', '.recipe-shopping-list', function(e) { diff --git a/js/viewjs/shoppinglocations.js b/js/viewjs/shoppinglocations.js index 33a080e9..3c53e9a8 100644 --- a/js/viewjs/shoppinglocations.js +++ b/js/viewjs/shoppinglocations.js @@ -8,39 +8,11 @@ var locationsTable = $('#shoppinglocations-table').DataTable({ $('#shoppinglocations-table tbody').removeClass("d-none"); Grocy.FrontendHelpers.InitDataTable(locationsTable); -$(document).on('click', '.shoppinglocation-delete-button', function(e) -{ - var objectName = $(e.currentTarget).attr('data-shoppinglocation-name'); - var objectId = $(e.currentTarget).attr('data-shoppinglocation-id'); - - bootbox.confirm({ - message: __t('Are you sure to delete store "%s"?', objectName), - closeButton: false, - buttons: { - confirm: { - label: __t('Yes'), - className: 'btn-success' - }, - cancel: { - label: __t('No'), - className: 'btn-danger' - } - }, - callback: function(result) - { - if (result === true) - { - Grocy.Api.Delete('objects/shopping_locations/' + objectId, {}, - function(result) - { - window.location.href = U('/shoppinglocations'); - }, - function(xhr) - { - console.error(xhr); - } - ); - } - } - }); -}); +Grocy.FrontendHelpers.MakeDeleteConfirmBox( + 'Are you sure to delete store "%s"?', + '.shoppinglocation-delete-button', + 'data-shoppinglocation-name', + 'data-shoppinglocation-id', + 'objects/shopping_locations/', + '/shoppinglocations' +); \ No newline at end of file diff --git a/js/viewjs/taskcategories.js b/js/viewjs/taskcategories.js index d47cc729..599930a3 100644 --- a/js/viewjs/taskcategories.js +++ b/js/viewjs/taskcategories.js @@ -8,39 +8,11 @@ $('#taskcategories-table tbody').removeClass("d-none"); Grocy.FrontendHelpers.InitDataTable(categoriesTable); -$(document).on('click', '.task-category-delete-button', function(e) -{ - var objectName = $(e.currentTarget).attr('data-category-name'); - var objectId = $(e.currentTarget).attr('data-category-id'); - - bootbox.confirm({ - message: __t('Are you sure to delete task category "%s"?', objectName), - closeButton: false, - buttons: { - confirm: { - label: __t('Yes'), - className: 'btn-success' - }, - cancel: { - label: __t('No'), - className: 'btn-danger' - } - }, - callback: function(result) - { - if (result === true) - { - Grocy.Api.Delete('objects/task_categories/' + objectId, {}, - function(result) - { - window.location.href = U('/taskcategories'); - }, - function(xhr) - { - console.error(xhr); - } - ); - } - } - }); -}); +Grocy.FrontendHelpers.MakeDeleteConfirmBox( + 'Are you sure to delete task category "%s"?', + '.task-category-delete-button', + 'data-category-name', + 'data-category-id', + 'objects/task_categories/', + '/taskcategories' +); \ No newline at end of file diff --git a/js/viewjs/tasks.js b/js/viewjs/tasks.js index 881c428a..85c27dcf 100644 --- a/js/viewjs/tasks.js +++ b/js/viewjs/tasks.js @@ -90,48 +90,21 @@ $(document).on('click', '.undo-task-button', function(e) ); }); -$(document).on('click', '.delete-task-button', function(e) -{ - e.preventDefault(); - - var objectName = $(e.currentTarget).attr('data-task-name'); - var objectId = $(e.currentTarget).attr('data-task-id'); - - bootbox.confirm({ - message: __t('Are you sure to delete task "%s"?', objectName), - closeButton: false, - buttons: { - confirm: { - label: __t('Yes'), - className: 'btn-success' - }, - cancel: { - label: __t('No'), - className: 'btn-danger' - } - }, - callback: function(result) +Grocy.FrontendHelpers.MakeDeleteConfirmBox( + 'Are you sure to delete task "%s"?', + '.delete-task-button', + 'data-task-name', + 'data-task-id', + 'objects/tasks/', + (result, objectId, objectName) => + { + animateCSS("#task-" + objectId + "-row", "fadeOut", function() { - if (result === true) - { - Grocy.Api.Delete('objects/tasks/' + objectId, {}, - function(result) - { - animateCSS("#task-" + objectId + "-row", "fadeOut", function() - { - $("#task-" + objectId + "-row").tooltip("hide"); - $("#task-" + objectId + "-row").remove(); - }); - }, - function(xhr) - { - console.error(xhr); - } - ); - } - } - }); -}); + $("#task-" + objectId + "-row").tooltip("hide"); + $("#task-" + objectId + "-row").remove(); + }); + } +); $("#show-done-tasks").change(function() { diff --git a/js/viewjs/userentities.js b/js/viewjs/userentities.js index 3ae6bd0f..93145ca0 100644 --- a/js/viewjs/userentities.js +++ b/js/viewjs/userentities.js @@ -8,39 +8,11 @@ $('#userentities-table tbody').removeClass("d-none"); Grocy.FrontendHelpers.InitDataTable(userentitiesTable); -$(document).on('click', '.userentity-delete-button', function(e) -{ - var objectName = $(e.currentTarget).attr('data-userentity-name'); - var objectId = $(e.currentTarget).attr('data-userentity-id'); - - bootbox.confirm({ - message: __t('Are you sure to delete userentity "%s"?', objectName), - closeButton: false, - buttons: { - confirm: { - label: __t('Yes'), - className: 'btn-success' - }, - cancel: { - label: __t('No'), - className: 'btn-danger' - } - }, - callback: function(result) - { - if (result === true) - { - Grocy.Api.Delete('objects/userentities/' + objectId, {}, - function(result) - { - window.location.href = U('/userentities'); - }, - function(xhr) - { - console.error(xhr); - } - ); - } - } - }); -}); +Grocy.FrontendHelpers.MakeDeleteConfirmBox( + 'Are you sure to delete userentity "%s"?', + '.userentity-delete-button', + 'data-userentity-name', + 'data-userentity-id', + 'objects/userentities/', + '/userentities' +); \ No newline at end of file diff --git a/js/viewjs/userfields.js b/js/viewjs/userfields.js index cdb1c747..394174f7 100644 --- a/js/viewjs/userfields.js +++ b/js/viewjs/userfields.js @@ -27,42 +27,14 @@ $("#entity-filter").on("change", function() $("#new-userfield-button").attr("href", U("/userfield/new?embedded&entity=" + value)); }); -$(document).on('click', '.userfield-delete-button', function(e) -{ - var objectName = $(e.currentTarget).attr('data-userfield-name'); - var objectId = $(e.currentTarget).attr('data-userfield-id'); - - bootbox.confirm({ - message: __t('Are you sure to delete user field "%s"?', objectName), - closeButton: false, - buttons: { - confirm: { - label: __t('Yes'), - className: 'btn-success' - }, - cancel: { - label: __t('No'), - className: 'btn-danger' - } - }, - callback: function(result) - { - if (result === true) - { - Grocy.Api.Delete('objects/userfields/' + objectId, {}, - function(result) - { - window.location.href = U('/userfields'); - }, - function(xhr) - { - console.error(xhr); - } - ); - } - } - }); -}); +Grocy.FrontendHelpers.MakeDeleteConfirmBox( + 'Are you sure to delete user field "%s"?', + '.userfield-delete-button', + 'data-userfield-name', + 'data-userfield-id', + 'objects/userfields/', + '/userfields' +); if (GetUriParam("entity") != undefined && !GetUriParam("entity").isEmpty()) { diff --git a/js/viewjs/userobjects.js b/js/viewjs/userobjects.js index 84126ba4..8232ffc1 100644 --- a/js/viewjs/userobjects.js +++ b/js/viewjs/userobjects.js @@ -8,38 +8,11 @@ $('#userobjects-table tbody').removeClass("d-none"); Grocy.FrontendHelpers.InitDataTable(userobjectsTable); -$(document).on('click', '.userobject-delete-button', function(e) -{ - var objectId = $(e.currentTarget).attr('data-userobject-id'); - - bootbox.confirm({ - message: __t('Are you sure to delete this userobject?'), - closeButton: false, - buttons: { - confirm: { - label: __t('Yes'), - className: 'btn-success' - }, - cancel: { - label: __t('No'), - className: 'btn-danger' - } - }, - callback: function(result) - { - if (result === true) - { - Grocy.Api.Delete('objects/userobjects/' + objectId, {}, - function(result) - { - window.location.reload(); - }, - function(xhr) - { - console.error(xhr); - } - ); - } - } - }); -}); +Grocy.FrontendHelpers.MakeDeleteConfirmBox( + 'Are you sure to delete this userobject?', + '.userobject-delete-button', + 'data-userobject-id', + 'data-userobject-id', + 'objects/userobjects/', + () => window.location.reload() +); \ No newline at end of file diff --git a/js/viewjs/users.js b/js/viewjs/users.js index 32d3dce0..90edcb12 100644 --- a/js/viewjs/users.js +++ b/js/viewjs/users.js @@ -8,39 +8,11 @@ $('#users-table tbody').removeClass("d-none"); Grocy.FrontendHelpers.InitDataTable(usersTable); -$(document).on('click', '.user-delete-button', function(e) -{ - var objectName = $(e.currentTarget).attr('data-user-username'); - var objectId = $(e.currentTarget).attr('data-user-id'); - - bootbox.confirm({ - message: __t('Are you sure to delete user "%s"?', objectName), - closeButton: false, - buttons: { - confirm: { - label: __t('Yes'), - className: 'btn-success' - }, - cancel: { - label: __t('No'), - className: 'btn-danger' - } - }, - callback: function(result) - { - if (result === true) - { - Grocy.Api.Delete('users/' + objectId, {}, - function(result) - { - window.location.href = U('/users'); - }, - function(xhr) - { - console.error(xhr); - } - ); - } - } - }); -}); +Grocy.FrontendHelpers.MakeDeleteConfirmBox( + 'Are you sure to delete user "%s"?', + '.user-delete-button', + 'data-user-username', + 'data-user-id', + 'users/', + '/users' +); \ No newline at end of file