Skip to content

Commit

Permalink
Merge pull request biigle#849 from biigle/notification-button
Browse files Browse the repository at this point in the history
Add button to mark all notifications as read
  • Loading branch information
mzur authored Jun 19, 2024
2 parents 7ec0ce9 + 39de4ee commit ae5b437
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 1 deletion.
15 changes: 15 additions & 0 deletions app/Http/Controllers/Api/NotificationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
6 changes: 5 additions & 1 deletion resources/assets/js/core/api/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
});

17 changes: 17 additions & 0 deletions resources/assets/js/core/notifications/list.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export default {
data() {
return {
notifications: [],
isLoading: false,
};
},
computed: {
Expand All @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions resources/views/notifications/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
</ul>
</div>
<div id="notifications-list" class="col-sm-9 col-md-7 col-md-offset-1" v-cloak>
@if (!$all)
<p v-cloak v-if="hasUnreadNotifications" class="clearfix">
<button class="btn btn-default btn-s pull-right" v-on:click="markAllAsRead" v-bind:disabled="isLoading">Mark all as read</button>
</p>
@endif
<notification v-for="item in notifications" v-bind:key="item.id" v-bind:item="item" v-bind:remove-item="{{$all ? 'false': 'true'}}" inline-template>
<div class="panel" v-bind:class="classObject">
<div class="panel-heading">
Expand Down
2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@
'parameters' => ['media-types' => 'id'],
]);

$router->put('notifications/all', 'NotificationController@updateAll');

$router->resource('notifications', 'NotificationController', [
'only' => ['update', 'destroy'],
]);
Expand Down
15 changes: 15 additions & 0 deletions tests/php/Http/Controllers/Api/NotificationControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,19 @@ public function testDestroy()
->assertStatus(200);
$this->assertEquals(0, $user->notifications()->count());
}

public function testUpdateAll()
{
$user = UserTest::create();
$user->notify(new InAppNotification('test', 'test'));
$user->notify(new InAppNotification('test', 'test'));
$user->notify(new InAppNotification('test', 'test'));

$this->doTestApiRoute('PUT', '/api/v1/notifications/all');

$this->be($user);
$this->assertEquals(3, $user->unreadNotifications()->count());
$this->put('/api/v1/notifications/all')->assertSuccessful();
$this->assertEquals(0, $user->unreadNotifications()->count());
}
}

0 comments on commit ae5b437

Please sign in to comment.