refactor project edit livewire

This commit is contained in:
Andras Bacsai
2024-11-03 23:55:04 +01:00
parent 3b4a81be2a
commit 2f0f244382
2 changed files with 29 additions and 17 deletions

View File

@@ -3,34 +3,47 @@
namespace App\Livewire\Project;
use App\Models\Project;
use Livewire\Attributes\Rule;
use Livewire\Component;
class Edit extends Component
{
public Project $project;
protected $rules = [
'project.name' => 'required|min:3|max:255',
'project.description' => 'nullable|string|max:255',
];
#[Rule(['required', 'string', 'min:3', 'max:255'])]
public string $name;
public function mount()
#[Rule(['nullable', 'string', 'max:255'])]
public ?string $description = null;
public function mount(string $project_uuid)
{
$projectUuid = request()->route('project_uuid');
$teamId = currentTeam()->id;
$project = Project::where('team_id', $teamId)->where('uuid', $projectUuid)->first();
if (! $project) {
return redirect()->route('dashboard');
try {
$this->project = Project::where('team_id', currentTeam()->id)->where('uuid', $project_uuid)->firstOrFail();
$this->syncData();
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
public function syncData(bool $toModel = false)
{
if ($toModel) {
$this->validate();
$this->project->update([
'name' => $this->name,
'description' => $this->description,
]);
} else {
$this->name = $this->project->name;
$this->description = $this->project->description;
}
$this->project = $project;
}
public function submit()
{
try {
$this->validate();
$this->project->save();
$this->dispatch('saved');
$this->syncData(true);
$this->dispatch('success', 'Project updated.');
} catch (\Throwable $e) {
return handleError($e, $this);

View File

@@ -11,10 +11,9 @@
</div>
</div>
<div class="pt-2 pb-10">Edit project details here.</div>
<div class="flex gap-2">
<x-forms.input label="Name" id="project.name" />
<x-forms.input label="Description" id="project.description" />
<x-forms.input label="Name" id="name" />
<x-forms.input label="Description" id="description" />
</div>
</form>
</div>