refactor: update rule handling in Proxmox firewall component to use index-based access for improved clarity and performance
This commit is contained in:
@@ -243,11 +243,11 @@ class Firewall extends Component
|
||||
$this->showRuleForm = true;
|
||||
}
|
||||
|
||||
public function startEditRule(int $pos): void
|
||||
public function startEditRule(int $index): void
|
||||
{
|
||||
$rule = collect($this->rules)->firstWhere('pos', $pos);
|
||||
if ($rule) {
|
||||
$this->editingRulePos = $pos;
|
||||
if (isset($this->rules[$index])) {
|
||||
$rule = $this->rules[$index];
|
||||
$this->editingRulePos = $rule['pos'];
|
||||
$this->showRuleForm = true;
|
||||
$this->ruleAction = $rule['action'] ?? 'ACCEPT';
|
||||
$this->ruleSource = $rule['source'] ?? '';
|
||||
@@ -380,26 +380,24 @@ class Firewall extends Component
|
||||
}
|
||||
}
|
||||
|
||||
public function toggleRule(int $pos): void
|
||||
public function toggleRule(int $index): void
|
||||
{
|
||||
try {
|
||||
// Find the rule to get its current enable state
|
||||
$ruleIndex = collect($this->rules)->search(function ($rule) use ($pos) {
|
||||
return ($rule['pos'] ?? 0) == $pos;
|
||||
});
|
||||
|
||||
if ($ruleIndex === false) {
|
||||
if (!isset($this->rules[$index])) {
|
||||
$this->error = 'Rule not found';
|
||||
$this->showErrorModal = true;
|
||||
return;
|
||||
}
|
||||
|
||||
$rule = $this->rules[$index];
|
||||
$pos = $rule['pos'];
|
||||
|
||||
// Toggle the enable state (0 = disabled, 1 = enabled)
|
||||
$currentEnable = isset($this->rules[$ruleIndex]['enable']) ? (int)$this->rules[$ruleIndex]['enable'] : 1;
|
||||
$currentEnable = isset($rule['enable']) ? (int)$rule['enable'] : 1;
|
||||
$newEnable = $currentEnable == 0 ? 1 : 0;
|
||||
|
||||
// Optimistically update the local state for immediate UI feedback
|
||||
$this->rules[$ruleIndex]['enable'] = $newEnable;
|
||||
$this->rules[$index]['enable'] = $newEnable;
|
||||
|
||||
// Update only the enable field
|
||||
$response = $this->firewallRequest(
|
||||
@@ -414,7 +412,7 @@ class Firewall extends Component
|
||||
$this->loadFirewallRules();
|
||||
} else {
|
||||
// Revert the optimistic update on failure
|
||||
$this->rules[$ruleIndex]['enable'] = $currentEnable;
|
||||
$this->rules[$index]['enable'] = $currentEnable;
|
||||
$message = $response->json()['errors'][0]['message'] ?? 'Unknown error';
|
||||
$this->error = 'Failed to toggle rule: ' . $message;
|
||||
$this->showErrorModal = true;
|
||||
@@ -437,10 +435,12 @@ class Firewall extends Component
|
||||
}
|
||||
}
|
||||
|
||||
public function confirmDeleteRule(int $pos): void
|
||||
public function confirmDeleteRule(int $index): void
|
||||
{
|
||||
$this->deletingRulePos = $pos;
|
||||
$this->showDeleteModal = true;
|
||||
if (isset($this->rules[$index])) {
|
||||
$this->deletingRulePos = $this->rules[$index]['pos'];
|
||||
$this->showDeleteModal = true;
|
||||
}
|
||||
}
|
||||
|
||||
public function cancelDeleteRule(): void
|
||||
|
||||
@@ -607,7 +607,8 @@
|
||||
<x-ri-save-line class="size-4 inline mr-1" x-show="editingPositions" />
|
||||
<span x-text="editingPositions ? 'Save' : 'Edit Order'"></span>
|
||||
</button>
|
||||
<button
|
||||
<button
|
||||
type="button"
|
||||
wire:click="startCreateRule"
|
||||
class="px-4 py-2 bg-primary text-white rounded-lg hover:bg-primary/90 transition-colors text-sm font-medium">
|
||||
<x-ri-add-line class="size-4 inline mr-1" />
|
||||
@@ -857,7 +858,9 @@
|
||||
<td class="py-3 px-4 text-sm">
|
||||
<div class="flex gap-2">
|
||||
<button
|
||||
@click="$wire.toggleRule(rule.pos)"
|
||||
type="button"
|
||||
wire:click="toggleRule(index)"
|
||||
:wire:key="'toggle-' + index"
|
||||
wire:loading.attr="disabled"
|
||||
class="transition-colors"
|
||||
:class="rule.enable == 0 ? 'text-warning hover:text-warning/80' : 'text-success hover:text-success/80'"
|
||||
@@ -866,13 +869,17 @@
|
||||
<x-ri-toggle-fill x-show="rule.enable != 0" class="size-4" />
|
||||
</button>
|
||||
<button
|
||||
@click="$wire.startEditRule(rule.pos)"
|
||||
type="button"
|
||||
wire:click="startEditRule(index)"
|
||||
:wire:key="'edit-' + index"
|
||||
class="text-primary hover:text-primary/80 transition-colors"
|
||||
title="Edit rule">
|
||||
<x-ri-edit-line class="size-4" />
|
||||
</button>
|
||||
<button
|
||||
@click="$wire.confirmDeleteRule(rule.pos)"
|
||||
type="button"
|
||||
wire:click="confirmDeleteRule(index)"
|
||||
:wire:key="'delete-' + index"
|
||||
class="text-error hover:text-error/80 transition-colors"
|
||||
title="Delete rule">
|
||||
<x-ri-delete-bin-line class="size-4" />
|
||||
|
||||
Reference in New Issue
Block a user