Skip to content

Commit

Permalink
feat: updated pagination logic and added loading screen while fetchin…
Browse files Browse the repository at this point in the history
…g results. fixes #220
  • Loading branch information
CS76 committed Aug 29, 2024
1 parent dd17dfb commit 7fabdd4
Show file tree
Hide file tree
Showing 10 changed files with 371 additions and 26 deletions.
6 changes: 3 additions & 3 deletions app/Console/Commands/OrganismDedupeOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class OrganismDedupeOptions extends Command
*
* @var string
*/
protected $signature = 'app:organism-molecule-counts';
protected $signature = 'app:organism-dedupe';

/**
* The console command description.
Expand All @@ -32,8 +32,8 @@ public function handle()
{
$this->info('Updating missing slugs...');

Organism::whereNotNull('slug')
->chunk(100, function ($organisms) {
Organism::whereNull('slug')
->chunk(1000, function ($organisms) {
$organisms->each(function ($organism) {
$slug = Str::slug($organism->name);
$organism->update(['slug' => $slug]);
Expand Down
2 changes: 1 addition & 1 deletion app/Console/Commands/OrganismMoleculeCounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function handle()
->update(['molecule_count' => $count->count]);
}

$this->info('Updatin the residual organisms.');
$this->info('Updating the residual organisms.');

DB::table('organisms')
->whereNotIn('id', $moleculeCounts->pluck('id'))
Expand Down
20 changes: 13 additions & 7 deletions app/Livewire/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public function placeholder()
HTML;
}

public function updatingPage($page)
{
$this->page = $page;
}

public function updatedQuery()
{
$this->page = 1;
Expand All @@ -84,14 +89,15 @@ public function search(SearchMolecule $search)
public function render(SearchMolecule $search)
{
try {
$cacheKey = 'search.'.md5($this->query.$this->size.$this->type.$this->sort.$this->tagType.$this->page);

$results = Cache::remember($cacheKey, now()->addDay(), function () use ($search) {
return $search->query($this->query, $this->size, $this->type, $this->sort, $this->tagType, $this->page);
});

$this->collection = $results[1];
$this->organisms = $results[2];

if ($this->query == '') {
$results = Cache::rememberForever('search.default', function () use ($search) {
return $search->query($this->query, $this->size, $this->type, $this->sort, $this->tagType, $this->page);
});
} else {
$results = $search->query($this->query, $this->size, $this->type, $this->sort, $this->tagType, $this->page);
}
$this->collection = $results[1];
$this->organisms = $results[2];

Expand Down
22 changes: 11 additions & 11 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added public/img/loading.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 6 additions & 4 deletions resources/views/livewire/search.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ class="text-base font-semibold leading-7 text-secondary-dark text-sm">
<div class="sm:hidden">
<label for="tabs" class="sr-only">Select a tab</label>
<select id="tabs" name="tabs" x-model="activeTab"
class="block w-full rounded-md border-gray-300 py-2 pl-3 pr-10 text-base focus:border-indigo-500 focus:outline-none focus:ring-indigo-500 sm:text-sm"
>
class="block w-full rounded-md border-gray-300 py-2 pl-3 pr-10 text-base focus:border-indigo-500 focus:outline-none focus:ring-indigo-500 sm:text-sm">
<option value="molecules">Molecules</option>
<option value="organism">Organism</option>
<option value="citations">Citations</option>
Expand Down Expand Up @@ -252,15 +251,18 @@ class="rounded-md bg-secondary-dark px-3.5 py-1.5 text-base font-semibold leadin
<div class="p-4 w-full">
{{ $molecules->links() }}
</div>
<div class="relative items-center block p-6 bg-white dark:bg-gray-800 dark:border-gray-800 dark:hover:bg-gray-700">
<div class="grid grid-cols-1 gap-y-4 sm:grid-cols-2 sm:gap-x-6 sm:gap-y-10 lg:grid-cols-4 lg:gap-x-8">
@foreach ($molecules as $molecule)
<livewire:molecule-card :key="$molecule->identifier" :molecule="$molecule" />
@endforeach

</div>
<div class="p-4">
{{ $molecules->links() }}
<div wire:loading role="status" class="border rounded-lg shadow-md opacity-90 absolute -translate-x-1/2 top-24 left-1/2 text-center justify-center">
<img class="w-full rounded-md" alt="loading" src="/img/loading.gif" />
</div>
</div>
</div>
@else
<div class="text-center pt-10 mt-10">
<svg class="w-12 h-12 mx-auto" viewBox="0 0 78 78" fill="none" xmlns="http://www.w3.org/2000/svg">
Expand Down
102 changes: 102 additions & 0 deletions resources/views/vendor/livewire/bootstrap.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
@php
if (! isset($scrollTo)) {
$scrollTo = 'body';
}
$scrollIntoViewJsSnippet = ($scrollTo !== false)
? <<<JS
(\$el.closest('{$scrollTo}') || document.querySelector('{$scrollTo}')).scrollIntoView()
JS
: '';
@endphp
<div>
@if ($paginator->hasPages())
<nav class="d-flex justify-items-center justify-content-between">
<div class="d-flex justify-content-between flex-fill d-sm-none">
<ul class="pagination">
{{-- Previous Page Link --}}
@if ($paginator->onFirstPage())
<li class="page-item disabled" aria-disabled="true">
<span class="page-link">@lang('pagination.previous')</span>
</li>
@else
<li class="page-item">
<button type="button" dusk="previousPage{{ $paginator->getPageName() == 'page' ? '' : '.' . $paginator->getPageName() }}" class="page-link" wire:click="previousPage('{{ $paginator->getPageName() }}')" x-on:click="{{ $scrollIntoViewJsSnippet }}" wire:loading.attr="disabled">@lang('pagination.previous')</button>
</li>
@endif
{{-- Next Page Link --}}
@if ($paginator->hasMorePages())
<li class="page-item">
<button type="button" dusk="nextPage{{ $paginator->getPageName() == 'page' ? '' : '.' . $paginator->getPageName() }}" class="page-link" wire:click="nextPage('{{ $paginator->getPageName() }}')" x-on:click="{{ $scrollIntoViewJsSnippet }}" wire:loading.attr="disabled">@lang('pagination.next')</button>
</li>
@else
<li class="page-item disabled" aria-disabled="true">
<span class="page-link" aria-hidden="true">@lang('pagination.next')</span>
</li>
@endif
</ul>
</div>
<div class="d-none flex-sm-fill d-sm-flex align-items-sm-center justify-content-sm-between">
<div>
<p class="small text-muted">
{!! __('Showing') !!}
<span class="fw-semibold">{{ $paginator->firstItem() }}</span>
{!! __('to') !!}
<span class="fw-semibold">{{ $paginator->lastItem() }}</span>
{!! __('of') !!}
<span class="fw-semibold">{{ $paginator->total() }}</span>
{!! __('results') !!}
</p>
</div>
<div>
<ul class="pagination">
{{-- Previous Page Link --}}
@if ($paginator->onFirstPage())
<li class="page-item disabled" aria-disabled="true" aria-label="@lang('pagination.previous')">
<span class="page-link" aria-hidden="true">&lsaquo;</span>
</li>
@else
<li class="page-item">
<button type="button" dusk="previousPage{{ $paginator->getPageName() == 'page' ? '' : '.' . $paginator->getPageName() }}" class="page-link" wire:click="previousPage('{{ $paginator->getPageName() }}')" x-on:click="{{ $scrollIntoViewJsSnippet }}" wire:loading.attr="disabled" aria-label="@lang('pagination.previous')">&lsaquo;</button>
</li>
@endif
{{-- Pagination Elements --}}
@foreach ($elements as $element)
{{-- "Three Dots" Separator --}}
@if (is_string($element))
<li class="page-item disabled" aria-disabled="true"><span class="page-link">{{ $element }}</span></li>
@endif
{{-- Array Of Links --}}
@if (is_array($element))
@foreach ($element as $page => $url)
@if ($page == $paginator->currentPage())
<li class="page-item active" wire:key="paginator-{{ $paginator->getPageName() }}-page-{{ $page }}" aria-current="page"><span class="page-link">{{ $page }}</span></li>
@else
<li class="page-item" wire:key="paginator-{{ $paginator->getPageName() }}-page-{{ $page }}"><button type="button" class="page-link" wire:click="gotoPage({{ $page }}, '{{ $paginator->getPageName() }}')" x-on:click="{{ $scrollIntoViewJsSnippet }}">{{ $page }}</button></li>
@endif
@endforeach
@endif
@endforeach
{{-- Next Page Link --}}
@if ($paginator->hasMorePages())
<li class="page-item">
<button type="button" dusk="nextPage{{ $paginator->getPageName() == 'page' ? '' : '.' . $paginator->getPageName() }}" class="page-link" wire:click="nextPage('{{ $paginator->getPageName() }}')" x-on:click="{{ $scrollIntoViewJsSnippet }}" wire:loading.attr="disabled" aria-label="@lang('pagination.next')">&rsaquo;</button>
</li>
@else
<li class="page-item disabled" aria-disabled="true" aria-label="@lang('pagination.next')">
<span class="page-link" aria-hidden="true">&rsaquo;</span>
</li>
@endif
</ul>
</div>
</div>
</nav>
@endif
</div>
53 changes: 53 additions & 0 deletions resources/views/vendor/livewire/simple-bootstrap.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
@php
if (! isset($scrollTo)) {
$scrollTo = 'body';
}
$scrollIntoViewJsSnippet = ($scrollTo !== false)
? <<<JS
(\$el.closest('{$scrollTo}') || document.querySelector('{$scrollTo}')).scrollIntoView()
JS
: '';
@endphp
<div>
@if ($paginator->hasPages())
<nav>
<ul class="pagination">
{{-- Previous Page Link --}}
@if ($paginator->onFirstPage())
<li class="page-item disabled" aria-disabled="true">
<span class="page-link">@lang('pagination.previous')</span>
</li>
@else
@if(method_exists($paginator,'getCursorName'))
<li class="page-item">
<button dusk="previousPage" type="button" class="page-link" wire:key="cursor-{{ $paginator->getCursorName() }}-{{ $paginator->previousCursor()->encode() }}" wire:click="setPage('{{$paginator->previousCursor()->encode()}}','{{ $paginator->getCursorName() }}')" x-on:click="{{ $scrollIntoViewJsSnippet }}" wire:loading.attr="disabled">@lang('pagination.previous')</button>
</li>
@else
<li class="page-item">
<button type="button" dusk="previousPage{{ $paginator->getPageName() == 'page' ? '' : '.' . $paginator->getPageName() }}" class="page-link" wire:click="previousPage('{{ $paginator->getPageName() }}')" x-on:click="{{ $scrollIntoViewJsSnippet }}" wire:loading.attr="disabled">@lang('pagination.previous')</button>
</li>
@endif
@endif
{{-- Next Page Link --}}
@if ($paginator->hasMorePages())
@if(method_exists($paginator,'getCursorName'))
<li class="page-item">
<button dusk="nextPage" type="button" class="page-link" wire:key="cursor-{{ $paginator->getCursorName() }}-{{ $paginator->nextCursor()->encode() }}" wire:click="setPage('{{$paginator->nextCursor()->encode()}}','{{ $paginator->getCursorName() }}')" x-on:click="{{ $scrollIntoViewJsSnippet }}" wire:loading.attr="disabled">@lang('pagination.next')</button>
</li>
@else
<li class="page-item">
<button type="button" dusk="nextPage{{ $paginator->getPageName() == 'page' ? '' : '.' . $paginator->getPageName() }}" class="page-link" wire:click="nextPage('{{ $paginator->getPageName() }}')" x-on:click="{{ $scrollIntoViewJsSnippet }}" wire:loading.attr="disabled">@lang('pagination.next')</button>
</li>
@endif
@else
<li class="page-item disabled" aria-disabled="true">
<span class="page-link">@lang('pagination.next')</span>
</li>
@endif
</ul>
</nav>
@endif
</div>
Loading

0 comments on commit 7fabdd4

Please sign in to comment.