Skip to content

Commit 9232e70

Browse files
committed
Use pinia to initialize config from backend
1 parent b274e06 commit 9232e70

File tree

4 files changed

+31
-29
lines changed

4 files changed

+31
-29
lines changed

frontend/src/App.vue

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@
88
<script setup lang="ts">
99
import { onBeforeMount, ref } from 'vue';
1010
import { RouterView } from 'vue-router';
11-
import { getMaptilerToken } from './composables/useFetch';
1211
import { useRoute } from 'vue-router';
1312
14-
import { useMaxmindDataStore } from './stores/maxmindDataStore';
13+
import { useConfigStore } from '@/stores/config';
14+
import { useMaxmindDataStore } from '@/stores/maxmindDataStore';
1515
1616
const route = useRoute();
1717
const maxmindDataStore = useMaxmindDataStore();
18+
const configStore = useConfigStore();
1819
const ready = ref(false);
1920
2021
onBeforeMount(async () => {
21-
await getMaptilerToken();
22+
await configStore.$reset();
23+
2224
if (!route.query.address || route.query.address === "") {
2325
await maxmindDataStore.$reset();
2426
} else {

frontend/src/components/ApproximateMap.vue

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,20 @@
88
import maplibregl from 'maplibre-gl';
99
import { storeToRefs } from 'pinia';
1010
11-
import { useMaptilerToken } from '@/composables/useMaptilerToken';
11+
import { useConfigStore } from '@/stores/config';
1212
import { useMaxmindDataStore } from '@/stores/maxmindDataStore';
1313
14+
const configStore = useConfigStore();
15+
const { maptilerToken } = storeToRefs(configStore);
1416
const maxmindDataStore = useMaxmindDataStore();
1517
const { data } = storeToRefs(maxmindDataStore);
1618
1719
const center = computed(() => ({ lng: data.value!.Location.Longitude, lat: data.value!.Location.Latitude }));
1820
1921
const updateMap = async () => {
20-
// TODO: move this out? i don't like it being called on every update
21-
// currently error if we move it out of `onMounted` specifically
22-
const maptilerToken = await useMaptilerToken();
2322
const map = new maplibregl.Map({
2423
container: 'map',
25-
style: `https://api.maptiler.com/maps/streets/style.json?key=${maptilerToken}`,
24+
style: `https://api.maptiler.com/maps/streets/style.json?key=${maptilerToken.value}`,
2625
center: center.value,
2726
zoom: 10,
2827
});
@@ -32,8 +31,8 @@
3231
.addTo(map);
3332
};
3433
35-
onMounted(() => updateMap());
36-
onUpdated(() => updateMap());
34+
onMounted(updateMap);
35+
onUpdated(updateMap);
3736
</script>
3837

3938
<style>

frontend/src/composables/useMaptilerToken.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

frontend/src/stores/config.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { ref } from 'vue';
2+
import type { Ref } from 'vue';
3+
import { defineStore } from 'pinia';
4+
5+
import { getMaptilerToken } from '@/composables/useFetch';
6+
7+
export const useConfigStore = defineStore('config', () => {
8+
const maptilerToken: Ref<string> = ref('');
9+
10+
async function $reset() {
11+
// TODO: error?
12+
const { data, error } = await getMaptilerToken();
13+
maptilerToken.value = data.value;
14+
};
15+
16+
return {
17+
maptilerToken,
18+
$reset,
19+
};
20+
});

0 commit comments

Comments
 (0)