Skip to content

Commit

Permalink
Fix: load more bug
Browse files Browse the repository at this point in the history
  • Loading branch information
awcodes committed Oct 21, 2023
1 parent bc1a766 commit 493d8a9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
2 changes: 2 additions & 0 deletions resources/views/components/modals/curator-panel.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ class="curator-panel h-full absolute inset-0 flex flex-col"
>
{{ __('curator::views.panel.deselect_all') }}
</x-filament::button>
@if($currentPage < $lastPage)
<x-filament::button
size="xs"
color="gray"
wire:click="loadMoreFiles()"
>
{{ __('curator::views.panel.load_more') }}
</x-filament::button>
@endif
@if ($isMultiple)
<p class="text-xs">Cmd + Click to select multiple files.</p>
@endif
Expand Down
48 changes: 33 additions & 15 deletions src/Components/Modals/CuratorPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Storage;
use Livewire\Component;
use Livewire\WithPagination;
use Symfony\Component\HttpFoundation\StreamedResponse;

class CuratorPanel extends Component implements HasForms, HasActions
{
use InteractsWithActions;
use InteractsWithForms;
use WithPagination;

public array $acceptedFileTypes = [];

Expand Down Expand Up @@ -68,6 +70,8 @@ class CuratorPanel extends Component implements HasForms, HasActions

public array $selected = [];

public int $defaultLimit = 25;

public ?string $statePath;

public bool $shouldPreserveFilenames = false;
Expand All @@ -80,6 +84,12 @@ class CuratorPanel extends Component implements HasForms, HasActions

public array $originalFilenames = [];

public int $currentPage = 0;

public int $mediaCount = 0;

public int $lastPage = 0;

public function mount(): void
{
$this->form->fill();
Expand Down Expand Up @@ -127,9 +137,9 @@ public function form(Form $form): Form
])->statePath('data');
}

public function getFiles(int $offset = 0): array
public function getFiles(int $page = 0, bool $excludeSelected = false): array
{
$files = App::make(Media::class)
$files = App::make(Media::class)->query()
->when($this->selected, function ($query, $selected) {
$selected = collect($selected)->pluck('id')->toArray();

Expand All @@ -145,38 +155,46 @@ public function getFiles(int $offset = 0): array
$wildcardTypes?->map(fn($type) => $query->orWhere('type', 'LIKE', str_replace('*', '%', $type)));

return $query;
})
->latest()
->offset($offset)
->limit(25)
->get();
});

$paginator = $files->paginate($this->defaultLimit, page: $page);

if ($this->selected) {
$this->currentPage = $paginator->currentPage();
$this->mediaCount = $paginator->total();
$this->lastPage = $paginator->lastPage();

$items = $paginator->items();

if (!$excludeSelected && $this->selected) {
$selected = collect($this->selected)->pluck('id')->toArray();

App::make(Media::class)
$selectedItems = App::make(Media::class)
->whereIn('id', $selected)
->get()
->sortBy(function ($model) use ($selected) {
return array_search($model->id, $selected);
})
->reverse()
->map(function ($item) use ($files) {
$files->prepend($item);
});

array_unshift($items, ...$selectedItems);

$this->setMediaForm();
$this->context = count($this->selected) === 1 ? 'edit' : 'create';
}

return $files->toArray();
return collect($items)->map(function ($item) {
return $item->toArray();
})->toArray();
}

public function loadMoreFiles(): void
{
if ($this->currentPage === $this->lastPage) {
return;
}

$this->files = [
...$this->files,
...$this->getFiles(count($this->files)),
...$this->getFiles($this->currentPage + 1, true),
];
}

Expand Down

0 comments on commit 493d8a9

Please sign in to comment.