Skip to content

Commit

Permalink
Completes #17
Browse files Browse the repository at this point in the history
  • Loading branch information
afonic committed Feb 29, 2024
1 parent 57c62df commit 9f96999
Show file tree
Hide file tree
Showing 16 changed files with 42 additions and 206 deletions.
3 changes: 0 additions & 3 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
// Enable the query string feature of Livewire, saving the filters in the URL
'enable_query_string' => false,

// Only allow filters for fields and conditions already loaded in the page to be applied
'only_allow_active_filters' => true,

// Validate that the values of radio and checkbox filters are in the available options array
'validate_filter_values' => true,

Expand Down
4 changes: 0 additions & 4 deletions src/Http/Livewire/LfCheckboxFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ public function filterOptions()

public function updatedSelected()
{
$this->dispatchFilterMounted();

if (config('statamic-livewire-filters.validate_filter_values')) {
$this->validate();
}
Expand Down Expand Up @@ -82,8 +80,6 @@ public function rules()
#[On('preset-params')]
public function setPresetSort($params)
{
$this->dispatchFilterMounted();

if (array_key_exists($this->getParamKey(), $params)) {
$this->selected = explode('|', $params[$this->getParamKey()]);
$this->previousSelected = $this->selected;
Expand Down
4 changes: 0 additions & 4 deletions src/Http/Livewire/LfDateFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class LfDateFilter extends Component

public function updatedSelected()
{
$this->dispatchFilterMounted();

$this->dispatch('filter-updated',
field: $this->field,
condition: $this->condition,
Expand Down Expand Up @@ -45,8 +43,6 @@ public function clear()
#[On('preset-params')]
public function setPresetSort($params)
{
$this->dispatchFilterMounted();

if (array_key_exists($this->getParamKey(), $params)) {
$this->selected = $params[$this->getParamKey()];
}
Expand Down
4 changes: 0 additions & 4 deletions src/Http/Livewire/LfRadioFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public function filterOptions()

public function updatedSelected()
{
$this->dispatchFilterMounted();

if (config('statamic-livewire-filters.validate_filter_values')) {
$this->validate();
}
Expand Down Expand Up @@ -61,8 +59,6 @@ public function rules()
#[On('preset-params')]
public function setPresetSort($params)
{
$this->dispatchFilterMounted();

if (array_key_exists($this->getParamKey(), $params)) {
$this->selected = $params[$this->getParamKey()];
}
Expand Down
4 changes: 0 additions & 4 deletions src/Http/Livewire/LfRangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public function mount($default)

public function dispatchEvent()
{
$this->dispatchFilterMounted();

$this->dispatch('filter-updated',
field: $this->field,
condition: $this->condition,
Expand All @@ -54,8 +52,6 @@ public function livewireComponentReady()
#[On('preset-params')]
public function setPresetSort($params)
{
$this->dispatchFilterMounted();

if (array_key_exists($this->getParamKey(), $params)) {
$this->selected = $params[$this->getParamKey()];
}
Expand Down
4 changes: 0 additions & 4 deletions src/Http/Livewire/LfTextFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ class LfTextFilter extends Component

public function updatedSelected()
{
$this->dispatchFilterMounted();

$this->dispatch('filter-updated',
field: $this->field,
condition: $this->condition,
Expand All @@ -36,8 +34,6 @@ public function clear()
#[On('preset-params')]
public function setPresetSort($params)
{
$this->dispatchFilterMounted();

if (array_key_exists($this->getParamKey(), $params)) {
$this->selected = $params[$this->getParamKey()];
}
Expand Down
18 changes: 2 additions & 16 deletions src/Http/Livewire/LivewireCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ class LivewireCollection extends Component
public $collections;

#[Locked]
public $filters;
public $allowedFilters;

public $paginate;

public $view = 'livewire-collection';

public function mount($params)
{
$this->filters = collect();
$this->allowedFilters = false;
if (is_null($this->params)) {
$this->setParameters($params);
} else {
Expand All @@ -35,20 +35,6 @@ public function mount($params)
$this->dispatchParamsUpdated();
}

#[On('filter-mounted')]
public function filterMounted($field, $condition, $modifier)
{
if ($condition === 'query_scope') {
$this->filters->push('query_scope:'.$modifier);
$this->filters->push($modifier.':'.$field);
} elseif ($condition === 'taxonomy') {
$this->filters->push('taxonomy:'.$field.':'.$modifier);
} else {
$this->filters->push($field.':'.$condition);
}
$this->filters = $this->filters->unique();
}

#[On('filter-updated')]
public function filterUpdated($field, $condition, $payload, $command, $modifier)
{
Expand Down
10 changes: 5 additions & 5 deletions src/Http/Livewire/Traits/GenerateParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ trait GenerateParams
{
protected function generateParams()
{
$params = config('statamic-livewire-filters.only_allow_active_filters')
? $this->removeParamsNotInFiltersCollection()
$params = ($this->allowedFilters && $this->allowedFilters->isNotEmpty())
? $this->removeParamsNotInAllowedFiltersCollection()
: $this->params;

return Parameters::make(array_merge(
Expand All @@ -30,17 +30,17 @@ protected function generateParamsForCount($collection, $params)
);
}

protected function removeParamsNotInFiltersCollection()
protected function removeParamsNotInAllowedFiltersCollection()
{
return collect($this->params)->filter(function ($value, $key) {
if ($key === 'sort') {
return true;
}
if ($key === 'query_scope') {
return $this->filters->contains('query_scope:'.$value);
return $this->allowedFilters->contains('query_scope:'.$value);
}

return $this->filters->contains($key);
return $this->allowedFilters->contains($key);
})->all();
}
}
8 changes: 8 additions & 0 deletions src/Http/Livewire/Traits/HandleParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public function setParameters($params)
$this->extractCollectionKeys($paramsCollection);
$this->extractView($paramsCollection);
$this->extractPagination($paramsCollection);
$this->extractAllowedFilters($paramsCollection);

$this->params = $paramsCollection->all();
$this->handlePresetParams();
Expand Down Expand Up @@ -43,6 +44,13 @@ protected function extractPagination($paramsCollection)
}
}

protected function extractAllowedFilters($paramsCollection)
{
if ($paramsCollection->has('allowed_filters')) {
$this->allowedFilters = collect(explode('|', $paramsCollection->pull('allowed_filters')));
}
}

protected function handleCondition($field, $condition, $payload, $command)
{
$paramKey = $field.':'.$condition;
Expand Down
10 changes: 0 additions & 10 deletions src/Http/Livewire/Traits/IsLivewireFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,6 @@ public function clearFilters()
->to(LivewireCollection::class);
}

public function dispatchFilterMounted()
{
$this->dispatch('filter-mounted',
field: $this->field,
condition: $this->condition,
modifier: $this->modifier,
)
->to(LivewireCollection::class);
}

protected function getParamKey()
{
if ($this->condition === 'taxonomy') {
Expand Down
15 changes: 0 additions & 15 deletions tests/Feature/LfCheckboxFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,6 @@ public function it_changes_the_value_of_selected_property_when_an_option_is_set_
);
}

/** @test */
public function it_sends_the_filter_mounted_event()
{
Livewire::test(LfCheckboxFilter::class, ['field' => 'item_options', 'blueprint' => 'pages.pages', 'condition' => 'is'])
->assertSet('selected', [])
->set('selected', ['option1'])
->assertSet('selected', ['option1'])
->assertDispatched('filter-mounted',
field: 'item_options',
condition: 'is',
modifier: 'any',
);

}

/** @test */
public function it_does_not_accept_a_value_not_in_the_options_array()
{
Expand Down
14 changes: 0 additions & 14 deletions tests/Feature/LfDateFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,6 @@ public function it_changes_the_value_of_selected_property_when_the_user_types()
);
}

/** @test */
public function it_sends_the_filter_mounted_event()
{
Livewire::test(LfDateFilter::class, ['field' => 'item_from', 'collection' => 'pages', 'blueprint' => 'pages.pages', 'condition' => 'is_after'])
->assertSet('selected', '')
->set('selected', '2024-03-01')
->assertSet('selected', '2024-03-01')
->assertDispatched('filter-mounted',
field: 'item_from',
condition: 'is_after',
modifier: 'any',
);
}

/** @test */
public function it_loads_a_param_that_is_preset()
{
Expand Down
14 changes: 0 additions & 14 deletions tests/Feature/LfRadioFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,6 @@ public function it_changes_the_value_of_selected_property_when_an_option_is_set_
);
}

/** @test */
public function it_sends_the_filter_mounted_event()
{
Livewire::test(LfRadioFilter::class, ['field' => 'item_options', 'collection' => 'pages', 'blueprint' => 'pages.pages', 'condition' => 'is'])
->assertSet('selected', '')
->set('selected', 'option1')
->assertSet('selected', 'option1')
->assertDispatched('filter-mounted',
field: 'item_options',
condition: 'is',
modifier: 'any',
);
}

/** @test */
public function it_does_not_accept_a_value_not_in_the_options_array()
{
Expand Down
22 changes: 0 additions & 22 deletions tests/Feature/LfRangeFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,28 +123,6 @@ public function it_changes_the_value_of_selected_property_when_slider_changes()
);
}

/** @test */
public function it_sends_the_filter_mounted_event()
{
Livewire::test(LfRangeFilter::class, [
'field' => 'max_items',
'blueprint' => 'pages.pages',
'condition' => 'gte',
'min' => 1,
'max' => 4,
'default' => 2,

])
->assertSet('selected', 2)
->set('selected', 3)
->assertSet('selected', 3)
->assertDispatched('filter-mounted',
field: 'max_items',
condition: 'gte',
modifier: 'any',
);
}

/** @test */
public function it_loads_a_param_that_is_preset()
{
Expand Down
14 changes: 0 additions & 14 deletions tests/Feature/LfTextFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,6 @@ public function it_changes_the_value_of_selected_property_when_the_user_types()
);
}

/** @test */
public function it_sends_the_filter_mounted_event()
{
Livewire::test(LfTextFilter::class, ['field' => 'item_options', 'collection' => 'pages', 'blueprint' => 'pages.pages', 'condition' => 'is'])
->assertSet('selected', '')
->set('selected', 'option1')
->assertSet('selected', 'option1')
->assertDispatched('filter-mounted',
field: 'item_options',
condition: 'is',
modifier: 'any',
);
}

/** @test */
public function it_loads_a_param_that_is_preset()
{
Expand Down
Loading

0 comments on commit 9f96999

Please sign in to comment.