Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/Livewire/CommentList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions src/Livewire/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
25 changes: 25 additions & 0 deletions tests/Livewire/CommentListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
14 changes: 14 additions & 0 deletions tests/Livewire/CommentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading