|
4 | 4 |
|
5 | 5 | namespace App\Livewire\Pages;
|
6 | 6 |
|
| 7 | +use App\Policies\NotificationPolicy; |
| 8 | +use Carbon\Carbon; |
| 9 | +use Filament\Notifications\Notification; |
7 | 10 | use Illuminate\Contracts\View\View;
|
| 11 | +use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
| 12 | +use Illuminate\Notifications\DatabaseNotification; |
| 13 | +use Illuminate\Support\Facades\Auth; |
8 | 14 | use Livewire\Component;
|
9 | 15 |
|
10 | 16 | final class Notifications extends Component
|
11 | 17 | {
|
| 18 | + use AuthorizesRequests; |
| 19 | + |
| 20 | + public string $notificationId; |
| 21 | + |
| 22 | + public function mount(): void |
| 23 | + { |
| 24 | + abort_if(Auth::guest(), 403); |
| 25 | + } |
| 26 | + |
| 27 | + public function getNotificationProperty(): DatabaseNotification |
| 28 | + { |
| 29 | + return DatabaseNotification::findOrFail($this->notificationId); |
| 30 | + } |
| 31 | + |
| 32 | + public function markAsRead(string $notificationId): void |
| 33 | + { |
| 34 | + $this->notificationId = $notificationId; |
| 35 | + |
| 36 | + $this->authorize(NotificationPolicy::MARK_AS_READ, $this->notification); // @phpstan-ignore-line |
| 37 | + |
| 38 | + $this->notification->markAsRead(); // @phpstan-ignore-line |
| 39 | + |
| 40 | + Notification::make() |
| 41 | + ->title(__('Cette notification a été marquée comme lue.')) |
| 42 | + ->success() |
| 43 | + ->seconds(5) |
| 44 | + ->send(); |
| 45 | + |
| 46 | + $this->dispatch('NotificationMarkedAsRead', Auth::user()->unreadNotifications()->count()); // @phpstan-ignore-line |
| 47 | + } |
| 48 | + |
12 | 49 | public function render(): View
|
13 | 50 | {
|
14 |
| - return view('livewire.pages.notifications'); |
| 51 | + return view('livewire.pages.notifications', [ |
| 52 | + // @phpstan-ignore-next-line |
| 53 | + 'notifications' => Auth::user() |
| 54 | + ->unreadNotifications() |
| 55 | + ->take(10) |
| 56 | + ->get() |
| 57 | + ->groupBy( |
| 58 | + fn ($notification) => Carbon::parse($notification->created_at)->format('M, Y') |
| 59 | + ), |
| 60 | + ]); |
15 | 61 | }
|
16 | 62 | }
|
0 commit comments