From d61b24dc3267ef31bde45515ef06cbc06d6f6c97 Mon Sep 17 00:00:00 2001 From: Benjamin Takats Date: Sat, 21 Jan 2023 18:52:25 +0100 Subject: [PATCH] search --- app/Http/Livewire/Library/LibraryTable.php | 30 +++++++++++++------ .../livewire/library/library-table.blade.php | 16 +++++----- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/app/Http/Livewire/Library/LibraryTable.php b/app/Http/Livewire/Library/LibraryTable.php index 20952333..acc68301 100644 --- a/app/Http/Livewire/Library/LibraryTable.php +++ b/app/Http/Livewire/Library/LibraryTable.php @@ -15,6 +15,7 @@ class LibraryTable extends Component public Country $country; public array $filters = []; public Collection $libraryItems; + public bool $isLecturerPage = false; public string $search = ''; @@ -23,19 +24,22 @@ class LibraryTable extends Component protected $queryString = [ 'currentTab' => ['except' => '*'], 'filters' => ['except' => ''], - 'search' => ['except' => ''], + 'search' => ['except' => ''], ]; public function mount() { + if (str(request() + ->route() + ->getName())->contains(['.lecturer'])) { + $this->isLecturerPage = true; + } $this->loadLibraryItems($this->search); } public function loadLibraryItems($term = null) { - $shouldBePublic = request() - ->route() - ->getName() !== 'library.table.lecturer'; + $shouldBePublic = !$this->isLecturerPage; if (!$shouldBePublic && !auth()->user()->is_lecturer) { abort(403); } @@ -61,8 +65,9 @@ class LibraryTable extends Component ->when($term, fn($query) => $query ->where('name', 'ilike', '%'.$term.'%') ->orWhere(fn($query) => $query - ->when(count($searchTags) > 0, fn($query) => $query->whereHas('tags', - fn($query) => $query->whereIn('tags.id', $searchTags))) + ->when(count($searchTags) > 0 && count($this->filters) < 1, + fn($query) => $query->whereHas('tags', + fn($query) => $query->whereIn('tags.id', $searchTags))) ) ) ->when($this->currentTab !== '*', fn($query) => $query @@ -88,11 +93,18 @@ class LibraryTable extends Component $this->loadLibraryItems($value); } + public function resetFiltering($isLecturerPage = false) + { + if ($isLecturerPage) { + return to_route('library.table.lecturer', ['country' => $this->country, 'currentTab' => '*']); + } else { + return to_route('library.table.libraryItems', ['country' => $this->country, 'currentTab' => '*']); + } + } + public function render() { - $shouldBePublic = request() - ->route() - ->getName() !== 'library.table.lecturer'; + $shouldBePublic = !$this->isLecturerPage; $libraries = \App\Models\Library::query() ->whereNull('parent_id') ->where('is_public', $shouldBePublic) diff --git a/resources/views/livewire/library/library-table.blade.php b/resources/views/livewire/library/library-table.blade.php index 59f28c18..de989e39 100644 --- a/resources/views/livewire/library/library-table.blade.php +++ b/resources/views/livewire/library/library-table.blade.php @@ -17,6 +17,8 @@ @else

{{ __('Libraries') }}

@endif + {{ __('Reset filtering and search') }}
@@ -37,14 +39,12 @@ @php $currentLibraryClass = $currentTab === $library['name'] ? 'border-amber-500 text-amber-600' : 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300'; @endphp - @if(!request()->isXmlHttpRequest()) - @if(str(request()->route()->getName())->contains(['.lecturer'])) - {{ $library['name'] }} - @else - {{ $library['name'] }} - @endif + @if($isLecturerPage) + {{ $library['name'] }} + @else + {{ $library['name'] }} @endif @endforeach