95 lines
2.3 KiB
PHP
95 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Tasks;
|
|
|
|
use Illuminate\Contracts\View\View;
|
|
use Livewire\Attributes\Computed;
|
|
use Livewire\Attributes\Lazy;
|
|
use Livewire\Attributes\Layout;
|
|
use Livewire\Attributes\Url;
|
|
use Livewire\Component;
|
|
|
|
#[Layout('layouts.dashboard', ['active' => 'tasks'])]
|
|
#[Lazy]
|
|
class Index extends Component
|
|
{
|
|
public static function placeholder(array $params = []): View
|
|
{
|
|
return view('livewire.tasks.placeholder');
|
|
}
|
|
|
|
|
|
#[Url(as: 'filter', keep: true)]
|
|
public string $filter = 'open';
|
|
|
|
/** @var array<int,string> */
|
|
protected array $allowedFilters = ['open', 'done', 'all'];
|
|
|
|
public function mount(): void
|
|
{
|
|
if (! in_array($this->filter, $this->allowedFilters, true)) {
|
|
$this->filter = 'open';
|
|
}
|
|
}
|
|
|
|
public function setFilter(string $filter): void
|
|
{
|
|
if (in_array($filter, $this->allowedFilters, true)) {
|
|
$this->filter = $filter;
|
|
}
|
|
}
|
|
|
|
/** @var array<int,string> */
|
|
public array $startedKeys = [];
|
|
|
|
public function startTask(string $slug): void
|
|
{
|
|
$task = collect($this->tasks)->firstWhere('slug', $slug);
|
|
if ($task === null) {
|
|
return;
|
|
}
|
|
|
|
if (! in_array($slug, $this->startedKeys, true)) {
|
|
$this->startedKeys[] = $slug;
|
|
}
|
|
|
|
// Start navigates to the dedicated Run page (per-slug quiz + Lerni AI).
|
|
$this->redirectRoute('tasks.run', ['slug' => $slug], navigate: true);
|
|
}
|
|
|
|
public function repeatTask(string $slug): void
|
|
{
|
|
$this->startTask($slug);
|
|
}
|
|
|
|
#[Computed]
|
|
public function tasks(): array
|
|
{
|
|
return config('tasks', []);
|
|
}
|
|
|
|
#[Computed]
|
|
public function filteredTasks(): array
|
|
{
|
|
if ($this->filter === 'all') {
|
|
return $this->tasks;
|
|
}
|
|
return array_values(array_filter($this->tasks, fn($t) => $t['status'] === $this->filter));
|
|
}
|
|
|
|
#[Computed]
|
|
public function counts(): array
|
|
{
|
|
return [
|
|
'open' => count(array_filter($this->tasks, fn($t) => $t['status'] === 'open')),
|
|
'done' => count(array_filter($this->tasks, fn($t) => $t['status'] === 'done')),
|
|
'all' => count($this->tasks),
|
|
];
|
|
}
|
|
|
|
public function render(): View
|
|
{
|
|
return view('livewire.tasks.index');
|
|
}
|
|
}
|