feat: extract fuel.map component and wire Leaflet to map-update browser event

This commit is contained in:
Ovidiu U
2026-04-08 08:46:33 +01:00
parent 0809a5340b
commit bce5aa72c8
5 changed files with 64 additions and 7 deletions

View File

@@ -0,0 +1,21 @@
<?php
namespace App\Livewire\Public\Fuel;
use Illuminate\View\View;
use Livewire\Attributes\On;
use Livewire\Component;
final class Map extends Component
{
#[On('stations-found')]
public function handle(array $results, array $meta, int $radius = 5, ?array $prediction = null): void
{
$this->dispatch('map-update', results: $results, meta: $meta, radius: $radius);
}
public function render(): View
{
return view('livewire.public.fuel.map');
}
}

View File

@@ -65,11 +65,13 @@ export function stationMap(results, meta, radius) {
maxZoom: 19,
}).addTo(this._map);
if (this.results && this.results.length > 0) {
window.addEventListener('map-update', (e) => {
this.results = e.detail.results;
this.meta = e.detail.meta;
this.radius = e.detail.radius;
this._plotMarkers();
}
});
this.$watch('results', () => this._plotMarkers());
this.locateUser();
},

View File

@@ -1,6 +1,4 @@
@props(['results' => []])
<div
x-data="stationMap(@entangle('results'), @entangle('meta'), @entangle('radius'))"
class="h-56 w-full overflow-hidden border-y border-[#e5ded7] md:h-96"
x-data="stationMap([], {}, 5)"
class="h-56 w-full overflow-hidden border-y border-border md:h-96"
></div>

View File

@@ -0,0 +1,3 @@
<div class="mb-4" wire:ignore>
<x-fuel.station-map />
</div>

View File

@@ -0,0 +1,33 @@
<?php
use App\Livewire\Public\Fuel\Map;
use Livewire\Livewire;
it('renders the map component', function () {
Livewire::test(Map::class)
->assertStatus(200);
});
it('dispatches map-update browser event when stations-found is received', function () {
Livewire::test(Map::class)
->dispatch('stations-found',
results: [['name' => 'BP Garage']],
meta: ['lat' => 51.5, 'lng' => -0.1, 'count' => 1],
radius: 5,
prediction: null
)
->assertDispatched('map-update');
});
it('passes radius in map-update payload', function () {
Livewire::test(Map::class)
->dispatch('stations-found',
results: [],
meta: ['lat' => 51.5, 'lng' => -0.1, 'count' => 0],
radius: 10,
prediction: null
)
->assertDispatched('map-update', fn ($event, $params) =>
$params['radius'] === 10
);
});