Fetch specific creator profile

This commit is contained in:
Karl Carriere
2024-12-17 09:48:33 -05:00
parent 762bddbec7
commit 8875038270

View File

@@ -1,59 +1,69 @@
import {computed, watch} from 'vue' import { useClient } from '@/plugins/api.js';
import {defineStore} from 'pinia' import { useAuthStore } from '@/stores/authStore.js';
import {useAuthStore} from "@/stores/authStore.js"; import { useSessionStorage } from '@vueuse/core';
import {useClient} from "@/plugins/api.js"; import { defineStore } from 'pinia';
import {useSessionStorage} from "@vueuse/core"; import { computed, watch } from 'vue';
import {useRouter} from "vue-router"; 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', 'creator-profile',
() => { {},
{ writeDefaults: false }
);
const router = useRouter() const hasCreator = computed(
() => value.value && Object.getOwnPropertyNames(value.value).length >= 1
const authStore = useAuthStore() );
watch( const client = useClient();
() => 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( async function fetchCurrentCreatorProfile() {
'creator-profile', try {
{}, const creatorResponse = await client.get(`/api/creators/profile`);
{writeDefaults: false}) value.value = creatorResponse.data;
// TODO: no cache-busting ???
} catch (error) {
value.value = undefined;
}
}
const hasCreator = computed(() => async function fetchSpecificCreatorProfile(creatorAlias) {
value.value try {
&& Object.getOwnPropertyNames(value.value).length >= 1) const creatorResponse = await client.get(
`/api/creators/@${creatorAlias}`
);
value.value = creatorResponse.data;
} catch (error) {
value.value = undefined;
}
}
const client = useClient() return {
creator: value,
async function fetchCurrentCreatorProfile() { hasCreator,
try { fetchCurrentCreatorProfile,
const creatorResponse = await client.get(`/api/creators/profile`) fetchSpecificCreatorProfile,
value.value = creatorResponse.data };
// TODO: no cache-busting ??? });
} catch (error) {
value.value = undefined
}
}
return {
creator: value,
hasCreator,
fetchCurrentCreatorProfile
}
})