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;
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 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()
{
try {
$this->validate();
$this->application->settings->save();
$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);