From 0c8b3e38ca7436703b59dcec7e5b9d76fa2fdedd Mon Sep 17 00:00:00 2001 From: HDVinnie Date: Thu, 5 May 2022 09:21:09 -0400 Subject: [PATCH] refactor: search to livewire --- .../Controllers/NotificationController.php | 99 +---- app/Http/Livewire/NotificationSearch.php | 161 ++++++++ .../livewire/notification-search.blade.php | 225 +++++++++++ resources/views/notification/index.blade.php | 356 +----------------- .../views/notification/results.blade.php | 54 --- 5 files changed, 390 insertions(+), 505 deletions(-) create mode 100644 app/Http/Livewire/NotificationSearch.php create mode 100644 resources/views/livewire/notification-search.blade.php delete mode 100644 resources/views/notification/results.blade.php diff --git a/app/Http/Controllers/NotificationController.php b/app/Http/Controllers/NotificationController.php index c71de3193..0dd79b8e1 100644 --- a/app/Http/Controllers/NotificationController.php +++ b/app/Http/Controllers/NotificationController.php @@ -26,104 +26,7 @@ class NotificationController extends Controller */ public function index(Request $request): \Illuminate\Contracts\View\Factory|\Illuminate\View\View { - $notifications = $request->user()->notifications()->paginate(25); - - return \view('notification.index', ['notifications' => $notifications]); - } - - /** - * Uses Input's To Put Together A Search. - * - * @throws \Throwable - */ - public function faceted(Request $request) - { - $user = $request->user(); - - $notification = $user->notifications(); - - if ($request->has('bon_gifts') && $request->input('bon_gifts') != null) { - $notification->where('type', '=', \App\Notifications\NewBon::class); - } - - if ($request->has('comments') && $request->input('comments') != null) { - $notification->where('type', '=', \App\Notifications\NewComment::class); - } - - if ($request->has('comment_tags') && $request->input('comment_tags') != null) { - $notification->where('type', '=', \App\Notifications\NewCommentTag::class); - } - - if ($request->has('followers') && $request->input('followers') != null) { - $notification->where('type', '=', \App\Notifications\NewFollow::class); - } - - if ($request->has('posts') && $request->input('posts') != null) { - $notification->where('type', '=', \App\Notifications\NewPost::class); - } - - if ($request->has('post_tags') && $request->input('post_tags') != null) { - $notification->where('type', '=', \App\Notifications\NewPostTag::class); - } - - if ($request->has('post_tips') && $request->input('post_tips') != null) { - $notification->where('type', '=', \App\Notifications\NewPostTip::class); - } - - if ($request->has('request_bounties') && $request->input('request_bounties') != null) { - $notification->where('type', '=', \App\Notifications\NewRequestBounty::class); - } - - if ($request->has('request_claims') && $request->input('request_claims') != null) { - $notification->where('type', '=', \App\Notifications\NewRequestClaim::class); - } - - if ($request->has('request_fills') && $request->input('request_fills') != null) { - $notification->where('type', '=', \App\Notifications\NewRequestFill::class); - } - - if ($request->has('request_approvals') && $request->input('request_approvals') != null) { - $notification->where('type', '=', \App\Notifications\NewRequestFillApprove::class); - } - - if ($request->has('request_rejections') && $request->input('request_rejections') != null) { - $notification->where('type', '=', \App\Notifications\NewRequestFillReject::class); - } - - if ($request->has('request_unclaims') && $request->input('request_unclaims') != null) { - $notification->where('type', '=', \App\Notifications\NewRequestUnclaim::class); - } - - if ($request->has('reseed_requests') && $request->input('reseed_requests') != null) { - $notification->where('type', '=', \App\Notifications\NewReseedRequest::class); - } - - if ($request->has('thanks') && $request->input('thanks') != null) { - $notification->where('type', '=', \App\Notifications\NewThank::class); - } - - if ($request->has('upload_tips') && $request->input('upload_tips') != null) { - $notification->where('type', '=', \App\Notifications\NewUploadTip::class); - } - - if ($request->has('topics') && $request->input('topics') != null) { - $notification->where('type', '=', \App\Notifications\NewTopic::class); - } - - if ($request->has('unfollows') && $request->input('unfollows') != null) { - $notification->where('type', '=', \App\Notifications\NewUnfollow::class); - } - - if ($request->has('uploads') && $request->input('uploads') != null) { - $notification->where('type', '=', \App\Notifications\NewUpload::class); - } - - $notifications = $notification->paginate(25); - - return \view('notification.results', [ - 'user' => $user, - 'notifications' => $notifications, - ])->render(); + return \view('notification.index'); } /** diff --git a/app/Http/Livewire/NotificationSearch.php b/app/Http/Livewire/NotificationSearch.php new file mode 100644 index 000000000..83853046b --- /dev/null +++ b/app/Http/Livewire/NotificationSearch.php @@ -0,0 +1,161 @@ + + * @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0 + */ + +namespace App\Http\Livewire; + +use App\Models\Notification; +use App\Models\User; +use Livewire\Component; +use Livewire\WithPagination; + +class NotificationSearch extends Component +{ + use WithPagination; + + public bool $bon_gifts = false; + + public bool $comment = false; + + public bool $comment_tags = false; + + public bool $followers = false; + + public bool $posts = false; + + public bool $post_tags = false; + + public bool $post_tips = false; + + public bool $request_bounties = false; + + public bool $request_claims = false; + + public bool $request_fills = false; + + public bool $request_approvals = false; + + public bool $request_rejections = false; + + public bool $request_unclaims = false; + + public bool $reseed_requests = false; + + public bool $thanks = false; + + public bool $upload_tips = false; + + public bool $topics = false; + + public bool $unfollows = false; + + public bool $uploads = false; + + public int $perPage = 25; + + public string $sortField = 'created_at'; + + public string $sortDirection = 'desc'; + + final public function paginationView(): string + { + return 'vendor.pagination.livewire-pagination'; + } + + final public function updatedPage(): void + { + $this->emit('paginationChanged'); + } + + final public function getNotificationsProperty(): \Illuminate\Contracts\Pagination\LengthAwarePaginator + { + return \auth()->user()->notifications() + ->when($this->bon_gifts, function ($query) { + $query->where('type', '=', \App\Notifications\NewBon::class); + }) + ->when($this->comment, function ($query) { + $query->where('type', '=', \App\Notifications\NewComment::class); + }) + ->when($this->comment_tags, function ($query) { + $query->where('type', '=', \App\Notifications\NewCommentTag::class); + }) + ->when($this->followers, function ($query) { + $query->where('type', '=', \App\Notifications\NewFollow::class); + }) + ->when($this->posts, function ($query) { + $query->where('type', '=', \App\Notifications\NewPost::class); + }) + ->when($this->post_tags, function ($query) { + $query->where('type', '=', \App\Notifications\NewPostTag::class); + }) + ->when($this->post_tips, function ($query) { + $query->where('type', '=', \App\Notifications\NewPostTip::class); + }) + ->when($this->request_bounties, function ($query) { + $query->where('type', '=', \App\Notifications\NewRequestBounty::class); + }) + ->when($this->request_claims, function ($query) { + $query->where('type', '=', \App\Notifications\NewRequestClaim::class); + }) + ->when($this->request_fills, function ($query) { + $query->where('type', '=', \App\Notifications\NewRequestFill::class); + }) + ->when($this->request_approvals, function ($query) { + $query->where('type', '=', \App\Notifications\NewRequestFillApprove::class); + }) + ->when($this->request_rejections, function ($query) { + $query->where('type', '=', \App\Notifications\NewRequestFillReject::class); + }) + ->when($this->request_unclaims, function ($query) { + $query->where('type', '=', \App\Notifications\NewRequestUnclaim::class); + }) + ->when($this->reseed_requests, function ($query) { + $query->where('type', '=', \App\Notifications\NewReseedRequest::class); + }) + ->when($this->thanks, function ($query) { + $query->where('type', '=', \App\Notifications\NewThank::class); + }) + ->when($this->upload_tips, function ($query) { + $query->where('type', '=', \App\Notifications\NewUploadTip::class); + }) + ->when($this->topics, function ($query) { + $query->where('type', '=', \App\Notifications\NewTopic::class); + }) + ->when($this->unfollows, function ($query) { + $query->where('type', '=', \App\Notifications\NewUnfollow::class); + }) + ->when($this->uploads, function ($query) { + $query->where('type', '=', \App\Notifications\NewUpload::class); + }) + ->orderBy($this->sortField, $this->sortDirection) + ->paginate($this->perPage); + } + + final public function sortBy($field): void + { + if ($this->sortField === $field) { + $this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc'; + } else { + $this->sortDirection = 'asc'; + } + + $this->sortField = $field; + } + + final public function render(): \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Contracts\Foundation\Application + { + return \view('livewire.notification-search', [ + 'user' => User::with(['group'])->findOrFail(\auth()->user()->id), + 'notifications' => $this->notifications, + ]); + } +} diff --git a/resources/views/livewire/notification-search.blade.php b/resources/views/livewire/notification-search.blade.php new file mode 100644 index 000000000..2c0544aa0 --- /dev/null +++ b/resources/views/livewire/notification-search.blade.php @@ -0,0 +1,225 @@ +
+
+
+

{{ __('notification.filter-by-type') }}

+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ +
+
+ @csrf + +
+ +
+ @csrf + @method('DELETE') + +
+
+
+ +
+
+
+ + + + + + + + + + + + @forelse($notifications as $notification) + + + + + + + + @empty + {{ __('notification.no-notifications') }}. + @endforelse + +
{{ __('notification.title') }}{{ __('notification.message') }}{{ __('notification.date') }}{{ __('notification.read') }}{{ __('notification.delete') }}
+ + {{ $notification->data['title'] }} + + + {{ $notification->data['body'] }} + + {{ $notification->created_at->diffForHumans() }} + +
+ @csrf + +
+
+
+ @csrf + @method('DELETE') + +
+
+
{{ $notifications->links() }}
+
+
+
+
\ No newline at end of file diff --git a/resources/views/notification/index.blade.php b/resources/views/notification/index.blade.php index e805bc2d2..bb1a3ff9e 100644 --- a/resources/views/notification/index.blade.php +++ b/resources/views/notification/index.blade.php @@ -13,357 +13,7 @@ @endsection @section('content') -
-
-

{{ __('notification.filter-by-type') }}

-
-
- @csrf -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
-
- -
-
-
-
- @csrf - -
- -
- @csrf - @method('DELETE') - -
-
- +
+ @livewire('notification-search')
- -
-
-
- @include('notification.results') -
-
-
-@endsection - -@section('javascripts') - - - - - - - - - - - -@endsection +@endsection \ No newline at end of file diff --git a/resources/views/notification/results.blade.php b/resources/views/notification/results.blade.php deleted file mode 100644 index 6768a6d46..000000000 --- a/resources/views/notification/results.blade.php +++ /dev/null @@ -1,54 +0,0 @@ -
- - - - - - - - - - - - @forelse($notifications as $notification) - - - - - - - - @empty - {{ __('notification.no-notifications') }}. - @endforelse - -
{{ __('notification.title') }}{{ __('notification.message') }}{{ __('notification.date') }}{{ __('notification.read') }}{{ __('notification.delete') }}
- - {{ $notification->data['title'] }} - - - {{ $notification->data['body'] }} - - {{ $notification->created_at->diffForHumans() }} - -
- @csrf - -
-
-
- @csrf - @method('DELETE') - -
-
-
{{ $notifications->links() }}
-