generated from thegrind/laravel-dockerized
75 lines
1.8 KiB
PHP
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');
|
|
}
|
|
}
|