add location logging, display last location on map
Signed-off-by: Nicolas Froger <nicolas@kektus.xyz>
This commit is contained in:
parent
4356816f07
commit
130581a411
13 changed files with 386 additions and 3 deletions
32
summer2024-frontend/src/stores/location.js
Normal file
32
summer2024-frontend/src/stores/location.js
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
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 }
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue