32 lines
968 B
JavaScript
32 lines
968 B
JavaScript
import { defineStore } from 'pinia'
|
|
import { ref } from 'vue'
|
|
import { formatRelative } from 'date-fns'
|
|
import { fr } from 'date-fns/locale'
|
|
import { API_BASE_URL } from '@/config.js'
|
|
import { fromLonLat } from 'ol/proj.js'
|
|
|
|
export const useLocationStore = defineStore('location', () => {
|
|
const locationApiPath = API_BASE_URL + '/location/last'
|
|
|
|
const lastLocation = ref(null)
|
|
|
|
function fetchLocation() {
|
|
return fetch(locationApiPath)
|
|
.then((response) => {
|
|
return response.json()
|
|
})
|
|
.then(async (loc) => {
|
|
const locDate = new Date(loc.timestamp)
|
|
loc.formatedDate = formatRelative(locDate, new Date(), { locale: fr })
|
|
loc.projectedCoordinates = fromLonLat([loc.longitude, loc.latitude])
|
|
lastLocation.value = loc
|
|
|
|
return location.value
|
|
})
|
|
.catch((error) => {
|
|
console.log('fetch last location failed with: ' + error)
|
|
})
|
|
}
|
|
|
|
return { lastLocation, fetchLocation }
|
|
})
|