refactor project env edit livewire

This commit is contained in:
Andras Bacsai
2024-11-04 00:00:58 +01:00
parent 2f0f244382
commit ca7290cebe
2 changed files with 37 additions and 17 deletions

View File

@@ -4,6 +4,8 @@ namespace App\Livewire\Project;
use App\Models\Application;
use App\Models\Project;
use Livewire\Attributes\Locked;
use Livewire\Attributes\Rule;
use Livewire\Component;
class EnvironmentEdit extends Component
@@ -12,29 +14,45 @@ class EnvironmentEdit extends Component
public Application $application;
#[Locked]
public $environment;
public array $parameters;
#[Rule(['required', 'string', 'min:3', 'max:255'])]
public string $name;
protected $rules = [
'environment.name' => 'required|min:3|max:255',
'environment.description' => 'nullable|min:3|max:255',
];
#[Rule(['nullable', 'string', 'max:255'])]
public ?string $description = null;
public function mount()
public function mount(string $project_uuid, string $environment_name)
{
$this->parameters = get_route_parameters();
$this->project = Project::ownedByCurrentTeam()->where('uuid', request()->route('project_uuid'))->first();
$this->environment = $this->project->environments()->where('name', request()->route('environment_name'))->first();
try {
$this->project = Project::ownedByCurrentTeam()->where('uuid', $project_uuid)->firstOrFail();
$this->environment = $this->project->environments()->where('name', $environment_name)->firstOrFail();
$this->syncData();
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
public function syncData(bool $toModel = false)
{
if ($toModel) {
$this->validate();
$this->environment->update([
'name' => $this->name,
'description' => $this->description,
]);
} else {
$this->name = $this->environment->name;
$this->description = $this->environment->description;
}
}
public function submit()
{
$this->validate();
try {
$this->environment->save();
return redirect()->route('project.environment.edit', ['project_uuid' => $this->project->uuid, 'environment_name' => $this->environment->name]);
$this->syncData(true);
$this->redirectRoute('project.environment.edit', ['environment_name' => $this->environment->name, 'project_uuid' => $this->project->uuid]);
} catch (\Throwable $e) {
return handleError($e, $this);
}

View File

@@ -13,7 +13,7 @@
<li class="inline-flex items-center">
<div class="flex items-center">
<a class="text-xs truncate lg:text-sm"
href="{{ route('project.show', ['project_uuid' => data_get($parameters, 'project_uuid')]) }}">
href="{{ route('project.show', ['project_uuid' => $project->uuid]) }}">
{{ $project->name }}</a>
<svg aria-hidden="true" class="w-4 h-4 mx-1 font-bold dark:text-warning" fill="currentColor"
viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
@@ -26,7 +26,9 @@
<li>
<div class="flex items-center">
<a class="text-xs truncate lg:text-sm"
href="{{ route('project.resource.index', ['environment_name' => data_get($parameters, 'environment_name'), 'project_uuid' => data_get($parameters, 'project_uuid')]) }}">{{ data_get($parameters, 'environment_name') }}</a>
href="{{ route('project.resource.index', ['environment_name' => $environment->name, 'project_uuid' => $project->uuid]) }}">
{{ $environment->name }}
</a>
</div>
</li>
<li>
@@ -43,8 +45,8 @@
</ol>
</nav>
<div class="flex gap-2">
<x-forms.input label="Name" id="environment.name" />
<x-forms.input label="Description" id="environment.description" />
<x-forms.input label="Name" id="name" />
<x-forms.input label="Description" id="description" />
</div>
</form>
</div>