Skip to content

Commit

Permalink
[FIX] Query String + Filters not clearing and duplicated filter button (
Browse files Browse the repository at this point in the history
#1568)

* Fix filter number not clearing

* Fix duplicated buttons

* fix phpstan warning

---------

Co-authored-by: luanfreitasdev <[email protected]>
  • Loading branch information
dansysanalyst and luanfreitasdev authored May 25, 2024
1 parent 7b187b8 commit 40ed96a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
34 changes: 30 additions & 4 deletions src/Concerns/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ public function clearFilter(string $field = '', bool $emit = true): void
{
collect($this->filters())
->each(function ($filter) use ($field) {
/**
* Filter Number will generate FIELD_start and FIELD_end fields,
* and both fields should also be cleaned.
* Here we verify if there are filter numbers and their fields.
*/

$extraFieldsToClear = [];

if (!empty($this->filters['number'])) {
$numberField = str($field)->beforeLast('_start')->beforeLast('_end')->append('')->toString();

if (isset($this->filters['number'][$numberField])) {
$field = $numberField;
$extraFieldsToClear = [$numberField . '_start', $numberField . '_end'];
}
}

if (isset($this->filters['multi_select'][$field])) {
$this->dispatch('pg:clear_multi_select::' . $this->tableName . ':' . $field);
}
Expand Down Expand Up @@ -55,6 +72,10 @@ public function clearFilter(string $field = '', bool $emit = true): void

if ($field === data_get($filter, 'field')) {
$unset($filter, $field, null);

foreach ($extraFieldsToClear as $fieldToClear) {
$unset($filter, $fieldToClear, null);
}
};
});

Expand Down Expand Up @@ -383,25 +404,30 @@ protected function powerGridQueryString(string $prefix = ''): array
}

if ($filter->key === 'number') {
$_start = $as->append('_start')->toString();
$_end = $as->append('_end')->toString();
$_start = $as->append('_start')->toString();
$_end = $as->append('_end')->toString();
$fieldProcessed = false;

$queryString['filters.number.' . $filter->field . '.start'] = [
'as' => $_start,
'except' => '',
];

if (!is_null(request()->get($_start))) {
if ($fieldProcessed === false && !is_null(request()->get($_start))) {
$this->addEnabledFilters($filter->field . '_start', strval($columns->get($filter->field, $filter->field)));

$fieldProcessed = true;
}

$queryString['filters.number.' . $filter->field . '.end'] = [
'as' => $_end,
'except' => '',
];

if (!is_null(request()->get($_end))) {
if ($fieldProcessed === false && !is_null(request()->get($_end))) {
$this->addEnabledFilters($filter->field . '_end', strval($columns->get($filter->field, $filter->field)));

$fieldProcessed = true;
}

continue;
Expand Down
2 changes: 1 addition & 1 deletion src/Concerns/ToggleDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function toggleDetail(string $id): void
/** @var \Illuminate\Support\Enumerable<(int|string), (int|string)> $except */
$except = $id;
collect($detailStates)->except($except)
->filter(fn ($state) => $state)->keys()
->filter(fn ($state) => boolval($state))->keys()
->each(
fn ($key) => data_set($this->setUp, "detail.state.$key", false)
);
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/ExportableJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private function prepareToExport(array $properties = []): Eloquent\Collection|Co

$sortField = Str::of($processDataSource->component->sortField)->contains('.') ? $processDataSource->component->sortField : $currentTable . '.' . $processDataSource->component->sortField;

$results = $processDataSource->prepareDataSource()
$results = $processDataSource->prepareDataSource() // @phpstan-ignore-line
->where(
fn ($query) => Builder::make($query, $this->componentTable)
->filterContains()
Expand Down

0 comments on commit 40ed96a

Please sign in to comment.