Skip to content

Commit

Permalink
Use pinia to initialize config from backend
Browse files Browse the repository at this point in the history
  • Loading branch information
TwistTheNeil committed Feb 20, 2024
1 parent b274e06 commit 9232e70
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
8 changes: 5 additions & 3 deletions frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@
<script setup lang="ts">
import { onBeforeMount, ref } from 'vue';
import { RouterView } from 'vue-router';
import { getMaptilerToken } from './composables/useFetch';
import { useRoute } from 'vue-router';
import { useMaxmindDataStore } from './stores/maxmindDataStore';
import { useConfigStore } from '@/stores/config';
import { useMaxmindDataStore } from '@/stores/maxmindDataStore';
const route = useRoute();
const maxmindDataStore = useMaxmindDataStore();
const configStore = useConfigStore();
const ready = ref(false);
onBeforeMount(async () => {
await getMaptilerToken();
await configStore.$reset();
if (!route.query.address || route.query.address === "") {
await maxmindDataStore.$reset();
} else {
Expand Down
13 changes: 6 additions & 7 deletions frontend/src/components/ApproximateMap.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@
import maplibregl from 'maplibre-gl';
import { storeToRefs } from 'pinia';
import { useMaptilerToken } from '@/composables/useMaptilerToken';
import { useConfigStore } from '@/stores/config';
import { useMaxmindDataStore } from '@/stores/maxmindDataStore';
const configStore = useConfigStore();
const { maptilerToken } = storeToRefs(configStore);
const maxmindDataStore = useMaxmindDataStore();
const { data } = storeToRefs(maxmindDataStore);
const center = computed(() => ({ lng: data.value!.Location.Longitude, lat: data.value!.Location.Latitude }));
const updateMap = async () => {
// TODO: move this out? i don't like it being called on every update
// currently error if we move it out of `onMounted` specifically
const maptilerToken = await useMaptilerToken();
const map = new maplibregl.Map({
container: 'map',
style: `https://api.maptiler.com/maps/streets/style.json?key=${maptilerToken}`,
style: `https://api.maptiler.com/maps/streets/style.json?key=${maptilerToken.value}`,
center: center.value,
zoom: 10,
});
Expand All @@ -32,8 +31,8 @@
.addTo(map);
};
onMounted(() => updateMap());
onUpdated(() => updateMap());
onMounted(updateMap);
onUpdated(updateMap);
</script>

<style>
Expand Down
19 changes: 0 additions & 19 deletions frontend/src/composables/useMaptilerToken.ts

This file was deleted.

20 changes: 20 additions & 0 deletions frontend/src/stores/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ref } from 'vue';
import type { Ref } from 'vue';
import { defineStore } from 'pinia';

import { getMaptilerToken } from '@/composables/useFetch';

export const useConfigStore = defineStore('config', () => {
const maptilerToken: Ref<string> = ref('');

async function $reset() {
// TODO: error?
const { data, error } = await getMaptilerToken();
maptilerToken.value = data.value;
};

return {
maptilerToken,
$reset,
};
});

0 comments on commit 9232e70

Please sign in to comment.