2021-06-19 18:16:09 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Livewire\Common\Notifications;
|
|
|
|
|
|
|
|
use Livewire\Component;
|
|
|
|
use Livewire\WithPagination;
|
2021-06-19 22:30:14 +03:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Illuminate\Notifications\DatabaseNotification;
|
|
|
|
use Illuminate\Support\Str;
|
2021-06-19 18:16:09 +03:00
|
|
|
|
|
|
|
class Imports extends Component
|
|
|
|
{
|
|
|
|
use WithPagination;
|
|
|
|
|
2021-06-19 22:30:14 +03:00
|
|
|
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['translation']) ?: trans('import.import');
|
|
|
|
|
2021-06-19 22:30:14 +03:00
|
|
|
$this->dispatchBrowserEvent('mark-read', [
|
|
|
|
'type' => 'import',
|
2021-06-20 16:40:46 +03:00
|
|
|
'message' => trans('notifications.messages.mark_read', ['type' => $type]),
|
2021-06-19 22:30:14 +03:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function markReadAll()
|
|
|
|
{
|
|
|
|
$notifications = $this->getNotifications();
|
|
|
|
|
|
|
|
foreach ($notifications as $notification) {
|
|
|
|
$notification->markAsRead();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->dispatchBrowserEvent('mark-read-all', [
|
|
|
|
'type' => 'import',
|
|
|
|
'message' => trans('notifications.messages.mark_read_all', ['type' => trans('import.import')]),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2021-06-19 18:16:09 +03:00
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
$limit = 5;
|
|
|
|
|
2021-06-19 22:30:14 +03:00
|
|
|
$notifications = $this->getNotifications($limit);
|
2021-06-19 18:16:09 +03:00
|
|
|
|
|
|
|
return view('livewire.common.notifications.imports', compact('notifications'));
|
|
|
|
}
|
|
|
|
|
2021-06-19 22:30:14 +03:00
|
|
|
protected function getNotifications($limit = false)
|
|
|
|
{
|
|
|
|
$query = user()->notifications()->unread()
|
|
|
|
->where('type', 'App\Notifications\Common\ImportCompleted')
|
|
|
|
->orWhere('type', 'App\Notifications\Common\ImportFailed');
|
|
|
|
|
|
|
|
if ($limit) {
|
|
|
|
$notifications = $query->paginate($limit);
|
|
|
|
} else {
|
|
|
|
$notifications = $query->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($notifications->items()) {
|
|
|
|
$items = [];
|
|
|
|
|
|
|
|
foreach ($notifications->items() as $key => $notification) {
|
|
|
|
$data = (object) $notification->getAttribute('data');
|
|
|
|
$data->notification_id = $notification->getAttribute('id');
|
|
|
|
|
|
|
|
$items[] = $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
$notifications->setCollection(Collection::make($items));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $notifications;
|
|
|
|
}
|
|
|
|
|
2021-06-19 18:16:09 +03:00
|
|
|
public function paginationView()
|
|
|
|
{
|
|
|
|
return 'vendor.livewire.default';
|
|
|
|
}
|
|
|
|
}
|