diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index ec39726..14c3767 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -138,8 +138,13 @@ public function index(Request $request) return [ 'id' => $notification->id, 'data' => $notification->data, + 'timestamp' => $notification->updated_at->format('Y-m-d H:i') . ' UTC', ]; }) + ->sortBy([ + ['timestamp', 'desc'] + ]) + ->values() ->all(); return view('home.index', compact( diff --git a/app/Notifications/HealthCheckIssue.php b/app/Notifications/HealthCheckIssue.php index 5fd7e71..05dcbba 100644 --- a/app/Notifications/HealthCheckIssue.php +++ b/app/Notifications/HealthCheckIssue.php @@ -8,12 +8,14 @@ use App\Models\YnhServer; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Notification; +use Illuminate\Support\Str; class HealthCheckIssue extends Notification { use Queueable; private YnhServer $server; + private string $group; /** * Create a new notification instance. @@ -21,6 +23,7 @@ class HealthCheckIssue extends Notification public function __construct(YnhServer $server) { $this->server = $server; + $this->group = Str::random(10); } /** @@ -50,6 +53,7 @@ public function via(object $notifiable): array public function toArray(object $notifiable): array { return [ + 'group' => $this->group, 'type' => NotificationTypeEnum::HEALTHCHECK_ISSUE->value, 'level' => NotificationLevelEnum::DANGER->value, 'message' => "A health check issue has been detected: no metrics have been recorded in the past 20 minutes.", diff --git a/resources/views/layouts/_notifications.blade.php b/resources/views/layouts/_notifications.blade.php index 05bb156..180b95d 100644 --- a/resources/views/layouts/_notifications.blade.php +++ b/resources/views/layouts/_notifications.blade.php @@ -14,18 +14,21 @@ let notifications = @json($notifications); function dismissNotification(notificationId) { - axios.get(`{{ url('/notification/${notificationId}/dismiss') }}`).then(response => { + axios.get(`{{ url('/notifications/${notificationId}/dismiss') }}`).then(response => { toaster.el.toast('The notification has been dismissed!', 'success'); }).catch(error => { toaster.el.toast('An error occurred.', 'danger'); console.error('Error:', error); }); - notifications = notifications.filter(notification => notification.id !== notificationId); - drawer25.redraw(); + const notification = notifications.find(notif => notif.id === notificationId); + if (notification) { + notifications = notifications.filter(notif => notif.data.group !== notification.data.group); + } + drawer33.redraw(); } function showNotifications() { - drawer25.render = () => { + drawer33.render = () => { const rows = notifications.map(notification => { let details = ''; for (let key in notification.data.details) { @@ -40,7 +43,7 @@ function showNotifications() { return `
-
${notification.data.type}
+
${notification.data.type} / ${notification.timestamp}

${notification.data.message}

DETAILS
-
+
@stack('alpine') @@ -179,21 +179,21 @@ el: new com.computablefacts.blueprintjs.MinimalToaster(document.getElementById('toaster')), toast: (msg, intent) => toaster.el.toast(msg, intent) }; - const drawer25 = { - el: new com.computablefacts.blueprintjs.MinimalDrawer(document.getElementById('drawer-25'), '25%'), + const drawer33 = { + el: new com.computablefacts.blueprintjs.MinimalDrawer(document.getElementById('drawer-33'), '33%'), redraw: null, render: null }; - drawer25.el.onOpen(el => { + drawer33.el.onOpen(el => { // console.log(drawer); const div = document.createElement('div'); - div.innerHTML = drawer25.render ? drawer25.render() : ''; + div.innerHTML = drawer33.render ? drawer33.render() : ''; el.appendChild(div); - drawer25.redraw = () => div.innerHTML = drawer25.render ? drawer25.render() : ''; + drawer33.redraw = () => div.innerHTML = drawer33.render ? drawer33.render() : ''; }); - drawer25.el.onClose(() => { - drawer25.redraw = null; - drawer25.render = null; + drawer33.el.onClose(() => { + drawer33.redraw = null; + drawer33.render = null; }); diff --git a/routes/web.php b/routes/web.php index e3a74bf..fd1fdb6 100644 --- a/routes/web.php +++ b/routes/web.php @@ -399,8 +399,11 @@ return view('auth.passwords.email', compact('email')); })->middleware('auth')->name('reset-password'); -Route::get('/notification/{notification}/dismiss', function (\Illuminate\Notifications\DatabaseNotification $notification, \Illuminate\Http\Request $request) { - $notification->markAsRead(); +Route::get('/notifications/{notification}/dismiss', function (\Illuminate\Notifications\DatabaseNotification $notification, \Illuminate\Http\Request $request) { + \Illuminate\Notifications\DatabaseNotification::query() + ->whereJsonContains('data->group', $notification->data['group']) + ->get() + ->each(fn($notif) => $notif->markAsRead()); })->middleware('auth'); Route::group(['prefix' => 'ynh', 'as' => 'ynh.'], function () {