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
2 changes: 1 addition & 1 deletion src/Filters/GlobalFilters/GlobalFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function getFilters(): array
{
if ($this->globalFilters === null) {
$this->globalFilters = collect(sharp()->config()->get('global_filters'))
->filter(fn (GlobalRequiredFilter $filter) => $filter->authorize())
->filter(fn (GlobalRequiredFilter $filter) => $filter->authorize() && count($filter->cachedValues()))
->values();
}

Expand Down
8 changes: 7 additions & 1 deletion src/Filters/SelectFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ trait SelectFilterTrait
private bool $isMaster = false;
private bool $isSearchable = false;
private array $searchKeys = ['label'];
private ?array $cachedValues = null;

final public function isMaster(): bool
{
Expand Down Expand Up @@ -74,7 +75,7 @@ public function toArray(): array

protected function formattedValues(): array
{
$values = $this->values();
$values = $this->cachedValues();

if (! is_array(collect($values)->first())) {
return collect($values)
Expand All @@ -86,5 +87,10 @@ protected function formattedValues(): array
return $values;
}

public function cachedValues(): array
{
return $this->cachedValues ??= $this->values();
}

abstract public function values(): array;
}
8 changes: 5 additions & 3 deletions src/Http/Middleware/HandleInertiaRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@ public function share(Request $request)
],
])
: null,
'globalFilters' => app(GlobalFilters::class)->isEnabled()
? GlobalFiltersData::from(app(GlobalFilters::class)->toArray())
: null,
'globalFilters' => transform(app(GlobalFilters::class), function (GlobalFilters $globalFilters) {
return $globalFilters->isEnabled()
? GlobalFiltersData::from($globalFilters->toArray())
: null;
}),
'menu' => fn () => MenuData::from(app(SharpMenuManager::class)),
'auth' => fn () => [
'user' => UserData::from(auth()->user()),
Expand Down
177 changes: 129 additions & 48 deletions tests/Http/GlobalFilterControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,134 @@

beforeEach(function () {
login();
});

describe('global filters', function () {
beforeEach(function () {
sharp()->config()->addGlobalFilter(
new class() extends GlobalRequiredFilter
{
public function buildFilterConfig(): void
{
$this->configureKey('test');
}

public function values(): array
{
return [
1 => 'One',
2 => 'Two',
3 => 'Three',
];
}

public function defaultValue(): mixed
{
return 2;
}
}
);
});

it('allows to user to update a global filter', function () {
$this->withoutExceptionHandling();

$this
->followingRedirects()
->post(route('code16.sharp.filters.update', ['2']), ['filterValues' => ['test' => '1']])
->assertOk();

$this->assertEquals('1', sharp()->context()->globalFilterValue('test'));
});

it('sets to global filter to the default value if missing', function () {
$this
->followingRedirects()
->post(route('code16.sharp.filters.update', ['2']), ['filterValues' => []])
->assertOk();

$this->assertEquals(2, sharp()->context()->globalFilterValue('test'));
});

it('does not allow to set a global filter to an unexpected value', function () {
$this
->followingRedirects()
->post(route('code16.sharp.filters.update', ['2']), ['filterValues' => ['test' => 4]])
->assertOk();

$this->assertEquals(2, sharp()->context()->globalFilterValue('test'));
});

it('sends the current value of the global filter with every inertia request', function () {
sharp()->config()->declareEntity(PersonEntity::class);

$this
->followingRedirects()
->get('/sharp/s-list/person')
->assertInertia(fn (Assert $page) => $page
->has('globalFilters.config.filters._root.0', fn (Assert $filter) => $filter
->where('key', 'test')
->where('required', true)
->etc()
)
->where('globalFilters.filterValues.current.test', 2)
->where('globalFilters.filterValues.default.test', 2)
);

$this
->followingRedirects()
->get('/sharp/3/s-list/person')
->assertInertia(fn (Assert $page) => $page
->has('globalFilters.config.filters._root.0', fn (Assert $filter) => $filter
->where('key', 'test')
->etc()
)
->where('globalFilters.filterValues.current.test', 3)
->where('globalFilters.filterValues.default.test', 2)
);
});
});

it('does not sends filters of none configured', function () {
sharp()->config()->declareEntity(PersonEntity::class);

$this
->followingRedirects()
->get('/sharp/s-list/person')
->assertInertia(fn (Assert $page) => $page
->where('globalFilters', null)
);
});

it('does not sends filters of there have no values', function () {
sharp()->config()->declareEntity(PersonEntity::class);

sharp()->config()->addGlobalFilter(
new class() extends GlobalRequiredFilter
{
public function buildFilterConfig(): void
{
$this->configureKey('test-no-values');
}

public function values(): array
{
return [];
}

public function defaultValue(): mixed
{
return null;
}
}
);

$this
->followingRedirects()
->get('/sharp/s-list/person')
->assertInertia(fn (Assert $page) => $page
->where('globalFilters', null)
);

sharp()->config()->addGlobalFilter(
new class() extends GlobalRequiredFilter
Expand All @@ -30,62 +158,15 @@ public function defaultValue(): mixed
}
}
);
});

it('allows to user to update a global filter', function () {
$this->withoutExceptionHandling();

$this
->followingRedirects()
->post(route('code16.sharp.filters.update', ['2']), ['filterValues' => ['test' => '1']])
->assertOk();

$this->assertEquals('1', sharp()->context()->globalFilterValue('test'));
});

it('sets to global filter to the default value if missing', function () {
$this
->followingRedirects()
->post(route('code16.sharp.filters.update', ['2']), ['filterValues' => []])
->assertOk();

$this->assertEquals(2, sharp()->context()->globalFilterValue('test'));
});

it('does not allow to set a global filter to an unexpected value', function () {
$this
->followingRedirects()
->post(route('code16.sharp.filters.update', ['2']), ['filterValues' => ['test' => 4]])
->assertOk();

$this->assertEquals(2, sharp()->context()->globalFilterValue('test'));
});

it('sends the current value of the global filter with every inertia request', function () {
sharp()->config()->declareEntity(PersonEntity::class);

$this
->followingRedirects()
->get('/sharp/s-list/person')
->assertInertia(fn (Assert $page) => $page
->has('globalFilters.config.filters._root.0', fn (Assert $filter) => $filter
->where('key', 'test')
->where('required', true)
->etc()
)
->where('globalFilters.filterValues.current.test', 2)
->where('globalFilters.filterValues.default.test', 2)
);

$this
->followingRedirects()
->get('/sharp/3/s-list/person')
->assertInertia(fn (Assert $page) => $page
->has('globalFilters.config.filters._root', 1)
->has('globalFilters.config.filters._root.0', fn (Assert $filter) => $filter
->where('key', 'test')
->etc()
)
->where('globalFilters.filterValues.current.test', 3)
->where('globalFilters.filterValues.default.test', 2)
);
});
Loading