Skip to content

Commit a2fb9f6

Browse files
authored
Merge pull request #101 from kirschbaum-development/fix/84-sidebar-not-hiding
fix: honor disableSidebar() and hideSubscribers() on CommentsEntry
2 parents d12f650 + 9708179 commit a2fb9f6

10 files changed

Lines changed: 170 additions & 15 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
:load-more-label="$getLoadMoreLabel()"
1010
:per-page-increment="$getPerPageIncrement()"
1111
:sidebar-enabled="$isSidebarEnabled()"
12+
:show-subscribers="$showSubscribers()"
1213
:tip-tap-css-classes="$getTipTapCssClasses()"
1314
/>
1415
</x-dynamic-component>

src/Filament/Actions/CommentsAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected function setUp(): void
3636
'showSubscribers' => $this->showSubscribers(),
3737
'tipTapCssClasses' => $this->getTipTapCssClasses(),
3838
]))
39-
->modalWidth($this->isSidebarEnabled() ? '4xl' : 'xl')
39+
->modalWidth(fn () => $this->isSidebarEnabled() ? '4xl' : 'xl')
4040
->label(__('commentions::comments.label'))
4141
->modalSubmitAction(false)
4242
->modalCancelAction(false)

src/Filament/Actions/CommentsTableAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected function setUp(): void
3030
'showSubscribers' => $this->showSubscribers(),
3131
'tipTapCssClasses' => $this->getTipTapCssClasses(),
3232
]))
33-
->modalWidth($this->isSidebarEnabled() ? '4xl' : 'xl')
33+
->modalWidth(fn () => $this->isSidebarEnabled() ? '4xl' : 'xl')
3434
->label(__('commentions::comments.label'))
3535
->modalSubmitAction(false)
3636
->modalCancelAction(false)

src/Filament/Concerns/HasSidebar.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
trait HasSidebar
1111
{
12-
protected bool $sidebarEnabled = true;
12+
protected ?bool $sidebarEnabled = null;
1313

14-
protected bool $showSubscribers = true;
14+
protected ?bool $showSubscribers = null;
1515

1616
/** @var (Closure(Model): string)|string|null */
1717
protected $subscribeLabelOverride = null;
@@ -45,7 +45,7 @@ public function disableSidebar(bool $condition = true): static
4545

4646
public function isSidebarEnabled(): bool
4747
{
48-
return $this->sidebarEnabled;
48+
return $this->sidebarEnabled ?? (bool) config('commentions.subscriptions.show_sidebar', true);
4949
}
5050

5151
public function hideSubscribers(bool $condition = true): static
@@ -57,7 +57,7 @@ public function hideSubscribers(bool $condition = true): static
5757

5858
public function showSubscribers(): bool
5959
{
60-
return $this->showSubscribers;
60+
return $this->showSubscribers ?? (bool) config('commentions.subscriptions.show_subscribers', true);
6161
}
6262

6363
public function subscribeLabel(Closure|string $label): static

src/Livewire/Concerns/HasSidebar.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ trait HasSidebar
1515

1616
public ?bool $showSubscribers = null;
1717

18-
public function mountHasSidebar(?bool $enableSidebar = null, ?bool $showSubscribers = null): void
18+
public function mountHasSidebar(?bool $sidebarEnabled = null, ?bool $showSubscribers = null): void
1919
{
20-
$this->sidebarEnabled = $enableSidebar ?? (bool) config('commentions.subscriptions.show_sidebar');
20+
$this->sidebarEnabled = $sidebarEnabled ?? (bool) config('commentions.subscriptions.show_sidebar');
2121

2222
$this->showSubscribers = $showSubscribers ?? (bool) config('commentions.subscriptions.show_subscribers', true);
2323
}

src/Livewire/SubscriptionSidebar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class SubscriptionSidebar extends Component
1616
public function mount(Model $record, ?bool $showSubscribers = null): void
1717
{
1818
$this->record = $record;
19-
$this->mountHasSidebar(true, $showSubscribers);
19+
$this->mountHasSidebar(config('commentions.subscriptions.show_sidebar', true), $showSubscribers);
2020
}
2121

2222
#[On('commentions:subscription:toggled')]
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
use Filament\Schemas\Concerns\InteractsWithSchemas;
4+
use Filament\Schemas\Contracts\HasSchemas;
5+
use Filament\Schemas\Schema;
6+
use Illuminate\Support\Facades\Auth;
7+
use Kirschbaum\Commentions\Config;
8+
use Kirschbaum\Commentions\Filament\Infolists\Components\CommentsEntry;
9+
use Livewire\Component;
10+
use Tests\Models\Post;
11+
use Tests\Models\User;
12+
13+
use function Pest\Laravel\actingAs;
14+
use function Pest\Livewire\livewire;
15+
16+
beforeEach(function () {
17+
Config::resolveAuthenticatedUserUsing(fn () => Auth::user());
18+
});
19+
20+
/**
21+
* Minimal Livewire host that renders the package's `CommentsEntry`
22+
* inside a Filament schema, exercising the real infolist blade view.
23+
*/
24+
class CommentsEntryTestHarness extends Component implements HasSchemas
25+
{
26+
use InteractsWithSchemas;
27+
28+
public Post $record;
29+
30+
public bool $disableSidebar = false;
31+
32+
public bool $hideSubscribers = false;
33+
34+
public function commentsInfolist(Schema $schema): Schema
35+
{
36+
return $schema
37+
->record($this->record)
38+
->components([
39+
CommentsEntry::make('comments')
40+
->disableSidebar($this->disableSidebar)
41+
->hideSubscribers($this->hideSubscribers),
42+
]);
43+
}
44+
45+
public function render()
46+
{
47+
return <<<'BLADE'
48+
<div>{{ $this->getSchema('commentsInfolist') }}</div>
49+
BLADE;
50+
}
51+
}
52+
53+
test('CommentsEntry disableSidebar removes the subscription sidebar', function () {
54+
/** @var User $user */
55+
$user = User::factory()->create();
56+
actingAs($user);
57+
58+
/** @var Post $post */
59+
$post = Post::factory()->create();
60+
$post->subscribe(User::factory()->create(['name' => 'Subscriber Sam']));
61+
62+
livewire(CommentsEntryTestHarness::class, [
63+
'record' => $post,
64+
'disableSidebar' => false,
65+
])->assertSee('Subscriber Sam');
66+
67+
livewire(CommentsEntryTestHarness::class, [
68+
'record' => $post,
69+
'disableSidebar' => true,
70+
])->assertDontSee('Subscriber Sam');
71+
});
72+
73+
test('CommentsEntry hideSubscribers hides the subscribers list', function () {
74+
/** @var User $user */
75+
$user = User::factory()->create();
76+
actingAs($user);
77+
78+
/** @var Post $post */
79+
$post = Post::factory()->create();
80+
$post->subscribe(User::factory()->create(['name' => 'Subscriber Sam']));
81+
82+
livewire(CommentsEntryTestHarness::class, [
83+
'record' => $post,
84+
'hideSubscribers' => false,
85+
])->assertSee('Subscriber Sam');
86+
87+
livewire(CommentsEntryTestHarness::class, [
88+
'record' => $post,
89+
'hideSubscribers' => true,
90+
])->assertDontSee('Subscriber Sam');
91+
});

tests/Livewire/CommentsSubscriptionTest.php

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Kirschbaum\Commentions\CommentSubscription;
77
use Kirschbaum\Commentions\Config;
88
use Kirschbaum\Commentions\Livewire\Comments;
9+
use Kirschbaum\Commentions\Livewire\SubscriptionSidebar;
910
use Tests\Models\Post;
1011
use Tests\Models\User;
1112

@@ -25,11 +26,47 @@
2526

2627
livewire(Comments::class, [
2728
'record' => $post,
28-
// Livewire binds mount* params by name; HasSidebar expects `enableSidebar`.
29-
'enableSidebar' => false,
29+
// The blade views pass this as `:sidebar-enabled`, so the mount hook
30+
// parameter must match the `sidebarEnabled` property name.
31+
'sidebarEnabled' => false,
3032
])->assertSet('resolvedSidebarEnabled', false);
3133
});
3234

35+
test('subscriber visibility can be disabled via parameter', function () {
36+
/** @var User $user */
37+
$user = User::factory()->create();
38+
actingAs($user);
39+
40+
$post = Post::factory()->create();
41+
42+
livewire(Comments::class, [
43+
'record' => $post,
44+
'showSubscribers' => false,
45+
])->assertSet('resolvedShowSubscribers', false);
46+
});
47+
48+
test('the subscription sidebar hides the subscribers list when showSubscribers is false', function () {
49+
/** @var User $user */
50+
$user = User::factory()->create();
51+
actingAs($user);
52+
53+
/** @var Post $post */
54+
$post = Post::factory()->create();
55+
56+
$subscriber = User::factory()->create(['name' => 'Subscriber Sam']);
57+
$post->subscribe($subscriber);
58+
59+
livewire(SubscriptionSidebar::class, [
60+
'record' => $post,
61+
'showSubscribers' => false,
62+
])->assertDontSee('Subscriber Sam');
63+
64+
livewire(SubscriptionSidebar::class, [
65+
'record' => $post,
66+
'showSubscribers' => true,
67+
])->assertSee('Subscriber Sam');
68+
});
69+
3370
test('canSubscribe reflects auth state', function () {
3471
$post = Post::factory()->create();
3572

@@ -102,8 +139,8 @@
102139
])->exists())->toBeFalse();
103140
});
104141

105-
test('showSubscribers defaults to config when not provided', function () {
106-
config(['commentions.subscriptions.show_subscribers' => false]);
142+
test('showSubscribers defaults to config when not provided', function (bool $showSubscribers) {
143+
config(['commentions.subscriptions.show_subscribers' => $showSubscribers]);
107144

108145
/** @var User $user */
109146
$user = User::factory()->create();
@@ -113,5 +150,8 @@
113150

114151
livewire(Comments::class, [
115152
'record' => $post,
116-
])->assertSet('showSubscribers', false);
117-
});
153+
])->assertSet('showSubscribers', $showSubscribers);
154+
})->with(
155+
['Show subscribers' => true],
156+
['Hide subscribers' => false],
157+
);

tests/Livewire/CommentsTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,20 @@
124124
'record' => $post,
125125
])->assertSee($componentAlias, false);
126126
});
127+
128+
test('showSidebar defaults to config when not provided', function (bool $showSidebar) {
129+
config(['commentions.subscriptions.show_sidebar' => $showSidebar]);
130+
131+
/** @var User $user */
132+
$user = User::factory()->create();
133+
actingAs($user);
134+
135+
$post = Post::factory()->create();
136+
137+
livewire(Comments::class, [
138+
'record' => $post,
139+
])->assertSet('sidebarEnabled', $showSidebar);
140+
})->with(
141+
['Show sidebar' => true],
142+
['Hide sidebar' => false],
143+
);

tests/TestCase.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
use BladeUI\Heroicons\BladeHeroiconsServiceProvider;
66
use BladeUI\Icons\BladeIconsServiceProvider;
7+
use Filament\Actions\ActionsServiceProvider;
78
use Filament\FilamentServiceProvider;
9+
use Filament\Infolists\InfolistsServiceProvider;
10+
use Filament\Schemas\SchemasServiceProvider;
811
use Filament\Support\SupportServiceProvider;
912
use Illuminate\Database\Schema\Blueprint;
1013
use Illuminate\Support\Facades\Schema;
@@ -33,6 +36,9 @@ protected function getPackageProviders($app)
3336
CommentionsServiceProvider::class,
3437
FilamentServiceProvider::class,
3538
SupportServiceProvider::class,
39+
ActionsServiceProvider::class,
40+
SchemasServiceProvider::class,
41+
InfolistsServiceProvider::class,
3642
LivewireServiceProvider::class,
3743
];
3844
}

0 commit comments

Comments
 (0)