lernschiff/app/Livewire/Notifications/Index.php

97 lines
3.6 KiB
PHP

<?php
namespace App\Livewire\Notifications;
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' => 'notifications'])]
#[Lazy]
class Index extends Component
{
public static function placeholder(array $params = []): View
{
return view('livewire.notifications.placeholder');
}
#[Url(as: 'filter', keep: true)]
public string $filter = 'all';
/** @var array<int,string> */
protected array $allowedFilters = ['all', 'unread'];
public array $readKeys = [];
public function mount(): void
{
if (! in_array($this->filter, $this->allowedFilters, true)) {
$this->filter = 'all';
}
}
public function setFilter(string $filter): void
{
if (in_array($filter, $this->allowedFilters, true)) {
$this->filter = $filter;
}
}
public function markRead(string $key): void
{
if (! in_array($key, $this->readKeys, true)) {
$this->readKeys[] = $key;
}
}
public function markAllRead(): void
{
$this->readKeys = array_column($this->notifications(), 'key');
}
#[Computed]
public function notifications(): array
{
$items = [
['key' => 'n1', 'title' => 'Heutige Aufgaben warten', 'body' => 'Du hast noch 4 offene Aufgaben für heute. Streak nicht brechen!', 'time' => 'vor 10 Min.', 'icon' => 'clock', 'color' => 'primary', 'baseRead' => false],
['key' => 'n2', 'title' => 'Neuer Rekord erreicht!', 'body' => 'Du hast 7 Tage am Stück gelernt — neuer persönlicher Rekord.', 'time' => 'vor 2 Std.', 'icon' => 'trophy', 'color' => 'amber', 'baseRead' => false],
['key' => 'n3', 'title' => 'Nachricht von Frau Müller', 'body' => 'Hallo Lina, super Arbeit gestern! Schau dir die Zusatzaufgabe an.', 'time' => 'vor 4 Std.', 'icon' => 'chat-bubble-left-right', 'color' => 'rose', 'baseRead' => false],
['key' => 'n4', 'title' => 'Neues Foto-Hilfe-Feature', 'body' => 'Du kannst jetzt Aufgaben fotografieren und die KI erklärt den Weg.', 'time' => 'gestern', 'icon' => 'sparkles', 'color' => 'violet', 'baseRead' => true],
['key' => 'n5', 'title' => 'Mathematik: 75% erreicht', 'body' => 'Du hast 18 von 24 Lektionen geschafft. Noch 6 bis zum Abschluss.', 'time' => '2 Tage', 'icon' => 'check-badge', 'color' => 'green', 'baseRead' => true],
['key' => 'n6', 'title' => 'Wochenende Übung', 'body' => 'Auch am Wochenende lernen hilft beim Streak. Heute 10 Minuten.', 'time' => '3 Tage', 'icon' => 'calendar-days', 'color' => 'primary', 'baseRead' => true],
];
return array_map(function ($n) {
$n['read'] = $n['baseRead'] || in_array($n['key'], $this->readKeys, true);
return $n;
}, $items);
}
#[Computed]
public function filteredNotifications(): array
{
if ($this->filter === 'unread') {
return array_values(array_filter($this->notifications, fn($n) => ! $n['read']));
}
return $this->notifications;
}
#[Computed]
public function counts(): array
{
return [
'all' => count($this->notifications),
'unread' => count(array_filter($this->notifications, fn($n) => ! $n['read'])),
];
}
public function render(): View
{
return view('livewire.notifications.index');
}
}