Skip to content

Commit

Permalink
Fix datepicker datetime filter (#1765)
Browse files Browse the repository at this point in the history
* Update Filter.php

time isn't ignored anymore

* Update Filter.php

* Update Filter.php

* Update Filter.php

fix namespace

* Add tests

---------

Co-authored-by: luanfreitasdev <[email protected]>
  • Loading branch information
edwinheij and luanfreitasdev authored Nov 12, 2024
1 parent a88af7b commit 1f48ead
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 11 deletions.
25 changes: 16 additions & 9 deletions src/Concerns/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace PowerComponents\LivewirePowerGrid\Concerns;

use DateTimeZone;
use Illuminate\Support\{Arr, Carbon, Collection};
use Illuminate\Support\{Arr, Carbon, Collection, Str};
use Livewire\Attributes\On;
use PowerComponents\LivewirePowerGrid\Column;

Expand Down Expand Up @@ -117,22 +117,29 @@ public function datePickerChanged(

$this->resetPage();

$startDate = strval($selectedDates[0]);
$endDate = strval($selectedDates[1]);
if (Str::contains($dateStr, 'to')) {
[$startDate, $endDate] = explode(' to ', $dateStr);
} else {
$startDate = strval($selectedDates[0]);
$endDate = strval($selectedDates[1]);
}

$appTimeZone = strval(config('app.timezone'));

$filterTimezone = new DateTimeZone($timezone);

$startDate = Carbon::parse($startDate)->format('Y-m-d');
$endDate = Carbon::parse($endDate)->format('Y-m-d');
$startDate = Carbon::parse($startDate)->format('Y-m-d H:i:s');
$endDate = Carbon::parse($endDate)->format('Y-m-d H:i:s');

$startDate = Carbon::createFromFormat('Y-m-d', $startDate, $filterTimezone);
$endDate = Carbon::createFromFormat('Y-m-d', $endDate, $filterTimezone);
$startDate = Carbon::createFromFormat('Y-m-d H:i:s', $startDate, $filterTimezone);
$endDate = Carbon::createFromFormat('Y-m-d H:i:s', $endDate, $filterTimezone);

if ($type === 'datetime') {
$startDate->startOfDay()->setTimeZone($appTimeZone);
$endDate->endOfDay()->setTimeZone($appTimeZone);
$endDate->setTimeZone($appTimeZone);

if ($endDate->isStartOfDay()) {
$endDate->endOfDay()->setTimeZone($appTimeZone);
}
}

$this->addEnabledFilters($field, $label);
Expand Down
4 changes: 2 additions & 2 deletions tests/Concerns/TestDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public static function generate(): array
'chef_id' => 1,
'price' => 10.00,
'in_stock' => true,
'produced_at' => '2021-01-01 00:00:00',
'produced_at' => '2021-01-01 03:00:00',
'chef_name' => null,
'diet' => 2,
'serving_at' => 'table',
Expand All @@ -167,7 +167,7 @@ public static function generate(): array
'chef_id' => 1,
'price' => 20.50,
'in_stock' => true,
'produced_at' => '2021-02-02 00:00:00',
'produced_at' => '2021-02-02 05:00:00',
'chef_name' => 'Nábia',
'diet' => 1,
],
Expand Down
109 changes: 109 additions & 0 deletions tests/Feature/Filters/FilterDatePickerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

use PowerComponents\LivewirePowerGrid\Facades\Filter;
use PowerComponents\LivewirePowerGrid\Tests\Concerns\Components\DishesTable;

use function PowerComponents\LivewirePowerGrid\Tests\Plugins\livewire;

use PowerComponents\LivewirePowerGrid\Themes\{Bootstrap5, Tailwind};

$component = new class () extends DishesTable {
public string $tableName = 'filter-date-picker-test';

public function filters(): array
{
return [
Filter::datepicker('produced_at'),
];
}
};

it('should dispatch "pg:datePicker-{tableName}" with date format', function (string $component, object $params) {
$component = livewire($component)
->call('setTestThemeClass', $params->theme)
->dispatch(
'pg:datePicker-filter-date-picker-test',
selectedDates: ['2024-11-06', '2021-02-02'],
field: 'produced_at',
dateStr: '2021-01-01 to 2021-02-02',
label: 'Produced At',
type: 'datetime',
timezone: 'UTC'
)
->assertSee('Peixada');

expect($component->filters)
->toBe([
'datetime' => [
'produced_at' => [
'start' => 'Fri Jan 01 2021 00:00:00 GMT+0000',
'end' => 'Tue Feb 02 2021 23:59:59 GMT+0000',
'formatted' => '2021-01-01 to 2021-02-02',
],
],
]);
})->with('filterComponent');

it('should filter "pg:datePicker-{tableName}" with date format', function (string $component, object $params) {
livewire($component)
->call('setTestThemeClass', $params->theme)
->assertSee('Peixada')
->dispatch(
'pg:datePicker-filter-date-picker-test',
selectedDates: ['2021-03-03', '2021-05-05'],
field: 'produced_at',
dateStr: '2021-03-03 to 2021-05-05',
label: 'Produced At',
type: 'date',
timezone: 'UTC'
)
->assertDontSee('Peixada');
})->with('filterComponent');

it('should filter "pg:datePicker-{tableName}" with datetime format', function (string $component, object $params) {
livewire($component)
->call('setTestThemeClass', $params->theme)
->assertSee('Peixada', 'Pastel de Nata')
->dispatch(
'pg:datePicker-filter-date-picker-test',
selectedDates: ['2021-01-01', '2021-02-02'],
field: 'produced_at',
dateStr: '2021-01-01 00:00:00 to 2021-02-02 04:00:00',
label: 'Produced At',
type: 'datetime',
timezone: 'UTC'
)
->assertSee('Pastel de Nata')
->assertDontSee('Peixada');
})->with('filterComponent');

it('should dispatch "pg:datePicker-{tableName}" with datetime format', function (string $component, object $params) {
$component = livewire($component)
->call('setTestThemeClass', $params->theme)
->dispatch(
'pg:datePicker-filter-date-picker-test',
selectedDates: ['2024-11-06', '2021-02-02'],
field: 'produced_at',
dateStr: '2021-01-01 03:00:00 to 2021-02-02 05:00:00',
label: 'Produced At',
type: 'datetime',
timezone: 'UTC'
)
->assertSee('Peixada');

expect($component->filters)
->toBe([
'datetime' => [
'produced_at' => [
'start' => 'Fri Jan 01 2021 03:00:00 GMT+0000',
'end' => 'Tue Feb 02 2021 05:00:00 GMT+0000',
'formatted' => '2021-01-01 03:00:00 to 2021-02-02 05:00:00',
],
],
]);
})->with('filterComponent');

dataset('filterComponent', [
'tailwind -> id' => [$component::class, (object) ['theme' => Tailwind::class]],
'bootstrap -> id' => [$component::class, (object) ['theme' => Bootstrap5::class]],
]);

0 comments on commit 1f48ead

Please sign in to comment.