refactor swarm view livewire

This commit is contained in:
Andras Bacsai
2024-11-04 11:25:45 +01:00
parent 83b3f6fe49
commit e4d5969326
2 changed files with 37 additions and 23 deletions

View File

@@ -3,32 +3,55 @@
namespace App\Livewire\Project\Application; namespace App\Livewire\Project\Application;
use App\Models\Application; use App\Models\Application;
use Livewire\Attributes\Rule;
use Livewire\Component; use Livewire\Component;
class Swarm extends Component class Swarm extends Component
{ {
public Application $application; public Application $application;
public string $swarm_placement_constraints = ''; #[Rule('required')]
public int $swarmReplicas;
protected $rules = [ #[Rule(['nullable'])]
'application.swarm_replicas' => 'required', public ?string $swarmPlacementConstraints = null;
'application.swarm_placement_constraints' => 'nullable',
'application.settings.is_swarm_only_worker_nodes' => 'required', #[Rule('required')]
]; public bool $isSwarmOnlyWorkerNodes;
public function mount() public function mount()
{ {
if ($this->application->swarm_placement_constraints) { try {
$this->swarm_placement_constraints = base64_decode($this->application->swarm_placement_constraints); $this->syncData();
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
public function syncData(bool $toModel = false)
{
if ($toModel) {
$this->validate();
$this->application->swarm_replicas = $this->swarmReplicas;
$this->application->swarm_placement_constraints = $this->swarmPlacementConstraints ? base64_encode($this->swarmPlacementConstraints) : null;
$this->application->settings->is_swarm_only_worker_nodes = $this->isSwarmOnlyWorkerNodes;
$this->application->save();
$this->application->settings->save();
} else {
$this->swarmReplicas = $this->application->swarm_replicas;
if ($this->application->swarm_placement_constraints) {
$this->swarmPlacementConstraints = base64_decode($this->application->swarm_placement_constraints);
} else {
$this->swarmPlacementConstraints = null;
}
$this->isSwarmOnlyWorkerNodes = $this->application->settings->is_swarm_only_worker_nodes;
} }
} }
public function instantSave() public function instantSave()
{ {
try { try {
$this->validate(); $this->syncData(true);
$this->application->settings->save();
$this->dispatch('success', 'Swarm settings updated.'); $this->dispatch('success', 'Swarm settings updated.');
} catch (\Throwable $e) { } catch (\Throwable $e) {
return handleError($e, $this); return handleError($e, $this);
@@ -38,14 +61,7 @@ class Swarm extends Component
public function submit() public function submit()
{ {
try { try {
$this->validate(); $this->syncData(true);
if ($this->swarm_placement_constraints) {
$this->application->swarm_placement_constraints = base64_encode($this->swarm_placement_constraints);
} else {
$this->application->swarm_placement_constraints = null;
}
$this->application->save();
$this->dispatch('success', 'Swarm settings updated.'); $this->dispatch('success', 'Swarm settings updated.');
} catch (\Throwable $e) { } catch (\Throwable $e) {
return handleError($e, $this); return handleError($e, $this);

View File

@@ -6,15 +6,13 @@
Save Save
</x-forms.button> </x-forms.button>
</div> </div>
{{-- <div>Advanced Swarm Configuration</div> --}}
<div class="flex flex-col gap-2 py-4"> <div class="flex flex-col gap-2 py-4">
<div class="flex flex-col items-end gap-2 xl:flex-row"> <div class="flex flex-col items-end gap-2 xl:flex-row">
<x-forms.input id="application.swarm_replicas" label="Replicas" required /> <x-forms.input id="swarmReplicas" label="Replicas" required />
<x-forms.checkbox instantSave helper="If turned off, this resource will start on manager nodes too." <x-forms.checkbox instantSave helper="If turned off, this resource will start on manager nodes too."
id="application.settings.is_swarm_only_worker_nodes" label="Only Start on Worker nodes" /> id="isSwarmOnlyWorkerNodes" label="Only Start on Worker nodes" />
</div> </div>
<x-forms.textarea id="swarm_placement_constraints" rows="7" <x-forms.textarea id="swarmPlacementConstraints" rows="7" label="Custom Placement Constraints"
label="Custom Placement Constraints"
placeholder="placement: placeholder="placement:
constraints: constraints:
- 'node.role == worker'" /> - 'node.role == worker'" />