travel-steps/summer2024-frontend/src/stores/location.js
Nicolas Froger 130581a411
add location logging, display last location on map
Signed-off-by: Nicolas Froger <nicolas@kektus.xyz>
2024-07-26 15:38:23 +02:00

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 }
})