diff --git a/controllers/StockApiController.php b/controllers/StockApiController.php index 08ebd820..0a9061fe 100644 --- a/controllers/StockApiController.php +++ b/controllers/StockApiController.php @@ -106,6 +106,12 @@ class StockApiController extends BaseApiController return $this->VoidApiActionResponse($response); } + public function ClearShoppingList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) + { + $this->StockService->ClearShoppingList(); + return $this->VoidApiActionResponse($response); + } + public function ExternalBarcodeLookup(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { try diff --git a/grocy.openapi.json b/grocy.openapi.json index 8dc419c6..db41715e 100644 --- a/grocy.openapi.json +++ b/grocy.openapi.json @@ -650,6 +650,26 @@ } } }, + "/stock/clear-shopping-list": { + "get": { + "description": "Removes all items from the shopping list", + "tags": [ + "Stock" + ], + "responses": { + "200": { + "description": "A VoidApiActionResponse object", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/VoidApiActionResponse" + } + } + } + } + } + } + }, "/stock/external-barcode-lookup/{barcode}": { "get": { "description": "Executes an external barcode lookoup via the configured plugin with the given barcode", diff --git a/localization/de.php b/localization/de.php index 094306a2..fd6d27d6 100644 --- a/localization/de.php +++ b/localization/de.php @@ -153,6 +153,8 @@ return array( 'Edit recipe ingredient' => 'Rezeptzutat bearbeiten', 'Are you sure to delete recipe "#1"?' => 'Rezept "#1" wirklich löschen?', 'Are you sure to delete recipe ingredient "#1"?' => 'Rezeptzutat "#1" wirklich löschen?', + 'Are you sure to empty the shopping list?' => 'Sicher, dass die Einkaufsliste geleert werden soll?', + 'Clear list' => 'Liste leeren', //Constants 'manually' => 'Manuell', diff --git a/public/viewjs/shoppinglist.js b/public/viewjs/shoppinglist.js index ec9afb40..0016ea3d 100644 --- a/public/viewjs/shoppinglist.js +++ b/public/viewjs/shoppinglist.js @@ -48,3 +48,36 @@ $(document).on('click', '#add-products-below-min-stock-amount', function(e) } ); }); + +$(document).on('click', '#clear-shopping-list', function(e) +{ + bootbox.confirm({ + message: L('Are you sure to empty the shopping list?'), + buttons: { + confirm: { + label: L('Yes'), + className: 'btn-success' + }, + cancel: { + label: L('No'), + className: 'btn-danger' + } + }, + callback: function(result) + { + if (result === true) + { + Grocy.Api.Get('stock/clear-shopping-list', + function(result) + { + window.location.href = U('/shoppinglist'); + }, + function(xhr) + { + console.error(xhr); + } + ); + } + } + }); +}); diff --git a/public/viewjs/shoppinglistform.js b/public/viewjs/shoppinglistform.js index 6b666034..8294445c 100644 --- a/public/viewjs/shoppinglistform.js +++ b/public/viewjs/shoppinglistform.js @@ -43,6 +43,7 @@ Grocy.Components.ProductPicker.GetPicker().on('change', function(e) { $('#amount_qu_unit').text(productDetails.quantity_unit_purchase.name); $('#amount').focus(); + Grocy.FrontendHelpers.ValidateForm('shoppinglist-form'); }, function(xhr) { diff --git a/routes.php b/routes.php index 2cfc997d..659bbebb 100644 --- a/routes.php +++ b/routes.php @@ -75,6 +75,7 @@ $app->group('/api', function() $this->get('/stock/get-product-details/{productId}', 'Grocy\Controllers\StockApiController:ProductDetails'); $this->get('/stock/get-current-stock', 'Grocy\Controllers\StockApiController:CurrentStock'); $this->get('/stock/add-missing-products-to-shoppinglist', 'Grocy\Controllers\StockApiController:AddMissingProductsToShoppingList'); + $this->get('/stock/clear-shopping-list', 'Grocy\Controllers\StockApiController:ClearShoppingList'); $this->get('/stock/external-barcode-lookup/{barcode}', 'Grocy\Controllers\StockApiController:ExternalBarcodeLookup'); $this->get('/recipes/add-not-fulfilled-products-to-shopping-list/{recipeId}', 'Grocy\Controllers\RecipesApiController:AddNotFulfilledProductsToShoppingList'); diff --git a/services/StockService.php b/services/StockService.php index 84b8fe15..28375b63 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -203,6 +203,11 @@ class StockService extends BaseService } } + public function ClearShoppingList() + { + $this->Database->shopping_list()->delete(); + } + private function ProductExists($productId) { $productRow = $this->Database->products()->where('id = :1', $productId)->fetch(); diff --git a/views/shoppinglist.blade.php b/views/shoppinglist.blade.php index 51a43871..af35f308 100644 --- a/views/shoppinglist.blade.php +++ b/views/shoppinglist.blade.php @@ -9,11 +9,14 @@