Javier Feliz 1f596fdc20
Some checks failed
tests / ci (push) Has been cancelled
linter / quality (push) Has been cancelled
Build & Push Docker Image to Registry / build (release) Successful in 4m34s
Editable scripts and install octane
2025-07-22 22:40:47 -04:00

75 lines
1.8 KiB
PHP

<?php
namespace App\Livewire\Forms;
use App\Models\Script as ModelsScript;
use Livewire\Attributes\On;
use Livewire\Attributes\Validate;
use Livewire\Component;
class Script extends Component
{
public ?ModelsScript $script;
#[Validate('required')]
public string $name = '';
#[Validate('required')]
public string $path = '';
#[Validate('required')]
public string $content = '';
public function mount()
{
if (!empty($this->script)) {
$this->fill([
'name' => $this->script->name,
'path' => $this->script->path,
'content' => $this->script->content,
]);
}
}
#[On('edit-script')]
public function loadScript($id)
{
$this->script = auth()->user()->scripts()->find($id);
$this->fill([
'name' => $this->script->name,
'path' => $this->script->path,
'content' => $this->script->content,
]);
}
public function resetToNew()
{
$this->script = null;
$this->reset(['name', 'path', 'content']);
}
public function create()
{
$this->validate();
if (empty($this->script)) {
auth()->user()->scripts()->create([
'name' => $this->name,
'path' => str($this->path)->remove('.sh')->lower()->kebab()->toString(),
'content' => $this->content
]);
} else {
$this->script->update([
'name' => $this->name,
'path' => str($this->path)->remove('.sh')->lower()->kebab()->toString(),
'content' => $this->content
]);
$this->script->refresh();
}
$this->reset(['name', 'path', 'content']);
}
public function render()
{
return view('livewire.forms.script');
}
}