fix: optimize adapter firewall toggle logic and improve error handling

This commit is contained in:
2025-12-18 08:10:36 -05:00
parent af1e2819c4
commit 8fabfd25d6

View File

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