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
This commit is contained in:
Tony Murray
2022-08-28 20:57:16 -05:00
committed by GitHub
parent a664d12eb7
commit 323f0ea8b5
3 changed files with 21 additions and 20 deletions

View File

@@ -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));

View File

@@ -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
*/

View File

@@ -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: "<?php echo url('port'); ?>/" + $target.data('port_id'),
data: {"groups": $target.val()},
url: "<?php echo url('port'); ?>/" + port_id,
data: groups,
success: function(data) {
toastr.success(data.message)
},