feat: add useSavedStations composable
This commit is contained in:
33
resources/js/composables/useSavedStations.js
Normal file
33
resources/js/composables/useSavedStations.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import { ref } from 'vue'
|
||||
import api from '../axios.js'
|
||||
|
||||
export function useSavedStations() {
|
||||
const savedStations = ref([])
|
||||
const loading = ref(false)
|
||||
|
||||
async function fetch() {
|
||||
loading.value = true
|
||||
try {
|
||||
const response = await api.get('/user/saved-stations')
|
||||
savedStations.value = response.data.data
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function save(stationId) {
|
||||
await api.post('/user/saved-stations', { station_id: stationId })
|
||||
await fetch()
|
||||
}
|
||||
|
||||
async function remove(stationId) {
|
||||
await api.delete(`/user/saved-stations/${stationId}`)
|
||||
savedStations.value = savedStations.value.filter(s => s.station_id !== stationId)
|
||||
}
|
||||
|
||||
function isSaved(stationId) {
|
||||
return savedStations.value.some(s => s.station_id === stationId)
|
||||
}
|
||||
|
||||
return { savedStations, loading, fetch, save, remove, isSaved }
|
||||
}
|
||||
Reference in New Issue
Block a user