flowtodo/app/Livewire/TaskListSidebar.php
Javier Feliz 0ddd325281
Some checks are pending
linter / quality (push) Waiting to run
tests / ci (push) Waiting to run
Initial commit
2025-07-15 18:28:47 -04:00

70 lines
1.6 KiB
PHP

<?php
namespace App\Livewire;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
class TaskListSidebar extends Component
{
public Collection $lists;
public string $list_name = '';
public function mount()
{
$this->lists = auth()->user()->taskLists()->orderBy('created_at')->get();
}
public function create()
{
$list = Auth::user()->taskLists()->create([
'name' => $this->list_name
]);
$this->lists->push($list);
$this->list_name = '';
}
public function delete($id)
{
Auth::user()->taskLists()->whereId($id)->delete();
$this->lists = $this->lists->filter(fn($l) => $l->id !== $id);
}
public function setHome($id)
{
$this->lists = $this->lists->map(function ($l) use ($id) {
if ($l->id == $id) {
$l->is_home = true;
} else {
$l->is_home = false;
}
return $l;
});
Auth::user()->taskLists()->update(['is_home' => false]);
Auth::user()->taskLists()->where('id', $id)->update(['is_home' => true]);
}
public function removeHome($id)
{
$this->lists = $this->lists->map(function ($l) use ($id) {
if ($l->id == $id) {
$l->is_home = false;
}
return $l;
});
$this->lists->where('id', $id)->first()->update(['is_home' => false]);
}
public function render()
{
return view('livewire.task-list-sidebar');
}
}