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

71 lines
2.0 KiB
PHP

<?php
namespace App\Livewire\StationLocator;
use Livewire\Component;
use Illuminate\Support\Facades\Http;
use Jantinnerezo\LivewireAlert\LivewireAlert;
class StationList extends Component
{
use LivewireAlert;
public $stations = [];
public $uploadPriceModal = false;
public $locationModal = false;
public $dataLocation = null;
public function mount()
{
$this->fetchStations();
}
public function fetchStations()
{
try {
$response = Http::get('https://api.example.com/stations');
if ($response->successful()) {
$this->stations = $response->json()['data'] ?? [];
} else {
$this->alert('error', 'Failed to fetch stations.');
}
} 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 station?')) {
try {
$response = Http::delete("https://api.example.com/stations/{$id}");
if ($response->successful()) {
$this->alert('success', 'Station deleted successfully!');
$this->fetchStations();
} else {
$this->alert('error', 'Failed to delete station.');
}
} catch (\Exception $e) {
$this->alert('error', 'An error occurred: ' . $e->getMessage());
}
}
}
public function showLocation($station)
{
$this->dataLocation = $station;
$this->locationModal = true;
}
public function toggleUploadPriceModal()
{
$this->uploadPriceModal = !$this->uploadPriceModal;
}
public function render()
{
return view('livewire.station-locator.station-list', [
'stations' => $this->stations,
])->layout('layouts.app', ['title' => 'Station List']);
}
}