Skip to content

Commit 86883da

Browse files
authored
Merge pull request #679 from code16/hide-globalfilter-if-empty
Hide GlobalFilters if they are empty
2 parents f2d6e1d + 5b9a2d8 commit 86883da

4 files changed

Lines changed: 142 additions & 53 deletions

File tree

src/Filters/GlobalFilters/GlobalFilters.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function getFilters(): array
2020
{
2121
if ($this->globalFilters === null) {
2222
$this->globalFilters = collect(sharp()->config()->get('global_filters'))
23-
->filter(fn (GlobalRequiredFilter $filter) => $filter->authorize())
23+
->filter(fn (GlobalRequiredFilter $filter) => $filter->authorize() && count($filter->cachedValues()))
2424
->values();
2525
}
2626

src/Filters/SelectFilterTrait.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ trait SelectFilterTrait
1212
private bool $isMaster = false;
1313
private bool $isSearchable = false;
1414
private array $searchKeys = ['label'];
15+
private ?array $cachedValues = null;
1516

1617
final public function isMaster(): bool
1718
{
@@ -74,7 +75,7 @@ public function toArray(): array
7475

7576
protected function formattedValues(): array
7677
{
77-
$values = $this->values();
78+
$values = $this->cachedValues();
7879

7980
if (! is_array(collect($values)->first())) {
8081
return collect($values)
@@ -86,5 +87,10 @@ protected function formattedValues(): array
8687
return $values;
8788
}
8889

90+
public function cachedValues(): array
91+
{
92+
return $this->cachedValues ??= $this->values();
93+
}
94+
8995
abstract public function values(): array;
9096
}

src/Http/Middleware/HandleInertiaRequests.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,11 @@ public function share(Request $request)
113113
],
114114
])
115115
: null,
116-
'globalFilters' => app(GlobalFilters::class)->isEnabled()
117-
? GlobalFiltersData::from(app(GlobalFilters::class)->toArray())
118-
: null,
116+
'globalFilters' => transform(app(GlobalFilters::class), function (GlobalFilters $globalFilters) {
117+
return $globalFilters->isEnabled()
118+
? GlobalFiltersData::from($globalFilters->toArray())
119+
: null;
120+
}),
119121
'menu' => fn () => MenuData::from(app(SharpMenuManager::class)),
120122
'auth' => fn () => [
121123
'user' => UserData::from(auth()->user()),

tests/Http/GlobalFilterControllerTest.php

Lines changed: 129 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,134 @@
66

77
beforeEach(function () {
88
login();
9+
});
10+
11+
describe('global filters', function () {
12+
beforeEach(function () {
13+
sharp()->config()->addGlobalFilter(
14+
new class() extends GlobalRequiredFilter
15+
{
16+
public function buildFilterConfig(): void
17+
{
18+
$this->configureKey('test');
19+
}
20+
21+
public function values(): array
22+
{
23+
return [
24+
1 => 'One',
25+
2 => 'Two',
26+
3 => 'Three',
27+
];
28+
}
29+
30+
public function defaultValue(): mixed
31+
{
32+
return 2;
33+
}
34+
}
35+
);
36+
});
37+
38+
it('allows to user to update a global filter', function () {
39+
$this->withoutExceptionHandling();
40+
41+
$this
42+
->followingRedirects()
43+
->post(route('code16.sharp.filters.update', ['2']), ['filterValues' => ['test' => '1']])
44+
->assertOk();
45+
46+
$this->assertEquals('1', sharp()->context()->globalFilterValue('test'));
47+
});
48+
49+
it('sets to global filter to the default value if missing', function () {
50+
$this
51+
->followingRedirects()
52+
->post(route('code16.sharp.filters.update', ['2']), ['filterValues' => []])
53+
->assertOk();
54+
55+
$this->assertEquals(2, sharp()->context()->globalFilterValue('test'));
56+
});
57+
58+
it('does not allow to set a global filter to an unexpected value', function () {
59+
$this
60+
->followingRedirects()
61+
->post(route('code16.sharp.filters.update', ['2']), ['filterValues' => ['test' => 4]])
62+
->assertOk();
63+
64+
$this->assertEquals(2, sharp()->context()->globalFilterValue('test'));
65+
});
66+
67+
it('sends the current value of the global filter with every inertia request', function () {
68+
sharp()->config()->declareEntity(PersonEntity::class);
69+
70+
$this
71+
->followingRedirects()
72+
->get('/sharp/s-list/person')
73+
->assertInertia(fn (Assert $page) => $page
74+
->has('globalFilters.config.filters._root.0', fn (Assert $filter) => $filter
75+
->where('key', 'test')
76+
->where('required', true)
77+
->etc()
78+
)
79+
->where('globalFilters.filterValues.current.test', 2)
80+
->where('globalFilters.filterValues.default.test', 2)
81+
);
82+
83+
$this
84+
->followingRedirects()
85+
->get('/sharp/3/s-list/person')
86+
->assertInertia(fn (Assert $page) => $page
87+
->has('globalFilters.config.filters._root.0', fn (Assert $filter) => $filter
88+
->where('key', 'test')
89+
->etc()
90+
)
91+
->where('globalFilters.filterValues.current.test', 3)
92+
->where('globalFilters.filterValues.default.test', 2)
93+
);
94+
});
95+
});
96+
97+
it('does not sends filters of none configured', function () {
98+
sharp()->config()->declareEntity(PersonEntity::class);
99+
100+
$this
101+
->followingRedirects()
102+
->get('/sharp/s-list/person')
103+
->assertInertia(fn (Assert $page) => $page
104+
->where('globalFilters', null)
105+
);
106+
});
107+
108+
it('does not sends filters of there have no values', function () {
109+
sharp()->config()->declareEntity(PersonEntity::class);
110+
111+
sharp()->config()->addGlobalFilter(
112+
new class() extends GlobalRequiredFilter
113+
{
114+
public function buildFilterConfig(): void
115+
{
116+
$this->configureKey('test-no-values');
117+
}
118+
119+
public function values(): array
120+
{
121+
return [];
122+
}
123+
124+
public function defaultValue(): mixed
125+
{
126+
return null;
127+
}
128+
}
129+
);
130+
131+
$this
132+
->followingRedirects()
133+
->get('/sharp/s-list/person')
134+
->assertInertia(fn (Assert $page) => $page
135+
->where('globalFilters', null)
136+
);
9137

10138
sharp()->config()->addGlobalFilter(
11139
new class() extends GlobalRequiredFilter
@@ -30,62 +158,15 @@ public function defaultValue(): mixed
30158
}
31159
}
32160
);
33-
});
34-
35-
it('allows to user to update a global filter', function () {
36-
$this->withoutExceptionHandling();
37-
38-
$this
39-
->followingRedirects()
40-
->post(route('code16.sharp.filters.update', ['2']), ['filterValues' => ['test' => '1']])
41-
->assertOk();
42-
43-
$this->assertEquals('1', sharp()->context()->globalFilterValue('test'));
44-
});
45-
46-
it('sets to global filter to the default value if missing', function () {
47-
$this
48-
->followingRedirects()
49-
->post(route('code16.sharp.filters.update', ['2']), ['filterValues' => []])
50-
->assertOk();
51-
52-
$this->assertEquals(2, sharp()->context()->globalFilterValue('test'));
53-
});
54-
55-
it('does not allow to set a global filter to an unexpected value', function () {
56-
$this
57-
->followingRedirects()
58-
->post(route('code16.sharp.filters.update', ['2']), ['filterValues' => ['test' => 4]])
59-
->assertOk();
60-
61-
$this->assertEquals(2, sharp()->context()->globalFilterValue('test'));
62-
});
63-
64-
it('sends the current value of the global filter with every inertia request', function () {
65-
sharp()->config()->declareEntity(PersonEntity::class);
66-
67-
$this
68-
->followingRedirects()
69-
->get('/sharp/s-list/person')
70-
->assertInertia(fn (Assert $page) => $page
71-
->has('globalFilters.config.filters._root.0', fn (Assert $filter) => $filter
72-
->where('key', 'test')
73-
->where('required', true)
74-
->etc()
75-
)
76-
->where('globalFilters.filterValues.current.test', 2)
77-
->where('globalFilters.filterValues.default.test', 2)
78-
);
79161

80162
$this
81163
->followingRedirects()
82164
->get('/sharp/3/s-list/person')
83165
->assertInertia(fn (Assert $page) => $page
166+
->has('globalFilters.config.filters._root', 1)
84167
->has('globalFilters.config.filters._root.0', fn (Assert $filter) => $filter
85168
->where('key', 'test')
86169
->etc()
87170
)
88-
->where('globalFilters.filterValues.current.test', 3)
89-
->where('globalFilters.filterValues.default.test', 2)
90171
);
91172
});

0 commit comments

Comments
 (0)