Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

refactor swarm view livewire

Andras Bacsaie4d5969326fb
diff --git a/app/Livewire/Project/Application/Swarm.php b/app/Livewire/Project/Application/Swarm.php
index 0151b52226..c2cdcd667b 100644
--- a/app/Livewire/Project/Application/Swarm.php
+++ b/app/Livewire/Project/Application/Swarm.php
@@ -3,32 +3,55 @@
namespace App\Livewire\Project\Application;
use App\Models\Application;
+use Livewire\Attributes\Rule;
use Livewire\Component;
class Swarm extends Component
{
public Application $application;
- public string $swarm_placement_constraints = '';
+ #[Rule('required')]
+ public int $swarmReplicas;
- protected $rules = [
- 'application.swarm_replicas' => 'required',
- 'application.swarm_placement_constraints' => 'nullable',
- 'application.settings.is_swarm_only_worker_nodes' => 'required',
- ];
+ #[Rule(['nullable'])]
+ public ?string $swarmPlacementConstraints = null;
+
+ #[Rule('required')]
+ public bool $isSwarmOnlyWorkerNodes;
public function mount()
{
- if ($this->application->swarm_placement_constraints) {
- $this->swarm_placement_constraints = base64_decode($this->application->swarm_placement_constraints);
+ try {
+ $this->syncData();
+ } catch (\Throwable $e) {
+ return handleError($e, $this);
}
}
- public function instantSave()
+ public function syncData(bool $toModel = false)
{
- try {
+ 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()
+ {
+ try {
+ $this->syncData(true);
$this->dispatch('success', 'Swarm settings updated.');
} catch (\Throwable $e) {
return handleError($e, $this);
@@ -38,14 +61,7 @@ class Swarm extends Component
public function submit()
{
try {
- $this->validate();
- 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->syncData(true);
$this->dispatch('success', 'Swarm settings updated.');
} catch (\Throwable $e) {
return handleError($e, $this);
diff --git a/resources/views/livewire/project/application/swarm.blade.php b/resources/views/livewire/project/application/swarm.blade.php
index 2954c9e683..e733dbd07d 100644
--- a/resources/views/livewire/project/application/swarm.blade.php
+++ b/resources/views/livewire/project/application/swarm.blade.php
@@ -6,15 +6,13 @@
Save
</x-forms.button>
</div>
- {{-- <div>Advanced Swarm Configuration</div> --}}
<div class="flex flex-col gap-2 py-4">
<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."
- id="application.settings.is_swarm_only_worker_nodes" label="Only Start on Worker nodes" />
+ id="isSwarmOnlyWorkerNodes" label="Only Start on Worker nodes" />
</div>
- <x-forms.textarea id="swarm_placement_constraints" rows="7"
- label="Custom Placement Constraints"
+ <x-forms.textarea id="swarmPlacementConstraints" rows="7" label="Custom Placement Constraints"
placeholder="placement:
constraints:
- 'node.role == worker'" />