refactor project edit livewire
This commit is contained in:
@@ -3,34 +3,47 @@
|
|||||||
namespace App\Livewire\Project;
|
namespace App\Livewire\Project;
|
||||||
|
|
||||||
use App\Models\Project;
|
use App\Models\Project;
|
||||||
|
use Livewire\Attributes\Rule;
|
||||||
use Livewire\Component;
|
use Livewire\Component;
|
||||||
|
|
||||||
class Edit extends Component
|
class Edit extends Component
|
||||||
{
|
{
|
||||||
public Project $project;
|
public Project $project;
|
||||||
|
|
||||||
protected $rules = [
|
#[Rule(['required', 'string', 'min:3', 'max:255'])]
|
||||||
'project.name' => 'required|min:3|max:255',
|
public string $name;
|
||||||
'project.description' => 'nullable|string|max:255',
|
|
||||||
];
|
|
||||||
|
|
||||||
public function mount()
|
#[Rule(['nullable', 'string', 'max:255'])]
|
||||||
|
public ?string $description = null;
|
||||||
|
|
||||||
|
public function mount(string $project_uuid)
|
||||||
{
|
{
|
||||||
$projectUuid = request()->route('project_uuid');
|
try {
|
||||||
$teamId = currentTeam()->id;
|
$this->project = Project::where('team_id', currentTeam()->id)->where('uuid', $project_uuid)->firstOrFail();
|
||||||
$project = Project::where('team_id', $teamId)->where('uuid', $projectUuid)->first();
|
$this->syncData();
|
||||||
if (! $project) {
|
} catch (\Throwable $e) {
|
||||||
return redirect()->route('dashboard');
|
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()
|
public function submit()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$this->validate();
|
$this->syncData(true);
|
||||||
$this->project->save();
|
|
||||||
$this->dispatch('saved');
|
|
||||||
$this->dispatch('success', 'Project updated.');
|
$this->dispatch('success', 'Project updated.');
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
return handleError($e, $this);
|
return handleError($e, $this);
|
||||||
|
@@ -11,10 +11,9 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="pt-2 pb-10">Edit project details here.</div>
|
<div class="pt-2 pb-10">Edit project details here.</div>
|
||||||
|
|
||||||
<div class="flex gap-2">
|
<div class="flex gap-2">
|
||||||
<x-forms.input label="Name" id="project.name" />
|
<x-forms.input label="Name" id="name" />
|
||||||
<x-forms.input label="Description" id="project.description" />
|
<x-forms.input label="Description" id="description" />
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
Reference in New Issue
Block a user