Performing logins, mobile cards, and other tasks
This commit is contained in:
@@ -1,21 +1,28 @@
|
||||
import { computed, ref } from 'vue';
|
||||
import { computed, ref, watch } from 'vue';
|
||||
import { fetchMyBusinesses } from '@/api/businesses';
|
||||
import { useAuthStore } from '@/stores/auth';
|
||||
|
||||
const ownedBusinessIds = ref<Set<string>>(new Set());
|
||||
const loaded = ref(false);
|
||||
let loadPromise: Promise<void> | null = null;
|
||||
let authWatchStarted = false;
|
||||
|
||||
export function useOwnedBusiness() {
|
||||
const auth = useAuthStore();
|
||||
|
||||
async function ensureLoaded() {
|
||||
if (!auth.isBusinessOwner) {
|
||||
async function ensureLoaded(force = false) {
|
||||
if (auth.token && !auth.user) {
|
||||
await auth.loadUser();
|
||||
}
|
||||
|
||||
if (!auth.isLoggedIn || !auth.isBusinessOwner) {
|
||||
ownedBusinessIds.value = new Set();
|
||||
loaded.value = true;
|
||||
loadPromise = null;
|
||||
return;
|
||||
}
|
||||
if (loaded.value) return;
|
||||
|
||||
if (loaded.value && !force) return;
|
||||
if (loadPromise) {
|
||||
await loadPromise;
|
||||
return;
|
||||
@@ -36,6 +43,18 @@ export function useOwnedBusiness() {
|
||||
await loadPromise;
|
||||
}
|
||||
|
||||
if (!authWatchStarted) {
|
||||
authWatchStarted = true;
|
||||
watch(
|
||||
() => [auth.token, auth.user?.id, auth.user?.role] as const,
|
||||
() => {
|
||||
loaded.value = false;
|
||||
loadPromise = null;
|
||||
void ensureLoaded(true);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
function isOwnBusiness(businessId: string | null | undefined): boolean {
|
||||
if (!businessId || !auth.isBusinessOwner) return false;
|
||||
return ownedBusinessIds.value.has(businessId);
|
||||
|
||||
Reference in New Issue
Block a user