diff --git a/app/Http/Controllers/Api/NotificationController.php b/app/Http/Controllers/Api/NotificationController.php index eaf180442..f2ec45f7a 100644 --- a/app/Http/Controllers/Api/NotificationController.php +++ b/app/Http/Controllers/Api/NotificationController.php @@ -28,6 +28,21 @@ public function update(Request $request, $id) $notification->markAsRead(); } + /** + * Mark all notification as read. + * + * @api {put} notifications/all Mark all notifications as read + * @apiGroup Notifications + * @apiName UpdateReadNotifications + * @apiPermission user + * + * @param Request $request + */ + public function updateAll(Request $request) + { + $request->user()->unreadNotifications()->eachById(fn ($n) => $n->markAsRead()); + } + /** * Delete a read notification. * diff --git a/resources/assets/js/core/api/notifications.js b/resources/assets/js/core/api/notifications.js index ed94f8038..f4a89c12f 100644 --- a/resources/assets/js/core/api/notifications.js +++ b/resources/assets/js/core/api/notifications.js @@ -14,5 +14,9 @@ * @type {Vue.resource} */ export default Vue.resource('api/v1/notifications{/id}', {}, { - markRead: {method: 'PUT'} + markRead: {method: 'PUT'}, + markReadAll: { + method: 'PUT', + url: 'api/v1/notifications/all'} }); + diff --git a/resources/assets/js/core/notifications/list.vue b/resources/assets/js/core/notifications/list.vue index bb019462d..51b749b44 100644 --- a/resources/assets/js/core/notifications/list.vue +++ b/resources/assets/js/core/notifications/list.vue @@ -63,6 +63,7 @@ export default { data() { return { notifications: [], + isLoading: false, }; }, computed: { @@ -73,6 +74,22 @@ export default { return Store.countUnread > 0; }, }, + methods:{ + markAllAsRead() { + this.isLoading = true; + return NotificationsApi.markReadAll({}, {}) + .then(() => { + this.notifications.map(item => { + item.read_at = new Date(); + Store.remove(item.id); + }); + }) + .catch(Messages.handleErrorResponse) + .finally(() => { + this.isLoading = false; + }); + } + }, created() { Store.initialize(); this.notifications = Store.all; diff --git a/resources/views/notifications/index.blade.php b/resources/views/notifications/index.blade.php index 25430efe7..cea6cfacd 100644 --- a/resources/views/notifications/index.blade.php +++ b/resources/views/notifications/index.blade.php @@ -19,6 +19,11 @@
+ +
+ @endif