1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xetaravel\Livewire\User; |
4
|
|
|
|
5
|
|
|
use Illuminate\Notifications\DatabaseNotificationCollection; |
6
|
|
|
use Livewire\Component; |
7
|
|
|
|
8
|
|
|
class Notification extends Component |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* All notifications from the user. |
12
|
|
|
* |
13
|
|
|
* @var DatabaseNotificationCollection |
14
|
|
|
*/ |
15
|
|
|
public DatabaseNotificationCollection $notifications; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Whatever the user has un-read notifications. |
19
|
|
|
* |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
public bool $hasUnreadNotifications = false; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The number of un-red notifications. |
26
|
|
|
* |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
public int $unreadNotificationsCount = 0; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The mount function. |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
public function mount(): void |
37
|
|
|
{ |
38
|
|
|
$this->fetchData(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function render() |
42
|
|
|
{ |
43
|
|
|
return view('livewire.user.notification'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Remove a notification by its id. |
48
|
|
|
* |
49
|
|
|
* @param string $notificationId The id of the notification to remove. |
50
|
|
|
* |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
|
|
public function remove(string $notificationId): void |
54
|
|
|
{ |
55
|
|
|
$notification = auth()->user()->notifications() |
56
|
|
|
->where('id', $notificationId) |
57
|
|
|
->first(); |
58
|
|
|
|
59
|
|
|
// That means the notification id has been modified in front-end. |
60
|
|
|
if (!$notification) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$notification->delete(); |
65
|
|
|
|
66
|
|
|
$this->fetchData(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Mark the notification as read by its id. |
71
|
|
|
* |
72
|
|
|
* @param string $notificationId The id of the notification to mark as read. |
73
|
|
|
* |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
public function markAsRead(string $notificationId): void |
77
|
|
|
{ |
78
|
|
|
$notification = auth()->user()->notifications() |
79
|
|
|
->where('id', $notificationId) |
80
|
|
|
->first(); |
81
|
|
|
|
82
|
|
|
// That means the notification id has been modified in front-end. |
83
|
|
|
if (!$notification) { |
84
|
|
|
return; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$notification->markAsRead(); |
88
|
|
|
|
89
|
|
|
$this->fetchData(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Mark all notifications from the user as read. |
94
|
|
|
* |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
|
|
public function markAllNotificationsAsRead(): void |
98
|
|
|
{ |
99
|
|
|
auth()->user()->unreadNotifications->markAsRead(); |
100
|
|
|
|
101
|
|
|
$this->notifications = auth()->user()->notifications; |
102
|
|
|
$this->unreadNotificationsCount = 0; |
103
|
|
|
$this->hasUnreadNotifications = false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Used to refresh the notifications and the unread count. |
108
|
|
|
* |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
|
|
private function fetchData(): void |
112
|
|
|
{ |
113
|
|
|
$this->notifications = auth()->user()->notifications; |
114
|
|
|
|
115
|
|
|
// Filter only the unread notifications |
116
|
|
|
$unreadNotifications = $this->notifications->filter(function($notification) { |
117
|
|
|
return $notification->read_at == null; |
118
|
|
|
}); |
119
|
|
|
$this->unreadNotificationsCount = $unreadNotifications->count(); |
120
|
|
|
|
121
|
|
|
$this->hasUnreadNotifications = $this->unreadNotificationsCount > 0; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|