feat: add useStations composable
This commit is contained in:
38
resources/js/composables/useStations.js
Normal file
38
resources/js/composables/useStations.js
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
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 = 'petrol', 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 }
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user