From 323f0ea8b50d094d686dbce5c53b46c273f2e38c Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Sun, 28 Aug 2022 20:57:16 -0500 Subject: [PATCH] Fix removing all port groups (#14253) * Fix removing all port groups * Make backend work in the situation where this endpoint is used for more than just this setting change change event is called multiple times when select2 is cleared (once for each item) prevent duplicate backend calls Remove no default Port Group item --- app/Http/Controllers/PortController.php | 10 ++++++---- .../Controllers/Select/PortGroupController.php | 13 ------------- includes/html/pages/device/edit/ports.inc.php | 18 +++++++++++++++--- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/app/Http/Controllers/PortController.php b/app/Http/Controllers/PortController.php index cc4a012de3..13a0494e66 100644 --- a/app/Http/Controllers/PortController.php +++ b/app/Http/Controllers/PortController.php @@ -26,20 +26,22 @@ namespace App\Http\Controllers; use App\Models\Port; +use Illuminate\Support\Facades\Validator; class PortController extends Controller { public function update(\Illuminate\Http\Request $request, Port $port) { - $this->validate($request, [ + $validated = Validator::make($request->json()->all(), [ + 'groups' => 'array', 'groups.*' => 'int', - ]); + ])->validate(); $updated = false; $message = ''; - if ($request->has('groups')) { - $changes = $port->groups()->sync($request->get('groups')); + if (array_key_exists('groups', $validated)) { + $changes = $port->groups()->sync($validated['groups']); $groups_updated = array_sum(array_map(function ($group_ids) { return count($group_ids); }, $changes)); diff --git a/app/Http/Controllers/Select/PortGroupController.php b/app/Http/Controllers/Select/PortGroupController.php index b21bada64a..75af2ceb12 100644 --- a/app/Http/Controllers/Select/PortGroupController.php +++ b/app/Http/Controllers/Select/PortGroupController.php @@ -39,19 +39,6 @@ class PortGroupController extends SelectController return PortGroup::hasAccess($request->user())->select(['id', 'name']); } - protected function formatResponse($paginator) - { - // prepend the default group, unless filtered out - if ($this->includeGeneral()) { - $general = new PortGroup; - $general->id = 0; - $general->name = 'no default Port Group'; - $paginator->prepend($general); - } - - return parent::formatResponse($paginator); - } - /** * @param PortGroup $port_group */ diff --git a/includes/html/pages/device/edit/ports.inc.php b/includes/html/pages/device/edit/ports.inc.php index bf644b5aca..ac6eaa630b 100644 --- a/includes/html/pages/device/edit/ports.inc.php +++ b/includes/html/pages/device/edit/ports.inc.php @@ -228,12 +228,24 @@ }); init_select2('.port_group_select', 'port-group', {}, null, 'No Group'); + var last_port_group_change; $('.port_group_select').on('change', function (e) { - var $target = $(e.target) + var $target = $(e.target); + var port_id = $target.data('port_id'); + var groups = JSON.stringify({"groups": $target.val()}); + + console.log(last_port_group_change, (port_id + groups)); + // don't send the same update multiple times... silly select2 + if (last_port_group_change === (port_id + groups)) { + return; + } + + last_port_group_change = port_id + groups; + $.ajax({ type: "PUT", - url: "/" + $target.data('port_id'), - data: {"groups": $target.val()}, + url: "/" + port_id, + data: groups, success: function(data) { toastr.success(data.message) },