Refactor BaseComponent to remove unused code and initialize route parameters in the boot method

This commit is contained in:
Andras Bacsai
2024-10-17 22:00:27 +02:00
parent bfa9a8776e
commit 513c74a7e3
3 changed files with 14 additions and 27 deletions

View File

@@ -1,20 +0,0 @@
<?php
namespace App\Livewire;
use Livewire\Component;
abstract class BaseComponent extends Component
{
public $parameters = [];
public function boot()
{
$this->parameters = $this->getRouteParameters();
}
protected function getRouteParameters()
{
return get_route_parameters();
}
}

View File

@@ -7,18 +7,22 @@ use Livewire\Component;
class DeleteEnvironment extends Component class DeleteEnvironment extends Component
{ {
public array $parameters;
public int $environment_id; public int $environment_id;
public bool $disabled = false; public bool $disabled = false;
public string $environmentName = ''; public string $environmentName = '';
public array $parameters;
public function mount() public function mount()
{ {
$this->parameters = get_route_parameters(); try {
$this->environmentName = Environment::findOrFail($this->environment_id)->name; $this->environmentName = Environment::findOrFail($this->environment_id)->name;
$this->parameters = get_route_parameters();
} catch (\Exception $e) {
return handleError($e, $this);
}
} }
public function delete() public function delete()
@@ -30,7 +34,7 @@ class DeleteEnvironment extends Component
if ($environment->isEmpty()) { if ($environment->isEmpty()) {
$environment->delete(); $environment->delete();
return redirect()->route('project.show', ['project_uuid' => $this->parameters['project_uuid']]); return redirect()->route('project.show', parameters: ['project_uuid' => $this->parameters['project_uuid']]);
} }
return $this->dispatch('error', 'Environment has defined resources, please delete them first.'); return $this->dispatch('error', 'Environment has defined resources, please delete them first.');

View File

@@ -2,22 +2,25 @@
namespace App\Livewire\Server; namespace App\Livewire\Server;
use App\Livewire\BaseComponent;
use App\Models\Server; use App\Models\Server;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Livewire\Component;
class Show extends BaseComponent class Show extends Component
{ {
use AuthorizesRequests; use AuthorizesRequests;
public Server $server; public Server $server;
public array $parameters;
protected $listeners = ['refreshServerShow']; protected $listeners = ['refreshServerShow'];
public function mount() public function mount()
{ {
try { try {
$this->server = Server::ownedByCurrentTeam()->whereUuid(request()->server_uuid)->firstOrFail(); $this->server = Server::ownedByCurrentTeam()->whereUuid(request()->server_uuid)->firstOrFail();
$this->parameters = get_route_parameters();
} catch (\Throwable $e) { } catch (\Throwable $e) {
return handleError($e, $this); return handleError($e, $this);
} }