cms-laravel/app/Livewire/StationLocator/FuelList.php

58 lines
1.6 KiB
PHP

<?php
namespace App\Livewire\StationLocator;
use Livewire\Component;
use Illuminate\Support\Facades\Http;
use Jantinnerezo\LivewireAlert\LivewireAlert;
class FuelList extends Component
{
use LivewireAlert;
public $fuels = [];
public $updating = false;
public function mount()
{
$this->fetchFuels();
}
public function fetchFuels()
{
try {
$response = Http::get('https://api.example.com/fuels');
if ($response->successful()) {
$this->fuels = $response->json()['data'] ?? [];
} else {
$this->alert('error', 'Failed to fetch fuels.');
}
} catch (\Exception $e) {
$this->alert('error', 'An error occurred: ' . $e->getMessage());
}
}
public function delete($id)
{
if (confirm('Are you sure you want to delete this fuel?')) {
try {
$this->updating = true;
$response = Http::delete("https://api.example.com/fuels/{$id}");
if ($response->successful()) {
$this->alert('success', 'Fuel deleted successfully!');
$this->fetchFuels();
} else {
$this->alert('error', 'Failed to delete fuel.');
}
} catch (\Exception $e) {
$this->alert('error', 'An error occurred: ' . $e->getMessage());
} finally {
$this->updating = false;
}
}
}
public function render()
{
return view('livewire.station-locator.fuel-list')->layout('layouts.app', ['title' => 'Fuel List']);
}
}