store current location in pinia

This commit is contained in:
Will Freeman
2024-12-22 21:11:38 -08:00
parent 53db867385
commit 7844a66b63
8 changed files with 384 additions and 357 deletions
+32
View File
@@ -0,0 +1,32 @@
import { defineStore } from 'pinia';
import { ref, type Ref } from 'vue';
export const useGlobalStore = defineStore('global', () => {
const currentLocation: Ref<[number, number] | null> = ref(null);
const setCurrentLocation = (): Promise<[number, number]> =>
new Promise((resolve, reject) => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
(position) => {
currentLocation.value = [position.coords.latitude, position.coords.longitude];
resolve([position.coords.latitude, position.coords.longitude]);
},
(error) => {
reject(error);
},
{
timeout: 10000,
enableHighAccuracy: true,
}
);
} else {
reject(new Error('Geolocation is not supported by this browser.'));
}
});
return {
currentLocation,
setCurrentLocation,
};
});