From 829fdff04e5ca096c384dafa90bc24092b0847e6 Mon Sep 17 00:00:00 2001 From: Iosif Chatzimichail Date: Sat, 2 Nov 2024 13:38:54 +0200 Subject: [PATCH] Handle URLs correctly --- src/Http/Livewire/LivewireCollection.php | 4 ++++ src/Http/Livewire/Traits/HandleParams.php | 6 +++++- tests/Feature/CustomQueryStringTest.php | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Http/Livewire/LivewireCollection.php b/src/Http/Livewire/LivewireCollection.php index db080e5..ae8b174 100644 --- a/src/Http/Livewire/LivewireCollection.php +++ b/src/Http/Livewire/LivewireCollection.php @@ -20,12 +20,16 @@ class LivewireCollection extends Component #[Locked] public $allowedFilters; + #[Locked] + public $currentPath; + public $paginate; public $view = 'livewire-collection'; public function mount($params) { + $this->currentPath = request()->path(); $this->allowedFilters = false; if (is_null($this->params)) { $this->setParameters($params); diff --git a/src/Http/Livewire/Traits/HandleParams.php b/src/Http/Livewire/Traits/HandleParams.php index 54cb37a..7d9e592 100644 --- a/src/Http/Livewire/Traits/HandleParams.php +++ b/src/Http/Livewire/Traits/HandleParams.php @@ -232,7 +232,11 @@ protected function updateCustomQueryStringUrl(): void ? '' : $prefix.'/'.$segments->implode('/'); - $this->dispatch('update-url', newUrl: url($path)); + $fullPath = $path + ? trim($this->currentPath, '/').'/'.trim($path, '/') + : $this->currentPath; + + $this->dispatch('update-url', newUrl: url($fullPath)); } protected function dispatchParamsUpdated(): void diff --git a/tests/Feature/CustomQueryStringTest.php b/tests/Feature/CustomQueryStringTest.php index cf879a2..c45b544 100644 --- a/tests/Feature/CustomQueryStringTest.php +++ b/tests/Feature/CustomQueryStringTest.php @@ -130,6 +130,6 @@ public function it_dispatched_the_update_url_event_with_the_correct_url() modifier: 'any', ) ->assertDispatched('update-url') - ->assertDispatched('update-url', fn ($name, $payload) => $payload['newUrl'] === 'http://localhost/filters/title/I Love Guitars/item_options/option1'); + ->assertDispatched('update-url', fn ($name, $payload) => str_contains($payload['newUrl'], 'filters/title/I Love Guitars/item_options/option1')); } }