Skip to content

Commit

Permalink
improves
Browse files Browse the repository at this point in the history
  • Loading branch information
luanfreitasdev committed Dec 11, 2023
1 parent ccd87f4 commit 635e47c
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 72 deletions.
6 changes: 4 additions & 2 deletions src/Components/Filters/Builders/BuilderBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

namespace PowerComponents\LivewirePowerGrid\Components\Filters\Builders;

use Livewire\Component;
use PowerComponents\LivewirePowerGrid\Components\Filters\FilterBase;

class BuilderBase
{
public static function make(FilterBase $filterBase): self
public static function make(Component $component, FilterBase $filterBase): self
{
return new self($filterBase);
return new self($component, $filterBase);
}

public function __construct(
protected Component $component,
protected null|array|FilterBase $filterBase = null
) {
}
Expand Down
64 changes: 36 additions & 28 deletions src/Components/Filters/Builders/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Support\Collection;

use function Livewire\store;

class Number extends BuilderBase
{
public function builder(Builder|QueryBuilder $builder, string $field, int|array|string|null $values): void
{
$thousands = store($this->component)->get('filters.number.' . $field . '.thousands');
$decimal = store($this->component)->get('filters.number.' . $field . '.decimal');

if (data_get($this->filterBase, 'builder')) {
/** @var \Closure $closure */
$closure = data_get($this->filterBase, 'builder');
Expand All @@ -23,12 +28,12 @@ public function builder(Builder|QueryBuilder $builder, string $field, int|array|
if (isset($values['start']) && !isset($values['end'])) {
$start = $values['start'];

if (isset($values['thousands'])) {
$start = str_replace($values['thousands'], '', $start);
if (isset($thousands)) {
$start = str_replace($thousands, '', $start);
}

if (isset($values['decimal'])) {
$start = str_replace($values['decimal'], '.', $start);
if (isset($decimal)) {
$start = str_replace($decimal, '.', $start);
}

$builder->where($field, '>=', $start);
Expand All @@ -37,12 +42,12 @@ public function builder(Builder|QueryBuilder $builder, string $field, int|array|
if (!isset($values['start']) && isset($values['end'])) {
$end = $values['end'];

if (isset($values['decimal'])) {
$end = str_replace($values['thousands'], '', $values['end']);
if (isset($decimal)) {
$end = str_replace($thousands, '', $values['end']);
}

if (isset($values['decimal'])) {
$end = (float) str_replace($values['decimal'], '.', $end);
if (isset($decimal)) {
$end = (float) str_replace($decimal, '.', $end);
}

$builder->where($field, '<=', $end);
Expand All @@ -52,14 +57,14 @@ public function builder(Builder|QueryBuilder $builder, string $field, int|array|
$start = $values['start'];
$end = $values['end'];

if (isset($values['thousands'])) {
$start = str_replace($values['thousands'], '', $values['start']);
$end = str_replace($values['thousands'], '', $values['end']);
if (isset($thousands)) {
$start = str_replace($thousands, '', $values['start']);
$end = str_replace($thousands, '', $values['end']);
}

if (isset($values['decimal'])) {
$start = str_replace($values['decimal'], '.', $start);
$end = str_replace($values['decimal'], '.', $end);
if (isset($decimal)) {
$start = str_replace($decimal, '.', $start);
$end = str_replace($decimal, '.', $end);
}

$builder->whereBetween($field, [$start, $end]);
Expand All @@ -68,6 +73,9 @@ public function builder(Builder|QueryBuilder $builder, string $field, int|array|

public function collection(Collection $collection, string $field, int|array|string|null $values): Collection
{
$thousands = store($this->component)->get('filters.number.' . $field . '.thousands');
$decimal = store($this->component)->get('filters.number.' . $field . '.decimal');

if (data_get($this->filterBase, 'collection')) {
/** @var \Closure $closure */
$closure = data_get($this->filterBase, 'collection');
Expand All @@ -79,12 +87,12 @@ public function collection(Collection $collection, string $field, int|array|stri
if (isset($values['start']) && !isset($values['end'])) {
$start = $values['start'];

if (isset($values['thousands'])) {
$start = str_replace($values['thousands'], '', $values['start']);
if (isset($thousands)) {
$start = str_replace($thousands, '', $values['start']);
}

if (isset($values['decimal'])) {
$start = (float) str_replace($values['decimal'], '.', $start);
if (isset($decimal)) {
$start = (float) str_replace($decimal, '.', $start);
}

return $collection->where($field, '>=', $start);
Expand All @@ -93,12 +101,12 @@ public function collection(Collection $collection, string $field, int|array|stri
if (!isset($values['start']) && isset($values['end'])) {
$end = $values['end'];

if (isset($values['thousands'])) {
$end = str_replace($values['thousands'], '', $values['end']);
if (isset($thousands)) {
$end = str_replace($thousands, '', $values['end']);
}

if (isset($values['decimal'])) {
$end = (float) str_replace($values['decimal'], '.', $end);
if (isset($decimal)) {
$end = (float) str_replace($decimal, '.', $end);
}

return $collection->where($field, '<=', $end);
Expand All @@ -108,14 +116,14 @@ public function collection(Collection $collection, string $field, int|array|stri
$start = $values['start'];
$end = $values['end'];

if (isset($values['thousands'])) {
$start = str_replace($values['thousands'], '', $values['start']);
$end = str_replace($values['thousands'], '', $values['end']);
if (isset($thousands)) {
$start = str_replace($thousands, '', $values['start']);
$end = str_replace($thousands, '', $values['end']);
}

if (isset($values['decimal'])) {
$start = str_replace($values['decimal'], '.', $start);
$end = str_replace($values['decimal'], '.', $end);
if (isset($decimal)) {
$start = str_replace($decimal, '.', $start);
$end = str_replace($decimal, '.', $end);
}

return $collection->whereBetween($field, [$start, $end]);
Expand Down
4 changes: 2 additions & 2 deletions src/Components/Filters/FilterNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ public static function getWireAttributes(string $field, array $filter): array
{
return collect()
->put('inputStartAttributes', new ComponentAttributeBag([
'wire:model.live.debounce.800ms' => 'filters.number.' . $field . '.start',
// 'wire:model.live.debounce.800ms' => 'filters.number.' . $field . '.start',
'wire:input.live.debounce.800ms' => 'filterNumberStart(\'' . $field . '\', ' . Js::from($filter) . ', $event.target.value)',
]))
->put('inputEndAttributes', new ComponentAttributeBag([
'wire:model.live.debounce.800ms' => 'filters.number.' . $field . '.end',
// 'wire:model.live.debounce.800ms' => 'filters.number.' . $field . '.end',
'wire:input.live.debounce.800ms' => 'filterNumberEnd(\'' . $field . '\', ' . Js::from($filter) . ', $event.target.value)',
]))
->toArray();
Expand Down
16 changes: 8 additions & 8 deletions src/DataSource/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function filter(): EloquentBuilder|QueryBuilder
}

foreach ($this->powerGridComponent->filters as $filterType => $filter) {
if (empty($filter)) {
if (blank($filter)) {
continue;
}

Expand All @@ -59,13 +59,13 @@ public function filter(): EloquentBuilder|QueryBuilder
->first();

match ($filterType) {
'datetime' => (new DateTimePicker($filter))->builder($query, $field, $value),
'date' => (new DatePicker($filter))->builder($query, $field, $value),
'multi_select' => (new MultiSelect($filter))->builder($query, $field, $value),
'select' => (new Select($filter))->builder($query, $field, $value),
'boolean' => (new Boolean($filter))->builder($query, $field, $value),
'number' => (new Number($filter))->builder($query, $field, $value),
'input_text' => (new InputText($filter))->builder($query, $field, [
'datetime' => (new DateTimePicker($this->powerGridComponent, $filter))->builder($query, $field, $value),
'date' => (new DatePicker($this->powerGridComponent, $filter))->builder($query, $field, $value),
'multi_select' => (new MultiSelect($this->powerGridComponent, $filter))->builder($query, $field, $value),
'select' => (new Select($this->powerGridComponent, $filter))->builder($query, $field, $value),
'boolean' => (new Boolean($this->powerGridComponent, $filter))->builder($query, $field, $value),
'number' => (new Number($this->powerGridComponent, $filter))->builder($query, $field, $value),
'input_text' => (new InputText($this->powerGridComponent, $filter))->builder($query, $field, [
'selected' => $this->validateInputTextOptions($this->powerGridComponent->filters, $field),
'value' => $value,
'searchMorphs' => $this->powerGridComponent->searchMorphs,
Expand Down
14 changes: 7 additions & 7 deletions src/DataSource/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ public function filter(): BaseCollection
->first();

$this->collection = match ($filterType) {
'datetime' => (new DateTimePicker($filter))->collection($this->collection, $field, $value),
'date' => (new DatePicker($filter))->collection($this->collection, $field, $value),
'multi_select' => (new MultiSelect($filter))->collection($this->collection, $field, $value),
'select' => (new Select($filter))->collection($this->collection, $field, $value),
'boolean' => (new Boolean($filter))->collection($this->collection, $field, $value),
'number' => (new Number($filter))->collection($this->collection, $field, $value),
'input_text' => (new InputText($filter))->collection($this->collection, $field, [
'datetime' => (new DateTimePicker($this->powerGridComponent, $filter))->collection($this->collection, $field, $value),
'date' => (new DatePicker($this->powerGridComponent, $filter))->collection($this->collection, $field, $value),
'multi_select' => (new MultiSelect($this->powerGridComponent, $filter))->collection($this->collection, $field, $value),
'select' => (new Select($this->powerGridComponent, $filter))->collection($this->collection, $field, $value),
'boolean' => (new Boolean($this->powerGridComponent, $filter))->collection($this->collection, $field, $value),
'number' => (new Number($this->powerGridComponent, $filter))->collection($this->collection, $field, $value),
'input_text' => (new InputText($this->powerGridComponent, $filter))->collection($this->collection, $field, [
'selected' => $this->validateInputTextOptions($this->powerGridComponent->filters, $field),
'value' => $value,
]),
Expand Down
15 changes: 9 additions & 6 deletions src/Traits/HasFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use DateTimeZone;
use Illuminate\Support\{Carbon};
use Livewire\Attributes\On;

use function Livewire\store;

use PowerComponents\LivewirePowerGrid\Column;

trait HasFilter
Expand Down Expand Up @@ -280,10 +283,10 @@ public function filterNumberStart(string $field, array $params, string $value):

$this->resetPage();

$value = filter_var($value, FILTER_SANITIZE_NUMBER_INT);
$this->filters['number'][$field]['start'] = $value;

$this->filters['number'][$field]['thousands'] = $thousands;
$this->filters['number'][$field]['decimal'] = $decimal;
store($this)->set('filters.number.' . $field . '.thousands', $thousands);
store($this)->set('filters.number.' . $field . '.decimal', $decimal);

$this->enabledFilters[$field]['id'] = $field;
$this->enabledFilters[$field]['label'] = $title;
Expand All @@ -303,10 +306,10 @@ public function filterNumberEnd(string $field, array $params, string $value): vo

$this->resetPage();

$value = filter_var($value, FILTER_SANITIZE_NUMBER_INT);
store($this)->set('filters.number.' . $field . '.thousands', $thousands);
store($this)->set('filters.number.' . $field . '.decimal', $decimal);

$this->filters['number'][$field]['thousands'] = $thousands;
$this->filters['number'][$field]['decimal'] = $decimal;
$this->filters['number'][$field]['end'] = $value;

$this->enabledFilters[$field]['id'] = $field;
$this->enabledFilters[$field]['label'] = $title;
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/Filters/FilterMultipleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function filters(): array

$component->assertSee('Barco-Sushi da Sueli');

$filters = array_merge($component->filters, filterNumber('price', '80,00', '100', '.', ','));
$filters = array_merge($component->filters, filterNumber('price', '80.00', '100'));

$component->set('filters', $filters)
->assertDontSee('Barco-Sushi da Sueli')
Expand All @@ -118,7 +118,7 @@ public function filters(): array
'input_text' => [],
'input_text_options' => [],
],
filterNumber('price', '80,00', '100', '.', ','),
filterNumber('price', '80.00', '100'),
filterBoolean('in_stock', 'true')
);

Expand Down
19 changes: 7 additions & 12 deletions tests/Feature/Filters/FilterNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
])
->call($params->theme)
->set('filters', filterNumber($params->field, '2', null))
->assertSeeHtmlInOrder([
'wire:model.live.debounce.800ms="filters.number.' . $params->field . '.start',
'wire:model.live.debounce.800ms="filters.number.' . $params->field . '.end',
])
->assertSee('Peixada da chef Nábia')
->assertSee('Francesinha')
->assertSee('борщ')
Expand All @@ -42,10 +38,8 @@ public function filters(): array
Filter::number('id')
->builder(function ($builder, $values) {
expect($values)->toBe([
'start' => '2',
'end' => null,
'thousands' => '',
'decimal' => '',
'start' => '2',
'end' => null,
])
->and($builder)->toBeInstanceOf(\Illuminate\Database\Eloquent\Builder::class);

Expand Down Expand Up @@ -220,14 +214,15 @@ public function filters(): array
})->group('filters', 'filterNumber')->with('filter_number_themes_collection', 'filter_number_themes_array');

it('properly filters by "min & max" formatted', function (string $component, object $params) {
livewire($component)
$instance = livewire($component)
->call($params->theme)
->set('testFilters', [
Filter::number('price', 'price')
Filter::number('price')
->thousands('.')
->decimal('.'),
])
->set('filters', filterNumber('price', '1,50', '20,51', '.', ','))
]);

$instance->set('filters', filterNumber('price', '1.50', '20.51'))
->assertSee('Pastel de Nata')
->assertSee('Peixada da chef Nábia')
->assertDontSee('Carne Louca');
Expand Down
9 changes: 4 additions & 5 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,14 @@ function expectInputText(object $params, mixed $component, string $value, string
}
}

function filterNumber(string $field, ?string $min, ?string $max, ?string $thousands = '', ?string $decimal = ''): array
function filterNumber(string $field, ?string $min, ?string $max): array
{
return [
'number' => [
$field => [
'start' => $min,
'end' => $max,
'thousands' => $thousands,
'decimal' => $decimal,
'start' => $min,
'end' => $max,

],
],
];
Expand Down

0 comments on commit 635e47c

Please sign in to comment.