From bbbef2d60c1c50e422ba1f4e533d2b2598f8b8db Mon Sep 17 00:00:00 2001 From: Ovidiu U Date: Fri, 10 Apr 2026 18:01:18 +0100 Subject: [PATCH] feat: add useStations composable --- resources/js/composables/useStations.js | 38 +++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 resources/js/composables/useStations.js diff --git a/resources/js/composables/useStations.js b/resources/js/composables/useStations.js new file mode 100644 index 0000000..52efe2c --- /dev/null +++ b/resources/js/composables/useStations.js @@ -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 } +}