|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Livewire; |
| 4 | + |
| 5 | +use App\Policies\NotificationPolicy; |
| 6 | +use Carbon\Carbon; |
| 7 | +use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
| 8 | +use Illuminate\Notifications\DatabaseNotification; |
| 9 | +use Illuminate\Support\Facades\Auth; |
| 10 | +use Livewire\Component; |
| 11 | +use WireUi\Traits\Actions; |
| 12 | + |
| 13 | +class Notifications extends Component |
| 14 | +{ |
| 15 | + use Actions, AuthorizesRequests; |
| 16 | + |
| 17 | + public $notificationId; |
| 18 | + |
| 19 | + public function mount(): void |
| 20 | + { |
| 21 | + abort_if(Auth::guest(), 403); |
| 22 | + } |
| 23 | + |
| 24 | + public function getNotificationProperty(): DatabaseNotification |
| 25 | + { |
| 26 | + return DatabaseNotification::findOrFail($this->notificationId); |
| 27 | + } |
| 28 | + |
| 29 | + public function markAsRead(string $notificationId): void |
| 30 | + { |
| 31 | + $this->notificationId = $notificationId; |
| 32 | + |
| 33 | + $this->authorize(NotificationPolicy::MARK_AS_READ, $this->notification); |
| 34 | + |
| 35 | + $this->notification->markAsRead(); |
| 36 | + |
| 37 | + $this->notification()->success('Notification', 'Cette notification a été marquée comme lue.'); |
| 38 | + |
| 39 | + $this->emit('NotificationMarkedAsRead', Auth::user()->unreadNotifications()->count()); |
| 40 | + } |
| 41 | + |
| 42 | + public function render() |
| 43 | + { |
| 44 | + return view('livewire.notifications', [ |
| 45 | + 'notifications' => Auth::user() |
| 46 | + ->unreadNotifications() |
| 47 | + ->take(10) |
| 48 | + ->get() |
| 49 | + ->groupBy( |
| 50 | + fn ($notification) => Carbon::parse($notification->created_at)->format('M, Y') |
| 51 | + ), |
| 52 | + ]); |
| 53 | + } |
| 54 | +} |
0 commit comments