Fetch specific creator profile
This commit is contained in:
@@ -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();
|
||||||
'creator-profile',
|
|
||||||
() => {
|
|
||||||
|
|
||||||
const router = useRouter()
|
|
||||||
|
|
||||||
const authStore = useAuthStore()
|
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
() => authStore.isAuthenticated,
|
() => authStore.isAuthenticated,
|
||||||
async (newValue) => {
|
async (newValue) => {
|
||||||
if (newValue) {
|
if (newValue) {
|
||||||
await fetchCurrentCreatorProfile()
|
await fetchCurrentCreatorProfile();
|
||||||
|
|
||||||
if (value.value === undefined) {
|
if (value.value === undefined) {
|
||||||
await router.push('/')
|
await router.push('/');
|
||||||
} else {
|
} else {
|
||||||
await router.push(`/@${value.value.name}`)
|
await router.push(`/@${value.value.name}`);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
value.value = undefined
|
value.value = undefined;
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
);
|
||||||
|
|
||||||
const value = useSessionStorage(
|
const value = useSessionStorage(
|
||||||
'creator-profile',
|
'creator-profile',
|
||||||
{},
|
{},
|
||||||
{writeDefaults: false})
|
{ writeDefaults: false }
|
||||||
|
);
|
||||||
|
|
||||||
const hasCreator = computed(() =>
|
const hasCreator = computed(
|
||||||
value.value
|
() => value.value && Object.getOwnPropertyNames(value.value).length >= 1
|
||||||
&& Object.getOwnPropertyNames(value.value).length >= 1)
|
);
|
||||||
|
|
||||||
const client = useClient()
|
const client = useClient();
|
||||||
|
|
||||||
async function fetchCurrentCreatorProfile() {
|
async function fetchCurrentCreatorProfile() {
|
||||||
try {
|
try {
|
||||||
const creatorResponse = await client.get(`/api/creators/profile`)
|
const creatorResponse = await client.get(`/api/creators/profile`);
|
||||||
value.value = creatorResponse.data
|
value.value = creatorResponse.data;
|
||||||
// TODO: no cache-busting ???
|
// TODO: no cache-busting ???
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
value.value = undefined
|
value.value = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchSpecificCreatorProfile(creatorAlias) {
|
||||||
|
try {
|
||||||
|
const creatorResponse = await client.get(
|
||||||
|
`/api/creators/@${creatorAlias}`
|
||||||
|
);
|
||||||
|
value.value = creatorResponse.data;
|
||||||
|
} catch (error) {
|
||||||
|
value.value = undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
creator: value,
|
creator: value,
|
||||||
hasCreator,
|
hasCreator,
|
||||||
fetchCurrentCreatorProfile
|
fetchCurrentCreatorProfile,
|
||||||
}
|
fetchSpecificCreatorProfile,
|
||||||
})
|
};
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user