diff --git a/app/Livewire/Public/FuelFinder.php b/app/Livewire/Public/FuelFinder.php new file mode 100644 index 0000000..49f879b --- /dev/null +++ b/app/Livewire/Public/FuelFinder.php @@ -0,0 +1,118 @@ +hasSearched) { + $this->findStations(); + } + } + + public function updatedRadius(): void + { + if ($this->hasSearched) { + $this->findStations(); + } + } + + public function updatedSort(): void + { + if ($this->hasSearched) { + $this->findStations(); + } + } + + public function findStations(): void + { + $this->validate(); + + $this->results = []; + $this->meta = []; + $this->prediction = null; + $this->apiError = null; + $this->hasSearched = false; + + $radiusKm = round($this->radius * 1.60934, 2); + + try { + $response = Http::timeout(10) + ->withHeaders(['X-Api-Key' => config('app.api_secret_key')]) + ->get(url('/api/stations'), [ + 'postcode' => $this->search, + 'fuel_type' => $this->fuelType, + 'radius' => $radiusKm, + 'sort' => $this->sort, + ]); + } catch (ConnectionException) { + $this->apiError = 'Unable to fetch stations. Please try again.'; + + return; + } + + 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', []); + $this->hasSearched = true; + + try { + $predictionResponse = Http::timeout(10) + ->withHeaders(['X-Api-Key' => config('app.api_secret_key')]) + ->get(url('/api/prediction')); + + if ($predictionResponse->successful()) { + $this->prediction = $predictionResponse->json(); + } + } catch (ConnectionException) { + // Prediction failure is silent — stations are more important + } + } + + public function render(): View + { + return view('livewire.public.fuel-finder'); + } +} diff --git a/resources/views/livewire/public/fuel-finder.blade.php b/resources/views/livewire/public/fuel-finder.blade.php new file mode 100644 index 0000000..354ab7d --- /dev/null +++ b/resources/views/livewire/public/fuel-finder.blade.php @@ -0,0 +1,3 @@ +