Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add button to mark all notifications as read #849

Merged
merged 18 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions app/Http/Controllers/Api/NotificationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Biigle\Http\Controllers\Api;

use Biigle\Http\Requests\UpdateAllNotifications;
use Illuminate\Http\Request;
use Illuminate\Notifications\DatabaseNotification;

class NotificationController extends Controller
{
Expand All @@ -28,6 +30,29 @@ public function update(Request $request, $id)
$notification->markAsRead();
}

/**
* Mark all notification as read.
*
* @api {put} notifications/ Mark all notifications as read
* @apiGroup Notifications
* @apiName UpdateReadNotifications
* @apiPermission user
*
* @apiParamExample {String} Request example:
* id: "0972569c-2d3e-444d-8e7d-2054e7ab20e9"
*
mzur marked this conversation as resolved.
Show resolved Hide resolved
* @param Request $request
* @return \Illuminate\Http\Response
*/
public function updateAll(UpdateAllNotifications $request)
{
$userId = $request->input('user_id');
$notifications = DatabaseNotification::where('notifiable_id', '=', $userId)->whereNull('read_at')->get();
$notifications->map(function ($n) {
$n->markAsRead();
});
mzur marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Delete a read notification.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function index(Request $request, Guard $auth)
return view('notifications.index', [
'all' => $all,
'notifications' => $notifications,
'user_id' => $user->id,
]);
}
}
34 changes: 34 additions & 0 deletions app/Http/Requests/UpdateAllNotifications.php
mzur marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Biigle\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UpdateAllNotifications extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
if (is_null($this->input('user_id')) || !is_numeric($this->input('user_id'))) {
// Skip authorization if the user id could not be found. The validation rules
// will take care of rejecting this request with the proper response code.
return true;
}

return $this->user()->id === $this->input('user_id');
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'user_id' => 'required|numeric'
];
}
}
4 changes: 3 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,7 @@
* @type {Vue.resource}
*/
export default Vue.resource('api/v1/notifications{/id}', {}, {
markRead: {method: 'PUT'}
markRead: {method: 'PUT'},
markReadAll: {method: 'PUT'}
});

18 changes: 18 additions & 0 deletions resources/assets/js/core/notifications/list.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ export default {
return Store.countUnread > 0;
},
},
methods:{
markAllAsRead(userId) {
this.isLoading = true;
return NotificationsApi.markReadAll({}, {user_id: userId})
.then(() => {
this.notifications.map(item => {
item.read_at = new Date();
if (this.removeItem) {
Store.remove(this.item.id);
}
})
})
.catch(Messages.handleErrorResponse)
.finally(() => {
this.isLoading = false;
});
mzur marked this conversation as resolved.
Show resolved Hide resolved
}
},
created() {
Store.initialize();
this.notifications = Store.all;
Expand Down
7 changes: 7 additions & 0 deletions resources/views/notifications/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
</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="row">
<button class="btn btn-default btn-s pull-right" v-on:click="markAllAsRead({{$user_id}})">Mark all as read</button>
</p>
@endif
<div class="row">
mzur marked this conversation as resolved.
Show resolved Hide resolved
<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 All @@ -36,6 +42,7 @@
</div>
</div>
</notification>
</div>
@if ($all)
<p class="text-muted" v-if="!hasNotifications" v-cloak>
You have no notifications.
Expand Down
2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@
'only' => ['update', 'destroy'],
]);

$router->put('notifications', 'NotificationController@updateAll');
mzur marked this conversation as resolved.
Show resolved Hide resolved

$router->resource('projects', 'ProjectController', [
'only' => ['index', 'show', 'update', 'store', 'destroy'],
'parameters' => ['projects' => 'id'],
Expand Down
24 changes: 24 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,28 @@ 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', ['user_id' => $user->id]);
$this->assertEquals(3, $user->unreadNotifications()->count());

$this->be(UserTest::create());
$this->put('/api/v1/notifications/', ['user_id' => $user->id])
->assertForbidden();

$this->be($user);
$this->put('/api/v1/notifications/', ['user_id' => $user->id])
->assertStatus(200);
$this->assertEquals(0, $user->unreadNotifications()->count());

// invalid data
$this->put('/api/v1/notifications/', ['test'])->assertInvalid();
$this->put('/api/v1/notifications/', ['user_id' => 'test'])->assertInvalid();
}
}