Skip to content

Commit 694611e

Browse files
authored
ISSUE-38: Add pagination support for comments (#40)
* Add pagination support for comments * Add test script to composer.json and update CommentsTest to throw TypeError * Update perPageIncrement to correctly fallback to perPage * Style Fix
1 parent ebc9551 commit 694611e

File tree

14 files changed

+217
-6
lines changed

14 files changed

+217
-6
lines changed

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,59 @@ You can publish the configuration file to make changes.
130130
php artisan vendor:publish --tag="commentions-config"
131131
```
132132

133+
#### Pagination (Filament)
134+
135+
Commentions supports built-in pagination for the embedded list of comments and it is enabled by default. You can disable it or control the number of comments shown per page and per click.
136+
137+
- Enabled by default
138+
- Disable via `disablePagination()`
139+
- Configure page size
140+
- Customize the load more label
141+
- Control how many comments are appended per click (defaults to the page size)
142+
143+
Examples:
144+
145+
Default Usage:
146+
147+
```php
148+
use Kirschbaum\Commentions\Filament\Actions\CommentsAction;
149+
150+
->recordActions([
151+
CommentsAction::make()
152+
->mentionables(User::all())
153+
->perPage(10)
154+
155+
])
156+
```
157+
Without Pagination:
158+
159+
```php
160+
use Kirschbaum\Commentions\Filament\Actions\CommentsAction;
161+
162+
->recordActions([
163+
CommentsAction::make()
164+
->mentionables(User::all())
165+
->disablePagination();
166+
167+
])
168+
```
169+
Advanced Usage:
170+
171+
```php
172+
use Kirschbaum\Commentions\Filament\Infolists\Components\CommentsEntry;
173+
174+
Infolists\Components\Section::make('Comments')
175+
->schema([
176+
CommentsEntry::make('comments')
177+
->mentionables(fn (Model $record) => User::all())
178+
->perPage(8)
179+
->loadMoreIncrementsBy(8)
180+
->loadMoreLabel('Show older'),
181+
])
182+
```
183+
184+
When pagination is enabled, a "Show more" button is displayed to load additional comments incrementally.
185+
133186
#### Configuring the User model and the mentionables
134187

135188
If your `User` model lives in a different namespace than `App\Models\User`, you can configure it in `config/commentions.php`:

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
}
5353
},
5454
"scripts": {
55+
"test": "vendor/bin/pest",
5556
"fix-style": "./vendor/bin/pint --config pint.json",
5657
"check-style": "./vendor/bin/pint --config pint.json --test",
5758
"static-analysis": "./vendor/bin/phpstan analyse -c phpstan.neon"

resources/views/comment-list.blade.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,16 @@ class="comm:w-8 comm:h-8 comm:text-gray-400 comm:dark:text-gray-500"
2121
:mentionables="$mentionables"
2222
/>
2323
@endforeach
24+
25+
@if ($this->hasMore)
26+
<div class="comm:mt-2">
27+
<x-filament::button
28+
color="gray"
29+
size="sm"
30+
wire:click="loadMore"
31+
wire:target="loadMore"
32+
wire:loading.attr="disabled"
33+
>{{ $loadMoreLabel }}</x-filament::button>
34+
</div>
35+
@endif
2436
</div>

resources/views/comments-modal.blade.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,9 @@
44
:record="$record"
55
:mentionables="$mentionables"
66
:polling-interval="$pollingInterval"
7+
:paginate="$paginate ?? true"
8+
:per-page="$perPage ?? 5"
9+
:load-more-label="$loadMoreLabel ?? 'Show more'"
10+
:per-page-increment="$perPageIncrement ?? null"
711
/>
812
</div>

resources/views/comments.blade.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,9 @@
3434
:record="$record"
3535
:mentionables="$this->mentions"
3636
:polling-interval="$pollingInterval"
37+
:paginate="$paginate ?? true"
38+
:per-page="$perPage ?? 5"
39+
:load-more-label="$loadMoreLabel ?? 'Show more'"
40+
:per-page-increment="$perPageIncrement ?? null"
3741
/>
3842
</div>

resources/views/filament/infolists/components/comments-entry.blade.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@
33
:record="$getRecord()"
44
:mentionables="$getMentionables()"
55
:polling-interval="$getPollingInterval()"
6+
:paginate="$isPaginated()"
7+
:per-page="$getPerPage()"
8+
:load-more-label="$getLoadMoreLabel()"
9+
:per-page-increment="$getPerPageIncrement()"
610
/>
711
</x-dynamic-component>

src/Filament/Actions/CommentsAction.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
use Filament\Actions\Action;
66
use Illuminate\Database\Eloquent\Model;
77
use Kirschbaum\Commentions\Filament\Concerns\HasMentionables;
8+
use Kirschbaum\Commentions\Filament\Concerns\HasPagination;
89
use Kirschbaum\Commentions\Filament\Concerns\HasPolling;
910

1011
class CommentsAction extends Action
1112
{
1213
use HasMentionables;
14+
use HasPagination;
1315
use HasPolling;
1416

1517
protected function setUp(): void
@@ -22,6 +24,10 @@ protected function setUp(): void
2224
'record' => $record,
2325
'mentionables' => $this->getMentionables(),
2426
'pollingInterval' => $this->getPollingInterval(),
27+
'paginate' => $this->isPaginated(),
28+
'perPage' => $this->getPerPage(),
29+
'loadMoreLabel' => $this->getLoadMoreLabel(),
30+
'perPageIncrement' => $this->getPerPageIncrement() ?: $this->getPerPage(),
2531
]))
2632
->modalWidth('xl')
2733
->label('Comments')
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Kirschbaum\Commentions\Filament\Concerns;
4+
5+
use Closure;
6+
7+
trait HasPagination
8+
{
9+
protected bool|Closure $paginate = true;
10+
11+
protected int|Closure $perPage = 5;
12+
13+
protected string|Closure $loadMoreLabel = 'Show more';
14+
15+
protected int|Closure|null $perPageIncrement = null;
16+
17+
public function paginate(bool|Closure $enabled = true): static
18+
{
19+
$this->paginate = $enabled;
20+
21+
return $this;
22+
}
23+
24+
public function disablePagination(): static
25+
{
26+
return $this->paginate(false);
27+
}
28+
29+
public function perPage(int|Closure $perPage): static
30+
{
31+
$this->perPage = $perPage;
32+
33+
return $this;
34+
}
35+
36+
public function loadMoreLabel(string|Closure $label): static
37+
{
38+
$this->loadMoreLabel = $label;
39+
40+
return $this;
41+
}
42+
43+
public function loadMoreIncrementsBy(int|Closure $increment): static
44+
{
45+
$this->perPageIncrement = $increment;
46+
47+
return $this;
48+
}
49+
50+
public function isPaginated(): bool
51+
{
52+
return (bool) $this->evaluate($this->paginate);
53+
}
54+
55+
public function getPerPage(): int
56+
{
57+
return (int) $this->evaluate($this->perPage);
58+
}
59+
60+
public function getLoadMoreLabel(): string
61+
{
62+
return (string) $this->evaluate($this->loadMoreLabel);
63+
}
64+
65+
public function getPerPageIncrement(): int
66+
{
67+
$value = $this->evaluate($this->perPageIncrement);
68+
69+
return (int) ($value ?? $this->getPerPage());
70+
}
71+
}

src/Filament/Infolists/Components/CommentsEntry.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
use Filament\Infolists\Components\Entry;
66
use Kirschbaum\Commentions\Filament\Concerns\HasMentionables;
7+
use Kirschbaum\Commentions\Filament\Concerns\HasPagination;
78
use Kirschbaum\Commentions\Filament\Concerns\HasPolling;
89

910
class CommentsEntry extends Entry
1011
{
1112
use HasMentionables;
13+
use HasPagination;
1214
use HasPolling;
1315

1416
protected string $view = 'commentions::filament.infolists.components.comments-entry';

src/HasComments.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,20 @@ public function comments(): MorphMany
1414
return $this->morphMany(Config::getCommentModel(), 'commentable');
1515
}
1616

17+
public function commentsQuery(): MorphMany
18+
{
19+
return $this->comments()
20+
->latest()
21+
->with(['author', 'reactions.reactor']);
22+
}
23+
1724
public function comment(string $body, ?Commenter $author): Comment
1825
{
1926
return SaveComment::run($this, $author, $body);
2027
}
2128

2229
public function getComments(): Collection
2330
{
24-
return $this->comments()
25-
->latest()
26-
->with(['author', 'reactions.reactor'])
27-
->get();
31+
return $this->commentsQuery()->get();
2832
}
2933
}

0 commit comments

Comments
 (0)