Skip to content

Commit

Permalink
Fix filters
Browse files Browse the repository at this point in the history
  • Loading branch information
luanfreitasdev committed Dec 19, 2023
1 parent 15db9a7 commit b4a6fec
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/Components/Filters/Builders/DatePicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public function builder(EloquentBuilder|QueryBuilder $builder, string $field, in

/** @var array $values */
[$startDate, $endDate] = [
0 => Carbon::parse($values[0])->format('Y-m-d'),
1 => Carbon::parse($values[1])->format('Y-m-d'),
0 => Carbon::parse($values['start'])->format('Y-m-d'),
1 => Carbon::parse($values['end'])->format('Y-m-d'),
];

if (data_get($this->filterBase, 'builder')) {
Expand Down
8 changes: 4 additions & 4 deletions src/Components/Filters/Builders/DateTimePicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public function builder(EloquentBuilder|QueryBuilder $builder, string $field, in

/** @var array $values */
[$startDate, $endDate] = [
0 => Carbon::parse($values[0]),
1 => Carbon::parse($values[1]),
0 => Carbon::parse($values['start']),
1 => Carbon::parse($values['end']),
];

if (data_get($this->filterBase, 'builder')) {
Expand All @@ -36,8 +36,8 @@ public function collection(Collection $collection, string $field, int|array|stri
{
/** @var array $values */
[$startDate, $endDate] = [
0 => Carbon::parse($values[0]),
1 => Carbon::parse($values[1]),
0 => Carbon::parse($values['start']),
1 => Carbon::parse($values['end']),
];

if (data_get($this->filterBase, 'collection')) {
Expand Down
56 changes: 42 additions & 14 deletions src/DataSource/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,54 @@ public function filter(): EloquentBuilder|QueryBuilder
foreach ($this->powerGridComponent->filters as $filterType => $columns) {
$columns = Arr::dot($columns);

foreach ($columns as $field => $value) {
if ($filterType == 'number') {
if (str($field)->contains('.start')) {
$value = [
'start' => $value,
];
// convert array:2 [
// "dishes.produced_at.start" => "2021-03-03",
// "dishes.produced_at.end" => "2021-03-01"
//] to
// array:2 [
// "dishes.produced_at" => ["start" => "2021-03-03"],
// "dishes.produced_at.end" => ["start" => "2021-03-01"]
//] and
// convert array:2 [
// "dishes.produced_at.0" => "2021-03-03",
// "dishes.produced_at.1" => "2021-03-01"
//] to
// array:2 [
// "dishes.produced_at" => [0 => "2021-03-03"],
// "dishes.produced_at" => [1 => "2021-03-01"]
//]

$newColumns = [];

foreach ($columns as $key => $value) {
$parts = explode('.', $key);
$lastPart = end($parts);

if (is_numeric($lastPart) && intval($lastPart) == $lastPart) {
array_pop($parts);
$prefix = implode('.', $parts);

if (!isset($newColumns[$prefix])) {
$newColumns[$prefix] = [];
}

if (str($field)->contains('.end')) {
$value = [
'end' => $value,
];
$index = intval($lastPart);

$newColumns[$prefix][$index] = $value;
} elseif ($lastPart === 'start' || $lastPart === 'end') {
$prefix = implode('.', array_slice($parts, 0, -1));

if (!isset($newColumns[$prefix])) {
$newColumns[$prefix] = [];
}

$field = str($field)->replace('.start', '')
->replace('.end', '')
->trim()
->toString();
$newColumns[$prefix][$lastPart] = $value;
} else {
$newColumns[$key] = $value;
}
}

foreach ($newColumns as $field => $value) {
$this->query->where(function ($query) use ($filterType, $field, $value, $filters) {
$filter = function ($query, $filters, $filterType, $field, $value) {
$filter = $filters->filter(function ($filter) use ($field) {
Expand Down
5 changes: 2 additions & 3 deletions src/Traits/HasFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,11 @@ public function datePickerChanged(
$endDate->endOfDay()->setTimeZone($appTimeZone);
}

$selectedDatesFormatted = [$startDate, $endDate];

$this->enabledFilters[$field]['data-field'] = $field;
$this->enabledFilters[$field]['label'] = $label;

$this->filters[$type][$field] = $selectedDatesFormatted;
$this->filters[$type][$field]['start'] = $startDate;
$this->filters[$type][$field]['end'] = $endDate;

$this->filters[$type][$field]['formatted'] = $dateStr;

Expand Down
12 changes: 9 additions & 3 deletions tests/Feature/Filters/FilterDateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
->set('testFilters', [
Filter::datepicker('produced_at_formatted', 'produced_at'),
])
->set('filters', filterDate('produced_at', ['2021-03-03', '2021-03-01']))
->set('filters', filterDate('dishes.produced_at', ['2021-03-03', '2021-03-01']))
->assertSee('No records found');
})->group('filters', 'filterDatePicker')
->with('filter_datetime_themes_with_join', 'filter_datetime_query_builder');
Expand Down Expand Up @@ -179,7 +179,10 @@ function filterDate(string $dataField, array $value): array
{
return [
'date' => [
$dataField => $value,
$dataField => [
'start' => $value[0],
'end' => $value[1],
],
],
];
}
Expand All @@ -188,7 +191,10 @@ function filterDateTime(string $dataField, array $value): array
{
return [
'datetime' => [
$dataField => $value,
$dataField => [
'start' => $value[0],
'end' => $value[1],
],
],
];
}

0 comments on commit b4a6fec

Please sign in to comment.