Skip to content

Commit

Permalink
Merge pull request #9 from ainesophaur/bugfix/blade-rendering-issue
Browse files Browse the repository at this point in the history
Plugin issue with multiple panels
  • Loading branch information
bramr94 authored Jun 19, 2024
2 parents d16f93f + 3041739 commit 3ed0ae5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
32 changes: 22 additions & 10 deletions src/FilamentDeveloperLoginsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DutchCodingCompany\FilamentDeveloperLogins\Livewire\MenuLogins;
use DutchCodingCompany\FilamentDeveloperLogins\View\Components\DeveloperLogins;
use Filament\Facades\Filament;
use Filament\Panel;
use Filament\Support\Concerns\EvaluatesClosures;
use Filament\Support\Facades\FilamentView;
use Filament\View\PanelsRenderHook;
Expand Down Expand Up @@ -44,17 +45,16 @@ public function packageBooted(): void

protected static function registerRenderHooks(): void
{
$panel = Filament::getCurrentPanel();
if (is_null($panel) || ! $panel->hasPlugin('filament-developer-logins')) {
return;
}

/** @var FilamentDeveloperLoginsPlugin $plugin */
$plugin = $panel->getPlugin('filament-developer-logins');

FilamentView::registerRenderHook(
PanelsRenderHook::AUTH_LOGIN_FORM_AFTER,
static function () use ($plugin) : ?string {
static function (): ?string {
$panel = Filament::getCurrentPanel();
if (! self::panelHasPlugin($panel)) {
return null;
}

/** @var FilamentDeveloperLoginsPlugin $plugin */
$plugin = $panel->getPlugin('filament-developer-logins');
if (! $plugin->getEnabled()) {
return null;
}
Expand All @@ -65,7 +65,14 @@ static function () use ($plugin) : ?string {

FilamentView::registerRenderHook(
PanelsRenderHook::GLOBAL_SEARCH_AFTER,
static function () use ($plugin) : ?string {
static function (): ?string {
$panel = Filament::getCurrentPanel();
if (! self::panelHasPlugin($panel)) {
return null;
}

/** @var FilamentDeveloperLoginsPlugin $plugin */
$plugin = $panel->getPlugin('filament-developer-logins');
if (! $plugin->getEnabled() || ! $plugin->getSwitchable()) {
return null;
}
Expand All @@ -74,4 +81,9 @@ static function () use ($plugin) : ?string {
},
);
}

protected static function panelHasPlugin(?Panel $panel): bool
{
return ! is_null($panel) && $panel->hasPlugin('filament-developer-logins');
}
}
16 changes: 16 additions & 0 deletions tests/Feature/MenuLoginsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use DutchCodingCompany\FilamentDeveloperLogins\Tests\TestCase;
use Filament\Facades\Filament;
use Filament\Pages\Dashboard;
use Filament\Panel;
use Livewire\Livewire;

final class MenuLoginsTest extends TestCase
Expand Down Expand Up @@ -80,4 +81,19 @@ public function test_forbidden_is_returned_when_enabled_or_switchable_is_false()
->call('loginAs', '[email protected]')
->assertForbidden();
}

public function test_component_is_only_rendered_in_panel_with_plugin(): void
{
$user = TestUser::factory()->create();

$this->actingAs($user)
->get(Dashboard::getUrl(panel: $this->panelName))
->assertSuccessful()
->assertSeeLivewire(MenuLogins::class);

$this->actingAs($user)
->get(Dashboard::getUrl(panel: $this->panelName.'-other'))
->assertSuccessful()
->assertDontSeeLivewire(MenuLogins::class);
}
}
16 changes: 14 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function setUp(): void
);
}

protected function registerTestPanel(): void
protected function registerTestPanels(): void
{
Filament::registerPanel(
fn (): Panel => Panel::make()
Expand All @@ -57,6 +57,18 @@ protected function registerTestPanel(): void
->modelClass(TestUser::class),
]),
);

Filament::registerPanel(fn () => Panel::make()
->darkMode(false)
->id($this->panelName.'-other')
->path($this->panelName.'-other')
->login()
->pages([
Dashboard::class,
])
->plugins([
]),
);
}

protected function defineRoutes($router): void
Expand All @@ -80,7 +92,7 @@ protected function getEnvironmentSetUp($app): void

protected function getPackageProviders($app): array
{
$this->registerTestPanel();
$this->registerTestPanels();

return [
FilamentServiceProvider::class,
Expand Down

0 comments on commit 3ed0ae5

Please sign in to comment.