Skip to content

Commit

Permalink
Merge pull request #34 from reachweb/remove-commands
Browse files Browse the repository at this point in the history
Remove command
  • Loading branch information
afonic authored Nov 7, 2024
2 parents 3c4bdce + daf8073 commit 863572e
Show file tree
Hide file tree
Showing 17 changed files with 89 additions and 219 deletions.
33 changes: 7 additions & 26 deletions src/Http/Livewire/LfCheckboxFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,13 @@ public function updatedSelected()
$this->validate();
}

$optionsToAdd = array_diff($this->selected, $this->previousSelected);
$optionsToRemove = array_diff($this->previousSelected, $this->selected);

foreach ($optionsToAdd as $option) {
$this->dispatch('filter-updated',
field: $this->field,
condition: $this->condition,
payload: $option,
command: 'add',
modifier: $this->modifier,
)
->to(LivewireCollection::class);
}

foreach ($optionsToRemove as $option) {
$this->dispatch('filter-updated',
field: $this->field,
condition: $this->condition,
payload: $option,
command: 'remove',
modifier: $this->modifier,
)
->to(LivewireCollection::class);
}

$this->previousSelected = $this->selected;
$this->dispatch('filter-updated',
field: $this->field,
condition: $this->condition,
payload: $this->selected,
modifier: $this->modifier,
)
->to(LivewireCollection::class);
}

public function clear()
Expand Down
1 change: 0 additions & 1 deletion src/Http/Livewire/LfDateFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public function updatedSelected()
field: $this->field,
condition: $this->condition,
payload: $this->selected,
command: 'replace',
modifier: $this->modifier,
)
->to(LivewireCollection::class);
Expand Down
1 change: 0 additions & 1 deletion src/Http/Livewire/LfDualRangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public function dispatchEvent()
'min' => $min,
'max' => $max,
],
command: 'replace',
modifier: $this->modifier,
)
->to(LivewireCollection::class);
Expand Down
1 change: 0 additions & 1 deletion src/Http/Livewire/LfRadioFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public function updatedSelected()
field: $this->field,
condition: $this->condition,
payload: $this->selected,
command: 'replace',
modifier: $this->modifier,
)
->to(LivewireCollection::class);
Expand Down
1 change: 0 additions & 1 deletion src/Http/Livewire/LfRangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public function dispatchEvent()
field: $this->field,
condition: $this->condition,
payload: $this->selected,
command: 'replace',
modifier: null,
)
->to(LivewireCollection::class);
Expand Down
1 change: 0 additions & 1 deletion src/Http/Livewire/LfTextFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public function updatedSelected()
field: $this->field,
condition: $this->condition,
payload: $this->selected,
command: 'replace',
modifier: $this->modifier,
)
->to(LivewireCollection::class);
Expand Down
16 changes: 11 additions & 5 deletions src/Http/Livewire/LivewireCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,31 @@ public function mount($params)
}

#[On('filter-updated')]
public function filterUpdated($field, $condition, $payload, $command, $modifier)
public function filterUpdated($field, $condition, $payload, $modifier)
{
$this->resetPagination();
if ($payload === '' || $payload === null || $payload === []) {
$this->clearFilter($field, $condition, $modifier);
$this->dispatchParamsUpdated();

return;
}
if ($condition === 'query_scope') {
$this->handleQueryScopeCondition($field, $payload, $command, $modifier);
$this->handleQueryScopeCondition($field, $payload, $modifier);

return;
}
if ($condition === 'taxonomy') {
$this->handleTaxonomyCondition($field, $payload, $command, $modifier);
$this->handleTaxonomyCondition($field, $payload, $modifier);

return;
}
if ($condition === 'dual_range') {
$this->handleDualRangeCondition($field, $payload, $command, $modifier);
$this->handleDualRangeCondition($field, $payload, $modifier);

return;
}
$this->handleCondition($field, $condition, $payload, $command);
$this->handleCondition($field, $condition, $payload);
}

#[On('sort-updated')]
Expand Down
165 changes: 50 additions & 115 deletions src/Http/Livewire/Traits/HandleParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Reach\StatamicLivewireFilters\Http\Livewire\Traits;

use Livewire\Attributes\On;
use Reach\StatamicLivewireFilters\Exceptions\CommandNotFoundException;
use Reach\StatamicLivewireFilters\Http\Livewire\LfTags;

trait HandleParams
Expand Down Expand Up @@ -69,153 +68,78 @@ protected function extractAllowedFilters($paramsCollection)
}
}

protected function handleCondition($field, $condition, $payload, $command)
protected function handleCondition($field, $condition, $payload)
{
$paramKey = $field.':'.$condition;
$this->runCommand($command, $paramKey, $payload);
$this->params[$paramKey] = $this->toPipeSeparatedString($payload);

$this->dispatchParamsUpdated();
}

protected function handleTaxonomyCondition($field, $payload, $command, $modifier)
protected function handleTaxonomyCondition($field, $payload, $modifier)
{
$paramKey = 'taxonomy:'.$field.':'.$modifier;
$this->runCommand($command, $paramKey, $payload);
$this->params[$paramKey] = $this->toPipeSeparatedString($payload);

$this->dispatchParamsUpdated();
}

protected function handleQueryScopeCondition($field, $payload, $command, $modifier)
protected function handleQueryScopeCondition($field, $payload, $modifier)
{
$queryScopeKey = 'query_scope';
$modifierKey = $modifier.':'.$field;

switch ($command) {
case 'add':
$this->params[$queryScopeKey] = $modifier;
if (! isset($this->params[$modifierKey])) {
$this->params[$modifierKey] = $payload;
} else {
$payloads = collect(explode('|', $this->params[$modifierKey]));
if (! $payloads->contains($payload)) {
$payloads->push($payload);
$this->params[$modifierKey] = $payloads->implode('|');
}
}
break;

case 'remove':
if (isset($this->params[$modifierKey])) {
$payloads = collect(explode('|', $this->params[$modifierKey]));
$payloads = $payloads->reject(fn ($item) => $item === $payload);
if ($payloads->isNotEmpty()) {
$this->params[$modifierKey] = $payloads->implode('|');
} else {
unset($this->params[$modifierKey], $this->params[$queryScopeKey]);
}
}
break;

case 'replace':
$this->params[$queryScopeKey] = $modifier;
$this->params[$modifierKey] = $payload;
break;

case 'clear':
unset($this->params[$queryScopeKey], $this->params[$modifierKey]);
break;

default:
throw new CommandNotFoundException($command);
}
$this->params[$queryScopeKey] = $modifier;
$this->params[$modifierKey] = $this->toPipeSeparatedString($payload);

$this->dispatchParamsUpdated();
}

protected function handleDualRangeCondition($field, $payload, $command, $modifier)
protected function handleDualRangeCondition($field, $payload, $modifier)
{
$minModifier = 'gte';
$maxModifier = 'lte';

// If the modifier is set, we need to extract the min and max modifiers
if ($modifier !== 'any') {
[$minModifier, $maxModifier] = explode('|', $modifier);
}
[$minModifier, $maxModifier] = $this->getDualRangeConditions($modifier);

$minParamKey = $field.':'.$minModifier;
$maxParamKey = $field.':'.$maxModifier;

switch ($command) {
case 'replace':
$this->params[$minParamKey] = $payload['min'];
$this->params[$maxParamKey] = $payload['max'];
break;

case 'clear':
unset($this->params[$minParamKey]);
unset($this->params[$maxParamKey]);
break;
$this->params[$minParamKey] = $payload['min'];
$this->params[$maxParamKey] = $payload['max'];

default:
throw new CommandNotFoundException($command);
}
$this->dispatchParamsUpdated();
}

protected function runCommand($command, $paramKey, $value)
#[On('clear-filter')]
public function clearFilter($field, $condition, $modifier): void
{
switch ($command) {
case 'add':
$this->addValueToParam($paramKey, $value);
break;
case 'replace':
$this->replaceValueOfParam($paramKey, $value);
break;
case 'remove':
$this->removeValueFromParam($paramKey, $value);
break;
case 'clear':
$this->clearParam($paramKey);
break;
default:
throw new CommandNotFoundException($command);
break;
}
if ($condition === 'query_scope') {
$queryScopeKey = 'query_scope';
$modifierKey = $modifier.':'.$field;
unset($this->params[$queryScopeKey], $this->params[$modifierKey]);

$this->dispatchParamsUpdated();
}
return;
}
if ($condition === 'taxonomy') {
$paramKey = 'taxonomy:'.$field.':'.$modifier;
unset($this->params[$paramKey]);

protected function addValueToParam($paramKey, $value)
{
if (! isset($this->params[$paramKey])) {
$this->params[$paramKey] = $value;
} else {
$values = collect(explode('|', $this->params[$paramKey]));
if (! $values->contains($value)) {
$values->push($value);
$this->params[$paramKey] = $values->implode('|');
}
return;
}
}
if ($condition === 'dual_range') {
[$minModifier, $maxModifier] = $this->getDualRangeConditions($modifier);

protected function replaceValueOfParam($paramKey, $value)
{
$this->params[$paramKey] = $value;
}
$minParamKey = $field.':'.$minModifier;
$maxParamKey = $field.':'.$maxModifier;

protected function clearParam($paramKey)
{
unset($this->params[$paramKey]);
unset($this->params[$minParamKey], $this->params[$maxParamKey]);

return;
}
unset($this->params[$field.':'.$condition]);
}

protected function removeValueFromParam($paramKey, $value)
protected function toPipeSeparatedString($payload): string
{
if (isset($this->params[$paramKey])) {
$values = collect(explode('|', $this->params[$paramKey]));

$values = $values->reject(fn ($item) => $item === $value);

if ($values->isNotEmpty()) {
$this->params[$paramKey] = $values->implode('|');
} else {
unset($this->params[$paramKey]);
}
}
return is_array($payload) ? implode('|', $payload) : $payload;
}

protected function handlePresetParams()
Expand Down Expand Up @@ -281,6 +205,17 @@ protected function getConfigAliases(): array
->all();
}

protected function getDualRangeConditions($modifer): array
{
$modifiers = ['gte', 'lte'];

if ($modifer === 'any') {
return $modifiers;
}

return explode('|', $modifer);
}

protected function dispatchParamsUpdated(): void
{
if (config('statamic-livewire-filters.enable_filter_values_count')) {
Expand Down
4 changes: 1 addition & 3 deletions src/Http/Livewire/Traits/IsLivewireFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,9 @@ public function initiateField()

public function clearFilters()
{
$this->dispatch('filter-updated',
$this->dispatch('clear-filter',
field: $this->field,
condition: $this->condition,
payload: false,
command: 'clear',
modifier: $this->modifier,
)
->to(LivewireCollection::class);
Expand Down
15 changes: 5 additions & 10 deletions tests/Feature/LfCheckboxFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,14 @@ public function it_changes_the_value_of_selected_property_when_an_option_is_set_
->assertDispatched('filter-updated',
field: 'item_options',
condition: 'is',
payload: 'option1',
command: 'add',
payload: ['option1'],
)
->set('selected', ['option1', 'option2'])
->assertSet('selected', ['option1', 'option2'])
->assertDispatched('filter-updated',
field: 'item_options',
condition: 'is',
payload: 'option2',
command: 'add',
payload: ['option1', 'option2'],
);
}

Expand All @@ -172,8 +170,7 @@ public function it_can_turn_off_validation_of_values_in_the_config()
->assertDispatched('filter-updated',
field: 'item_options',
condition: 'is',
payload: 'not-an-option',
command: 'add',
payload: ['not-an-option'],
);
}

Expand All @@ -189,16 +186,14 @@ public function it_shows_taxonomy_terms_and_submits_the_right_events()
->assertDispatched('filter-updated',
field: 'colors',
condition: 'taxonomy',
payload: 'red',
command: 'add',
payload: ['red'],
)
->set('selected', ['yellow'])
->assertSet('selected', ['yellow'])
->assertDispatched('filter-updated',
field: 'colors',
condition: 'taxonomy',
payload: 'red',
command: 'remove',
payload: ['yellow'],
);
}

Expand Down
Loading

0 comments on commit 863572e

Please sign in to comment.