92 lines
2.4 KiB
PHP
Raw Normal View History

2021-06-19 18:16:09 +03:00
<?php
namespace App\Http\Livewire\Common\Notifications;
use Livewire\Component;
use Livewire\WithPagination;
use Illuminate\Support\Collection;
use Illuminate\Notifications\DatabaseNotification;
use Illuminate\Support\Str;
class Exports extends Component
{
use WithPagination;
protected $listeners = [
'refreshParent' => '$notifications',
];
public function markRead($notification_id)
{
$notification = DatabaseNotification::find($notification_id);
$data = $notification->getAttribute('data');
$notification->markAsRead();
2021-06-20 16:40:46 +03:00
$type = isset($data['file_name']) ?: trans('general.export');
2021-06-19 18:16:09 +03:00
$this->dispatchBrowserEvent('mark-read', [
'type' => 'export',
2021-06-20 16:40:46 +03:00
'message' => trans('notifications.messages.mark_read', ['type' => $type]),
2021-06-19 18:16:09 +03:00
]);
}
public function markReadAll()
{
$notifications = $this->getNotifications();
foreach ($notifications as $notification) {
$notification->markAsRead();
}
$this->dispatchBrowserEvent('mark-read-all', [
'type' => 'export',
'message' => trans('notifications.messages.mark_read_all', ['type' => trans('general.export')]),
]);
}
public function render()
{
$limit = 5;
$notifications = $this->getNotifications($limit);
return view('livewire.common.notifications.exports', compact('notifications'));
}
protected function getNotifications($limit = false)
{
$query = user()->notifications()->unread()
2021-06-25 12:24:28 +03:00
->where(function ($query) {
$query->where('type', 'App\Notifications\Common\ExportCompleted')
->orWhere('type', 'App\Notifications\Common\ExportFailed');
});
2021-06-19 18:16:09 +03:00
if ($limit) {
$notifications = $query->paginate($limit);
} else {
$notifications = $query->get();
}
if ($notifications) {
2021-06-19 18:16:09 +03:00
$items = [];
foreach ($notifications as $key => $notification) {
2021-06-19 18:16:09 +03:00
$data = (object) $notification->getAttribute('data');
$data->notification_id = $notification->getAttribute('id');
$items[] = $data;
}
$notifications->setCollection(Collection::make($items));
}
return $notifications;
}
public function paginationView()
{
return 'vendor.livewire.default';
}
}