Skip to content

Commit 1f48ead

Browse files
Fix datepicker datetime filter (#1765)
* 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]>
1 parent a88af7b commit 1f48ead

File tree

3 files changed

+127
-11
lines changed

3 files changed

+127
-11
lines changed

src/Concerns/Filter.php

+16-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace PowerComponents\LivewirePowerGrid\Concerns;
44

55
use DateTimeZone;
6-
use Illuminate\Support\{Arr, Carbon, Collection};
6+
use Illuminate\Support\{Arr, Carbon, Collection, Str};
77
use Livewire\Attributes\On;
88
use PowerComponents\LivewirePowerGrid\Column;
99

@@ -117,22 +117,29 @@ public function datePickerChanged(
117117

118118
$this->resetPage();
119119

120-
$startDate = strval($selectedDates[0]);
121-
$endDate = strval($selectedDates[1]);
120+
if (Str::contains($dateStr, 'to')) {
121+
[$startDate, $endDate] = explode(' to ', $dateStr);
122+
} else {
123+
$startDate = strval($selectedDates[0]);
124+
$endDate = strval($selectedDates[1]);
125+
}
122126

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

125129
$filterTimezone = new DateTimeZone($timezone);
126130

127-
$startDate = Carbon::parse($startDate)->format('Y-m-d');
128-
$endDate = Carbon::parse($endDate)->format('Y-m-d');
131+
$startDate = Carbon::parse($startDate)->format('Y-m-d H:i:s');
132+
$endDate = Carbon::parse($endDate)->format('Y-m-d H:i:s');
129133

130-
$startDate = Carbon::createFromFormat('Y-m-d', $startDate, $filterTimezone);
131-
$endDate = Carbon::createFromFormat('Y-m-d', $endDate, $filterTimezone);
134+
$startDate = Carbon::createFromFormat('Y-m-d H:i:s', $startDate, $filterTimezone);
135+
$endDate = Carbon::createFromFormat('Y-m-d H:i:s', $endDate, $filterTimezone);
132136

133137
if ($type === 'datetime') {
134-
$startDate->startOfDay()->setTimeZone($appTimeZone);
135-
$endDate->endOfDay()->setTimeZone($appTimeZone);
138+
$endDate->setTimeZone($appTimeZone);
139+
140+
if ($endDate->isStartOfDay()) {
141+
$endDate->endOfDay()->setTimeZone($appTimeZone);
142+
}
136143
}
137144

138145
$this->addEnabledFilters($field, $label);

tests/Concerns/TestDatabase.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public static function generate(): array
156156
'chef_id' => 1,
157157
'price' => 10.00,
158158
'in_stock' => true,
159-
'produced_at' => '2021-01-01 00:00:00',
159+
'produced_at' => '2021-01-01 03:00:00',
160160
'chef_name' => null,
161161
'diet' => 2,
162162
'serving_at' => 'table',
@@ -167,7 +167,7 @@ public static function generate(): array
167167
'chef_id' => 1,
168168
'price' => 20.50,
169169
'in_stock' => true,
170-
'produced_at' => '2021-02-02 00:00:00',
170+
'produced_at' => '2021-02-02 05:00:00',
171171
'chef_name' => 'Nábia',
172172
'diet' => 1,
173173
],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
use PowerComponents\LivewirePowerGrid\Facades\Filter;
4+
use PowerComponents\LivewirePowerGrid\Tests\Concerns\Components\DishesTable;
5+
6+
use function PowerComponents\LivewirePowerGrid\Tests\Plugins\livewire;
7+
8+
use PowerComponents\LivewirePowerGrid\Themes\{Bootstrap5, Tailwind};
9+
10+
$component = new class () extends DishesTable {
11+
public string $tableName = 'filter-date-picker-test';
12+
13+
public function filters(): array
14+
{
15+
return [
16+
Filter::datepicker('produced_at'),
17+
];
18+
}
19+
};
20+
21+
it('should dispatch "pg:datePicker-{tableName}" with date format', function (string $component, object $params) {
22+
$component = livewire($component)
23+
->call('setTestThemeClass', $params->theme)
24+
->dispatch(
25+
'pg:datePicker-filter-date-picker-test',
26+
selectedDates: ['2024-11-06', '2021-02-02'],
27+
field: 'produced_at',
28+
dateStr: '2021-01-01 to 2021-02-02',
29+
label: 'Produced At',
30+
type: 'datetime',
31+
timezone: 'UTC'
32+
)
33+
->assertSee('Peixada');
34+
35+
expect($component->filters)
36+
->toBe([
37+
'datetime' => [
38+
'produced_at' => [
39+
'start' => 'Fri Jan 01 2021 00:00:00 GMT+0000',
40+
'end' => 'Tue Feb 02 2021 23:59:59 GMT+0000',
41+
'formatted' => '2021-01-01 to 2021-02-02',
42+
],
43+
],
44+
]);
45+
})->with('filterComponent');
46+
47+
it('should filter "pg:datePicker-{tableName}" with date format', function (string $component, object $params) {
48+
livewire($component)
49+
->call('setTestThemeClass', $params->theme)
50+
->assertSee('Peixada')
51+
->dispatch(
52+
'pg:datePicker-filter-date-picker-test',
53+
selectedDates: ['2021-03-03', '2021-05-05'],
54+
field: 'produced_at',
55+
dateStr: '2021-03-03 to 2021-05-05',
56+
label: 'Produced At',
57+
type: 'date',
58+
timezone: 'UTC'
59+
)
60+
->assertDontSee('Peixada');
61+
})->with('filterComponent');
62+
63+
it('should filter "pg:datePicker-{tableName}" with datetime format', function (string $component, object $params) {
64+
livewire($component)
65+
->call('setTestThemeClass', $params->theme)
66+
->assertSee('Peixada', 'Pastel de Nata')
67+
->dispatch(
68+
'pg:datePicker-filter-date-picker-test',
69+
selectedDates: ['2021-01-01', '2021-02-02'],
70+
field: 'produced_at',
71+
dateStr: '2021-01-01 00:00:00 to 2021-02-02 04:00:00',
72+
label: 'Produced At',
73+
type: 'datetime',
74+
timezone: 'UTC'
75+
)
76+
->assertSee('Pastel de Nata')
77+
->assertDontSee('Peixada');
78+
})->with('filterComponent');
79+
80+
it('should dispatch "pg:datePicker-{tableName}" with datetime format', function (string $component, object $params) {
81+
$component = livewire($component)
82+
->call('setTestThemeClass', $params->theme)
83+
->dispatch(
84+
'pg:datePicker-filter-date-picker-test',
85+
selectedDates: ['2024-11-06', '2021-02-02'],
86+
field: 'produced_at',
87+
dateStr: '2021-01-01 03:00:00 to 2021-02-02 05:00:00',
88+
label: 'Produced At',
89+
type: 'datetime',
90+
timezone: 'UTC'
91+
)
92+
->assertSee('Peixada');
93+
94+
expect($component->filters)
95+
->toBe([
96+
'datetime' => [
97+
'produced_at' => [
98+
'start' => 'Fri Jan 01 2021 03:00:00 GMT+0000',
99+
'end' => 'Tue Feb 02 2021 05:00:00 GMT+0000',
100+
'formatted' => '2021-01-01 03:00:00 to 2021-02-02 05:00:00',
101+
],
102+
],
103+
]);
104+
})->with('filterComponent');
105+
106+
dataset('filterComponent', [
107+
'tailwind -> id' => [$component::class, (object) ['theme' => Tailwind::class]],
108+
'bootstrap -> id' => [$component::class, (object) ['theme' => Bootstrap5::class]],
109+
]);

0 commit comments

Comments
 (0)