From b3e0b698cb2ee4df03efb94c28778ccade66dbb7 Mon Sep 17 00:00:00 2001 From: Iosif Chatzimichail Date: Tue, 5 Nov 2024 18:31:18 +0200 Subject: [PATCH] Handle dates in DualRangeFilter --- .../livewire/filters/lf-dual-range.blade.php | 8 ++-- src/Http/Livewire/LfDualRangeFilter.php | 18 ++++++-- tests/Feature/LfDualRangeFilterTest.php | 46 +++++++++++++++++-- 3 files changed, 61 insertions(+), 11 deletions(-) diff --git a/resources/views/livewire/filters/lf-dual-range.blade.php b/resources/views/livewire/filters/lf-dual-range.blade.php index 5d45dea..b6a9c94 100644 --- a/resources/views/livewire/filters/lf-dual-range.blade.php +++ b/resources/views/livewire/filters/lf-dual-range.blade.php @@ -23,13 +23,13 @@ }, format: { to: (value) => { - return this.format === 'date' - ? new Date(value).getFullYear() + return this.format === 'float' + ? value : Math.round(value); }, from: (value) => { - return this.format === 'date' - ? new Date(value, 0).getTime() + return this.format === 'float' + ? value : parseFloat(value); } } diff --git a/src/Http/Livewire/LfDualRangeFilter.php b/src/Http/Livewire/LfDualRangeFilter.php index ba56609..cf6eff9 100644 --- a/src/Http/Livewire/LfDualRangeFilter.php +++ b/src/Http/Livewire/LfDualRangeFilter.php @@ -2,6 +2,7 @@ namespace Reach\StatamicLivewireFilters\Http\Livewire; +use Carbon\Carbon; use Livewire\Attributes\Locked; use Livewire\Attributes\On; use Livewire\Attributes\Validate; @@ -33,7 +34,7 @@ class LfDualRangeFilter extends Component public $minRange = 1; #[Locked] - public $format = 'number'; + public $format = 'integer'; public function mount() { @@ -44,12 +45,21 @@ public function mount() public function dispatchEvent() { + $min = $this->selectedMin; + $max = $this->selectedMax; + + if ($this->statamic_field['type'] === 'date') { + $min = Carbon::createFromDate($this->selectedMin)->startOfYear()->format('Y-m-d'); + $max = Carbon::createFromDate($this->selectedMax)->endOfYear()->format('Y-m-d'); + $this->modifier = 'is_after|is_before'; + } + $this->dispatch('filter-updated', field: $this->field, condition: $this->condition, payload: [ - 'min' => $this->selectedMin, - 'max' => $this->selectedMax, + 'min' => $min, + 'max' => $max, ], command: 'replace', modifier: $this->modifier, @@ -63,6 +73,7 @@ public function updatedSelectedMin($value) if ($value > $this->selectedMax - $this->minRange) { $this->selectedMin = $this->selectedMax - $this->minRange; } + $this->dispatchEvent(); } @@ -72,6 +83,7 @@ public function updatedSelectedMax($value) if ($value < $this->selectedMin + $this->minRange) { $this->selectedMax = $this->selectedMin + $this->minRange; } + $this->dispatchEvent(); } diff --git a/tests/Feature/LfDualRangeFilterTest.php b/tests/Feature/LfDualRangeFilterTest.php index 8f109d7..4217855 100644 --- a/tests/Feature/LfDualRangeFilterTest.php +++ b/tests/Feature/LfDualRangeFilterTest.php @@ -46,7 +46,7 @@ public function setUp(): void [ 'handle' => 'year', 'field' => [ - 'type' => 'text', + 'type' => 'date', 'display' => 'Year', 'listable' => 'hidden', ], @@ -57,9 +57,9 @@ public function setUp(): void ]); $this->blueprint->setHandle('yachts')->setNamespace('collections.'.$this->collection->handle())->save(); - $this->makeEntry($this->collection, 'yacht-a')->set('title', 'Luxury Yacht A')->set('cabins', 4)->set('year', 2020)->save(); - $this->makeEntry($this->collection, 'yacht-b')->set('title', 'Luxury Yacht B')->set('cabins', 6)->set('year', 2022)->save(); - $this->makeEntry($this->collection, 'yacht-c')->set('title', 'Luxury Yacht C')->set('cabins', 8)->set('year', 2024)->save(); + $this->makeEntry($this->collection, 'yacht-a')->set('title', 'Luxury Yacht A')->set('cabins', 4)->set('year', '2020-05-22')->save(); + $this->makeEntry($this->collection, 'yacht-b')->set('title', 'Luxury Yacht B')->set('cabins', 6)->set('year', '2022-02-02')->save(); + $this->makeEntry($this->collection, 'yacht-c')->set('title', 'Luxury Yacht C')->set('cabins', 8)->set('year', '2024-12-30')->save(); } #[Test] @@ -195,6 +195,44 @@ public function collection_component_handles_different_conditions_by_modifier() ]); } + #[Test] + public function it_can_handle_date_field_and_dispatch_correct_event() + { + Livewire::test(LfDualRangeFilter::class, [ + 'field' => 'year', + 'blueprint' => 'yachts.yachts', + 'condition' => 'dual_range', + 'min' => 2018, + 'max' => 2024, + 'minRange' => 1, + ]) + ->set('selectedMin', 2020) + ->assertDispatched('filter-updated', + field: 'year', + condition: 'dual_range', + payload: ['min' => '2020-01-01', 'max' => '2024-12-31'], + command: 'replace', + modifier: 'is_after|is_before', + ); + } + + #[Test] + public function collection_component_handles_date_field() + { + Livewire::test(LivewireCollection::class, ['params' => ['from' => 'yachts']]) + ->dispatch('filter-updated', + field: 'year', + condition: 'dual_range', + payload: ['min' => '2020-01-01', 'max' => '2024-12-31'], + command: 'replace', + modifier: 'is_after|is_before', + ) + ->assertSet('params', [ + 'year:is_after' => '2020-01-01', + 'year:is_before' => '2024-12-31', + ]); + } + #[Test] public function it_loads_preset_params_correctly() {