refactor project edit livewire
This commit is contained in:
@@ -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);
|
||||
|
@@ -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>
|
||||
|
Reference in New Issue
Block a user