From 73288f023ca0523552e61fe9bec9e38940b91573 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Dalmolin?= Date: Sat, 12 Sep 2026 08:10:43 -0300 Subject: [PATCH] feat: Allow forcing a refresh of the comments list (#105) Dispatch commentions:refresh to reload the list immediately, e.g. after saving a record so custom getComments() items appear without polling. --- README.md | 17 +++++++++++++++++ src/Livewire/CommentList.php | 1 + src/Livewire/Comments.php | 6 ++++++ tests/Livewire/CommentListTest.php | 25 +++++++++++++++++++++++++ tests/Livewire/CommentsTest.php | 14 ++++++++++++++ 5 files changed, 63 insertions(+) diff --git a/README.md b/README.md index 062846d..7fe4cc2 100644 --- a/README.md +++ b/README.md @@ -892,6 +892,23 @@ Infolists\Components\Section::make('Comments') ]), ``` +### Refreshing comments + +To reload the comments list immediately (for example after saving the record, so custom `getComments()` items such as activity-log entries appear without waiting for polling), dispatch the `commentions:refresh` Livewire event: + +```php +protected function afterSave(): void +{ + $this->dispatch('commentions:refresh'); +} +``` + +Or from JavaScript: + +```js +Livewire.dispatch('commentions:refresh') +``` + ### Rendering non-Comments in the list Sometimes you might want to render non-Comments in the list of comments. For example, you might want to render when the status of a project is changed. For this, you can override the `getComments` method in your model, and return instances of the `Kirschbaum\Commentions\RenderableComment` data object. diff --git a/src/Livewire/CommentList.php b/src/Livewire/CommentList.php index 0ed6a33..94cbf11 100644 --- a/src/Livewire/CommentList.php +++ b/src/Livewire/CommentList.php @@ -45,6 +45,7 @@ public function comments(): Collection #[On('comment:saved')] #[On('comment:updated')] #[On('comment:deleted')] + #[On('commentions:refresh')] public function reloadComments(): void { unset($this->comments); diff --git a/src/Livewire/Comments.php b/src/Livewire/Comments.php index a707ba4..bff9bd0 100644 --- a/src/Livewire/Comments.php +++ b/src/Livewire/Comments.php @@ -98,6 +98,12 @@ public function save(): void $this->dispatch('comment:saved'); } + #[On('commentions:refresh')] + public function refreshComments(): void + { + $this->dispatch('comment:saved'); + } + public function render() { return view('commentions::comments'); diff --git a/tests/Livewire/CommentListTest.php b/tests/Livewire/CommentListTest.php index 962bcbd..2f174a9 100644 --- a/tests/Livewire/CommentListTest.php +++ b/tests/Livewire/CommentListTest.php @@ -161,6 +161,31 @@ function assertCommentKey($component, string $class, int|string $id): void ->assertSee('System message'); }); +test('CommentList reloads comments when commentions:refresh is dispatched', function () { + /** @var User $user */ + $user = User::factory()->create(); + /** @var Post $post */ + $post = Post::factory()->create(); + + CommentModel::factory()->author($user)->commentable($post)->create([ + 'body' => 'Existing comment', + ]); + + $component = livewire(CommentList::class, [ + 'record' => $post, + 'paginate' => false, + ])->assertSee('Existing comment'); + + CommentModel::factory()->author($user)->commentable($post)->create([ + 'body' => 'Comment added after render', + ]); + + $component + ->assertDontSee('Comment added after render') + ->dispatch('commentions:refresh') + ->assertSee('Comment added after render'); +}); + test('CommentList renders Comment and RenderableComment sharing an id without key collision', function () { /** @var User $user */ $user = User::factory()->create(); diff --git a/tests/Livewire/CommentsTest.php b/tests/Livewire/CommentsTest.php index a0b886c..72cfc5b 100644 --- a/tests/Livewire/CommentsTest.php +++ b/tests/Livewire/CommentsTest.php @@ -45,6 +45,20 @@ }); }); +test('Comments forwards commentions:refresh so the list reloads', function () { + /** @var User $user */ + $user = User::factory()->create(); + actingAs($user); + + $post = Post::factory()->create(); + + livewire(Comments::class, [ + 'record' => $post, + ]) + ->dispatch('commentions:refresh') + ->assertDispatched('comment:saved'); +}); + test('comment creation requires body', function () { /** @var User $user */ $user = User::factory()->create();