disable read all button if it has no unread messages#2534
Open
swarakaka wants to merge 1 commit intoorchidsoftware:masterfrom
Open
disable read all button if it has no unread messages#2534swarakaka wants to merge 1 commit intoorchidsoftware:masterfrom
swarakaka wants to merge 1 commit intoorchidsoftware:masterfrom
Conversation
tabuna
reviewed
Mar 1, 2023
| $this->isNotEmpty = $notifications->isNotEmpty(); | ||
|
|
||
| $unreadNotifications = $request->user()->unreadNotifications; | ||
| $this->hasUnread = boolval(count($unreadNotifications)); |
Member
There was a problem hiding this comment.
I think we can write it down better as:
$request->user()->unreadNotifications()->exists()But it still doesn't take type into account. Because there can be different types of notification in the application. And also use the automatic filling of public properties.
I think it should look something like this:
/**
* @var bool
*/
public $isNotEmpty = false;
/**
* @var bool
*/
public $hasUnread = false;
/**
* Query data.
*
* @return array
*/
public function query(Request $request): iterable
{
/** @var \Illuminate\Pagination\LengthAwarePaginator $notifications */
$prepare = $request->user()
->notifications()
->where('type', DashboardMessage::class);
$notifications = $prepare->paginate(10);
return [
'notifications' => $notifications,
'isNotEmpty' => $notifications->total() > 0,
'hasUnread' => $prepare->unread()->exists(),
];
}
/**
* Button commands.
*
* @return Action[]
*/
public function commandBar(): iterable
{
return [
Button::make(__('Remove All'))
->icon('trash')
->method('removeAll')
->confirm(__('After deleting notifications, this action cannot be undone and all associated data will be permanently lost.'))
->disabled(!$this->isNotEmpty),
Button::make(__('Mark All As Read'))
->icon('eye')
->method('markAllAsRead')
->disabled(!$this->hasUnread),
];
}What do you think about it?
Contributor
Author
There was a problem hiding this comment.
However, the values of the $isNotEmpty and $hasUnread variables do not change within the query, that is, they always return the base value and other variables are created. So both buttons will always remain disabled.
solution:
public function query(Request $request): iterable
{
/** @var \Illuminate\Pagination\LengthAwarePaginator $notifications */
$prepare = $request->user()
->notifications()
->where('type', DashboardMessage::class);
$notifications = $prepare->paginate(10);
$this->isNotEmpty = $notifications->total() > 0;
$this->hasUnread = $prepare->unread()->exists();
return [
'notifications' => $notifications,
];
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.