feat: add FuelFinder Livewire component
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
118
app/Livewire/Public/FuelFinder.php
Normal file
118
app/Livewire/Public/FuelFinder.php
Normal file
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Public;
|
||||
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\View\View;
|
||||
use Livewire\Attributes\Validate;
|
||||
use Livewire\Component;
|
||||
|
||||
class FuelFinder extends Component
|
||||
{
|
||||
#[Validate('required|string', message: 'Please enter a postcode, town or city.')]
|
||||
public string $search = '';
|
||||
|
||||
#[Validate('required|string', message: 'Please select a fuel type.')]
|
||||
public string $fuelType = 'petrol';
|
||||
|
||||
#[Validate('required|integer|min:1|max:20')]
|
||||
public int $radius = 5;
|
||||
|
||||
#[Validate('nullable|string|in:price,distance,updated,brand,reliable')]
|
||||
public string $sort = 'reliable';
|
||||
|
||||
public array $results = [];
|
||||
|
||||
public array $meta = [];
|
||||
|
||||
public ?array $prediction = null;
|
||||
|
||||
public ?string $apiError = null;
|
||||
|
||||
public bool $hasSearched = false;
|
||||
|
||||
public function updatedFuelType(): void
|
||||
{
|
||||
if ($this->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');
|
||||
}
|
||||
}
|
||||
3
resources/views/livewire/public/fuel-finder.blade.php
Normal file
3
resources/views/livewire/public/fuel-finder.blade.php
Normal file
@@ -0,0 +1,3 @@
|
||||
<div>
|
||||
<input type="text" name="search" wire:model="search" />
|
||||
</div>
|
||||
Reference in New Issue
Block a user