39 lines
1.1 KiB
JavaScript
39 lines
1.1 KiB
JavaScript
import { ref } from 'vue'
|
|
import api from '../axios.js'
|
|
|
|
export function useStations() {
|
|
const stations = ref([])
|
|
const meta = ref(null)
|
|
const loading = ref(false)
|
|
const error = ref(null)
|
|
|
|
async function search({ postcode, lat, lng, fuelType = 'e10', radius = 10, sort = 'price' }) {
|
|
loading.value = true
|
|
error.value = null
|
|
stations.value = []
|
|
meta.value = null
|
|
|
|
const params = { fuel_type: fuelType, radius, sort }
|
|
|
|
if (postcode) {
|
|
params.postcode = postcode
|
|
} else if (lat && lng) {
|
|
params.lat = lat
|
|
params.lng = lng
|
|
}
|
|
|
|
try {
|
|
const response = await api.get('/stations', { params })
|
|
stations.value = response.data.data
|
|
meta.value = response.data.meta
|
|
} catch (err) {
|
|
error.value = err.response?.data?.errors
|
|
?? { general: ['Unable to load stations. Please try again.'] }
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
return { stations, meta, loading, error, search }
|
|
}
|