diff --git a/src/stores/creatorProfileStore.js b/src/stores/creatorProfileStore.js index 133de87..2f2d5e3 100644 --- a/src/stores/creatorProfileStore.js +++ b/src/stores/creatorProfileStore.js @@ -1,59 +1,69 @@ -import {computed, watch} from 'vue' -import {defineStore} from 'pinia' -import {useAuthStore} from "@/stores/authStore.js"; -import {useClient} from "@/plugins/api.js"; -import {useSessionStorage} from "@vueuse/core"; -import {useRouter} from "vue-router"; +import { useClient } from '@/plugins/api.js'; +import { useAuthStore } from '@/stores/authStore.js'; +import { useSessionStorage } from '@vueuse/core'; +import { defineStore } from 'pinia'; +import { computed, watch } from 'vue'; +import { useRouter } from 'vue-router'; +export const useCreatorProfileStore = defineStore('creator-profile', () => { + const router = useRouter(); -export const useCreatorProfileStore = defineStore( + const authStore = useAuthStore(); + + watch( + () => authStore.isAuthenticated, + async (newValue) => { + if (newValue) { + await fetchCurrentCreatorProfile(); + + if (value.value === undefined) { + await router.push('/'); + } else { + await router.push(`/@${value.value.name}`); + } + } else { + value.value = undefined; + } + } + ); + + const value = useSessionStorage( 'creator-profile', - () => { + {}, + { writeDefaults: false } + ); - const router = useRouter() - - const authStore = useAuthStore() + const hasCreator = computed( + () => value.value && Object.getOwnPropertyNames(value.value).length >= 1 + ); - watch( - () => authStore.isAuthenticated, - async (newValue) => { - if (newValue) { - await fetchCurrentCreatorProfile() - - if (value.value === undefined) { - await router.push('/') - } else { - await router.push(`/@${value.value.name}`) - } - } else { - value.value = undefined - } - }) + const client = useClient(); - const value = useSessionStorage( - 'creator-profile', - {}, - {writeDefaults: false}) + async function fetchCurrentCreatorProfile() { + try { + const creatorResponse = await client.get(`/api/creators/profile`); + value.value = creatorResponse.data; + // TODO: no cache-busting ??? + } catch (error) { + value.value = undefined; + } + } - const hasCreator = computed(() => - value.value - && Object.getOwnPropertyNames(value.value).length >= 1) + async function fetchSpecificCreatorProfile(creatorAlias) { + try { + const creatorResponse = await client.get( + `/api/creators/@${creatorAlias}` + ); + value.value = creatorResponse.data; + } catch (error) { + value.value = undefined; + } + } - const client = useClient() - - async function fetchCurrentCreatorProfile() { - try { - const creatorResponse = await client.get(`/api/creators/profile`) - value.value = creatorResponse.data - // TODO: no cache-busting ??? - } catch (error) { - value.value = undefined - } - } - - return { - creator: value, - hasCreator, - fetchCurrentCreatorProfile - } - }) + return { + creator: value, + hasCreator, + fetchCurrentCreatorProfile, + fetchSpecificCreatorProfile, + }; +});