Index.php
1 <?php 2 3 namespace App\Http\Livewire\Notifications; 4 5 use App\Http\Services\NotificationService; 6 use App\Models\Notification; 7 use App\Traits\WithPerPagePagination; 8 use Illuminate\Foundation\Auth\Access\AuthorizesRequests; 9 use Illuminate\Support\Facades\DB; 10 use Livewire\Component; 11 use Livewire\WithPagination; 12 13 class Index extends Component 14 { 15 use WithPagination, WithPerPagePagination, AuthorizesRequests; 16 17 public bool $readyToLoad = false; 18 19 public array $filters = [ 20 'created_at_since' => null, 21 'created_at_to' => null, 22 'readed' => false, 23 'types' => [], 24 ]; 25 26 protected $rules = [ 27 'filters.created_at_since' => 'nullable|date', 28 'filters.created_at_to' => 'nullable|date|after:filters.created_at_since', 29 ]; 30 31 public string $search = ''; 32 33 public string $sortField = 'notifications.created_at'; 34 35 public string $sortDirection = 'desc'; 36 37 public function mount() 38 { 39 $this->authorize('can_read_notifications'); 40 } 41 42 public function fetchData() 43 { 44 $this->readyToLoad = true; 45 } 46 47 protected function getListeners() 48 { 49 return ['refresh' => '$refresh']; 50 } 51 52 public function updatingSearch(): void 53 { 54 $this->resetPage(); 55 } 56 57 public function getTypesProperty() 58 { 59 return DB::table('notifications')->pluck('type')->unique()->map(function ($type) { 60 return [ 61 'value' => $type, 62 'label' => __('notifications.type.' . $type), 63 ]; 64 })->toArray(); 65 } 66 67 public function getRowsQueryProperty() 68 { 69 return NotificationService::searchNotifications($this->search, $this->sortField, $this->sortDirection, $this->filters); 70 } 71 72 public function getNotificationsProperty() 73 { 74 return $this->readyToLoad ? $this->applyPagination($this->rowsQuery) : []; 75 } 76 77 public function render() 78 { 79 return view('livewire.notifications.index') 80 ->layoutData([ 81 'title' => __('notifications.title'), 82 'breadcrumb' => 'notifications.index', 83 ]); 84 } 85 }