From 7c288f01e80d5813ed94e59cd4de1a598394c20b Mon Sep 17 00:00:00 2001 From: Zack Spear Date: Thu, 1 Jun 2023 15:01:05 -0700 Subject: [PATCH] refactor: finalize WanIpCheck web component --- components/WanIpCheck.ce.vue | 60 ++++++++++++++++++++---------------- store/wanIpCheck.ts | 26 ++++++++++++++++ 2 files changed, 59 insertions(+), 27 deletions(-) create mode 100644 store/wanIpCheck.ts diff --git a/components/WanIpCheck.ce.vue b/components/WanIpCheck.ce.vue index 8511dfaab..eab70ff22 100644 --- a/components/WanIpCheck.ce.vue +++ b/components/WanIpCheck.ce.vue @@ -8,45 +8,51 @@ export interface Props { phpWanIp?: string; } -withDefaults(defineProps(), { - phpWanIp: '0.0.0.0', -}); +const props = defineProps(); -const serverStore = useServerStore(); const { isRemoteAccess } = storeToRefs(useServerStore()); -const wanIp = ref(sessionStorage.getItem('unraidConnect_wanIp')); +const wanIp = ref(); +const fetchError = ref(); +const loading = ref(false); -if (wanIp.value) { - const error = ref(null); - // @fix [Vue warn]: Property "pending" was accessed during render but is not defined on instance. - const pending = ref(false); -} else { - const { data, pending, error, refresh } = await useFetch('https://wanip4.unraid.net/', { - onRequestError({ request, options, error }) { // Handle the request errors - console.debug('[onRequestError]', { request, options, error }); - }, - onResponse({ request, response, options }) { // Process the response data - wanIp.value = response._data as string; // response returns text nothing to traverse +const computedError = computed(() => { + if (!props.phpWanIp) return 'DNS issue, unable to resolve wanip4.unraid.net'; + if (fetchError.value) return fetchError.value; + return; +}); + +onBeforeMount(() => { + wanIp.value = sessionStorage.getItem('unraidConnect_wanIp'); +}); + +watch(wanIp, async () => { + console.debug('[watch] wanIp'); + // if we don't have a client WAN IP AND we have the server WAN IP then we fetch + if (!wanIp.value && props.phpWanIp) { + loading.value = true; + const { data, error } = await useFetch('https://wanip4.unraid.net/'); + if (data.value) { + loading.value = false; + wanIp.value = data.value as string; // response returns text nothing to traverse // save in sessionStorage so we only make this request once per webGUI session sessionStorage.setItem('unraidConnect_wanIp', wanIp.value); - }, - onResponseError({ request, response, options }) { // Handle the response errors - console.debug('[onResponseError]', { request, response, options }); + } else if (error.value) { + loading.value = false; + fetchError.value = error.value; } - }); -} - + } +}); diff --git a/store/wanIpCheck.ts b/store/wanIpCheck.ts new file mode 100644 index 000000000..2516e17fb --- /dev/null +++ b/store/wanIpCheck.ts @@ -0,0 +1,26 @@ +import { defineStore, createPinia, setActivePinia } from "pinia"; +import { useServerStore } from './server'; +/** + * @see https://stackoverflow.com/questions/73476371/using-pinia-with-vue-js-web-components + * @see https://github.com/vuejs/pinia/discussions/1085 + */ +setActivePinia(createPinia()); + +export const useWanIpCheckStore = defineStore('wanIpCheck', () => { + const serverStore = useServerStore(); + /** + * State + */ + const wanIp = ref(sessionStorage.getItem('unraidConnect_wanIp')); + /** + * Getters + */ + /** + * Actions + */ + return { + // state + // getters + // actions + }; +});