lernschiff/app/Livewire/Tasks/Index.php

97 lines
2.4 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 $key): void
{
$task = collect($this->tasks)->firstWhere('key', $key);
if ($task === null) {
return;
}
if (! in_array($key, $this->startedKeys, true)) {
$this->startedKeys[] = $key;
}
// Start navigates to the dedicated Run page (subject-specific quiz)
// instead of opening a modal — same data but per-subject questions
// and a structured URL.
$this->redirectRoute('tasks.run', ['key' => $key], navigate: true);
}
public function repeatTask(string $key): void
{
$this->startTask($key);
}
#[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');
}
}