50 lines
1.1 KiB
PHP
50 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use App\Models\TaskList;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Livewire\Attributes\Validate;
|
|
use Livewire\Component;
|
|
|
|
class TasksContainer extends Component
|
|
{
|
|
public TaskList $list;
|
|
public Collection $tasks;
|
|
#[Validate('required|max:255')]
|
|
public string $task_content = '';
|
|
|
|
public function mount()
|
|
{
|
|
$this->tasks = $this->list->tasks->sortByDesc('created_at');
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$this->validate();
|
|
$t = $this->list->tasks()->create([
|
|
'content' => $this->task_content
|
|
]);
|
|
|
|
$this->tasks->prepend($t);
|
|
$this->task_content = '';
|
|
}
|
|
|
|
public function delete($id)
|
|
{
|
|
$this->list->tasks()->where('id', $id)->delete();
|
|
$this->tasks = $this->tasks->filter(fn($t) => $t->id !== $id);
|
|
}
|
|
|
|
public function toggleComplete($id)
|
|
{
|
|
$new = $this->tasks->where('id', $id)->first()->toggleComplete();
|
|
$this->tasks = $this->tasks->map(fn($t) => ($t->id == $id) ? $new : $t);
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.tasks-container');
|
|
}
|
|
}
|