From 8fabfd25d6273491227eabd0130ce39a62205792 Mon Sep 17 00:00:00 2001 From: "Joseph.Rawlings" Date: Thu, 18 Dec 2025 08:10:36 -0500 Subject: [PATCH] fix: optimize adapter firewall toggle logic and improve error handling --- .../Servers/Proxmox/Livewire/Firewall.php | 58 +++++++++++-------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/extensions/Servers/Proxmox/Livewire/Firewall.php b/extensions/Servers/Proxmox/Livewire/Firewall.php index 02de467d5..fe453daee 100644 --- a/extensions/Servers/Proxmox/Livewire/Firewall.php +++ b/extensions/Servers/Proxmox/Livewire/Firewall.php @@ -409,21 +409,23 @@ class Firewall extends Component public function toggleAdapterFirewall(string $adapter): void { - $this->loading = true; + $currentState = $this->adapterFirewallStates[$adapter] ?? false; + $newState = !$currentState; + + // Get the full network configuration string + $configString = $this->adapterConfigs[$adapter] ?? ''; + + if (empty($configString)) { + $this->error = 'Network adapter configuration not found'; + $this->showErrorModal = true; + return; + } + + // Optimistic UI update - update state immediately + $this->adapterFirewallStates[$adapter] = $newState; + $this->skipRender(); try { - $currentState = $this->adapterFirewallStates[$adapter] ?? false; - $newState = !$currentState; - - // Get the full network configuration string - $configString = $this->adapterConfigs[$adapter] ?? ''; - - if (empty($configString)) { - $this->error = 'Network adapter configuration not found'; - $this->showErrorModal = true; - return; - } - // Parse the configuration string and modify the firewall parameter $parts = explode(',', $configString); $firewallFound = false; @@ -456,18 +458,21 @@ class Firewall extends Component ); if ($response->successful()) { - $this->adapterFirewallStates[$adapter] = $newState; $this->adapterConfigs[$adapter] = $newConfigString; // Update stored config - $this->notify('Adapter firewall ' . ($newState ? 'enabled' : 'disabled'), 'success'); + $this->dispatch('notify', 'Adapter firewall ' . ($newState ? 'enabled' : 'disabled'), 'success'); } else { + // Revert on failure + $this->adapterFirewallStates[$adapter] = $currentState; + $this->error = 'Failed to toggle adapter firewall: ' . $response->body(); $this->showErrorModal = true; } } catch (\Exception $e) { + // Revert on failure + $this->adapterFirewallStates[$adapter] = $currentState; + $this->error = 'Failed to toggle adapter firewall: ' . $e->getMessage(); $this->showErrorModal = true; - } finally { - $this->loading = false; } } @@ -493,11 +498,13 @@ class Firewall extends Component public function toggleFirewall(): void { - $this->loading = true; + $newState = !$this->firewallEnabled; + + // Optimistic UI update - update state immediately + $this->firewallEnabled = $newState; + $this->skipRender(); try { - $newState = !$this->firewallEnabled; - // Always force DHCP disabled and NDP enabled $response = $this->firewallRequest( '/nodes/' . $this->server->node->name . '/qemu/' . $this->server->vm_id . '/firewall/options', @@ -510,9 +517,11 @@ class Firewall extends Component ); if ($response->successful()) { - $this->firewallEnabled = $newState; - $this->notify('Firewall ' . ($newState ? 'enabled' : 'disabled'), 'success'); + $this->dispatch('notify', 'Firewall ' . ($newState ? 'enabled' : 'disabled'), 'success'); } else { + // Revert on failure + $this->firewallEnabled = !$newState; + // Show detailed error message from API $errorBody = $response->json(); $errorMsg = $errorBody['errors'] ?? $response->body(); @@ -520,10 +529,11 @@ class Firewall extends Component $this->showErrorModal = true; } } catch (\Exception $e) { + // Revert on failure + $this->firewallEnabled = !$newState; + $this->error = 'Failed to toggle firewall: ' . $e->getMessage(); $this->showErrorModal = true; - } finally { - $this->loading = false; } }