Skip to content

Commit

Permalink
chore: clean up useFetch composable
Browse files Browse the repository at this point in the history
  • Loading branch information
TwistTheNeil committed Oct 30, 2023
1 parent 68ac5fd commit 1bd7cb7
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
4 changes: 2 additions & 2 deletions frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<script setup lang="ts">
import { onBeforeMount, ref } from 'vue';
import { RouterView } from 'vue-router';
import { useFetch_GetMaptilerToken } from './composables/useFetch';
import { getMaptilerToken } from './composables/useFetch';
import { useRoute } from 'vue-router';
import { useMaxmindDataStore } from './stores/maxmindDataStore';
Expand All @@ -18,7 +18,7 @@ const maxmindDataStore = useMaxmindDataStore();
const ready = ref(false);
onBeforeMount(async () => {
await useFetch_GetMaptilerToken();
await getMaptilerToken();
if (!route.query.address || route.query.address === "") {
await maxmindDataStore.$reset();
} else {
Expand Down
9 changes: 6 additions & 3 deletions frontend/src/composables/useFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ import type { Ref } from 'vue'

import type { MaxmindBackendResponse } from '@/types/MaxmindBackend';

// TODO: why param? remove
export async function useFetch_GetMaxmindData(url: string): Promise<{ data: Ref<MaxmindBackendResponse| null>, error: Ref<string | null> }> {
export async function getMaxmindData(ipAddress?: string): Promise<{ data: Ref<MaxmindBackendResponse| null>, error: Ref<string | null> }> {
const data: Ref<MaxmindBackendResponse | null> = ref(null);
const error: Ref<string | null> = ref(null);

try {
let url = '/api/geoip';
if (ipAddress) {
url = `/api/geoip/${ipAddress}`;
}
const fetchPromise = await fetch(url);
data.value = await fetchPromise.json();
} catch (err: any) {
Expand All @@ -18,7 +21,7 @@ export async function useFetch_GetMaxmindData(url: string): Promise<{ data: Ref<
return { data, error };
};

export async function useFetch_GetMaptilerToken(): Promise<{ data: Ref<string>, error: Ref<string | null> }> {
export async function getMaptilerToken(): Promise<{ data: Ref<string>, error: Ref<string | null> }> {
const data: Ref<string> = ref("");
const error: Ref<string | null> = ref(null);

Expand Down
4 changes: 2 additions & 2 deletions frontend/src/composables/useMaptilerToken.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ref } from 'vue';
import type { Ref } from 'vue';

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

const maptilerToken: Ref<string> = ref("");

Expand All @@ -11,7 +11,7 @@ export const useMaptilerToken = async () : Promise<string> => {
}

// TODO: error?
const { data, error } = await useFetch_GetMaptilerToken();
const { data, error } = await getMaptilerToken();
maptilerToken.value = data.value;

return maptilerToken.value;
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/stores/maxmindDataStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { ref } from 'vue';
import type { Ref } from 'vue';
import { defineStore } from 'pinia';
import type { MaxmindBackendResponse } from '@/types/MaxmindBackend';
import { useFetch_GetMaxmindData } from '@/composables/useFetch';
import { getMaxmindData } from '@/composables/useFetch';

export const useMaxmindDataStore = defineStore('maxindData', () => {
const data: Ref<MaxmindBackendResponse | null> = ref(null);
const error: Ref<string | null> = ref(null);

async function $reset() {
const { data: d, error: e } = await useFetch_GetMaxmindData('/api/geoip');
const { data: d, error: e } = await getMaxmindData();
data.value = d.value;
error.value = e.value;
};
Expand All @@ -19,7 +19,7 @@ export const useMaxmindDataStore = defineStore('maxindData', () => {
return;
}

const { data: d, error: e } = await useFetch_GetMaxmindData(`/api/geoip/${ipAddress}`);
const { data: d, error: e } = await getMaxmindData(ipAddress);
data.value = d.value;
error.value = e.value;
};
Expand Down

0 comments on commit 1bd7cb7

Please sign in to comment.