From 130576c9ba2d988ebbf4c46b7f1776accf764601 Mon Sep 17 00:00:00 2001 From: Ovidiu U Date: Sun, 5 Apr 2026 20:28:15 +0100 Subject: [PATCH] feat: implement StationSearch Livewire component --- app/Livewire/Public/StationSearch.php | 68 +++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 app/Livewire/Public/StationSearch.php diff --git a/app/Livewire/Public/StationSearch.php b/app/Livewire/Public/StationSearch.php new file mode 100644 index 0000000..6d9751c --- /dev/null +++ b/app/Livewire/Public/StationSearch.php @@ -0,0 +1,68 @@ +validate(); + + $this->results = []; + $this->meta = []; + $this->apiError = null; + + $radiusKm = round($this->radius * 1.60934, 2); + + $response = Http::timeout(10) + ->withHeaders(['X-Api-Key' => config('services.fuelalert.api_key')]) + ->get(url('/api/stations'), [ + 'postcode' => $this->search, + 'fuel_type' => $this->fuelType, + 'radius' => $radiusKm, + 'sort' => 'price', + ]); + + if ($response->status() === 422) { + $errors = $response->json('errors', []); + $this->apiError = collect($errors)->flatten()->first() + ?? $response->json('message', 'Validation error.'); + + return; + } + + if (! $response->successful()) { + $this->apiError = 'Unable to fetch stations. Please try again.'; + + return; + } + + $this->results = $response->json('data', []); + $this->meta = $response->json('meta', []); + } + + public function render(): View + { + return view('livewire.public.station-search'); + } +}