115 lines
3.4 KiB
JavaScript
115 lines
3.4 KiB
JavaScript
import { computed, ref, watch } from 'vue';
|
|
import { defineStore } from 'pinia';
|
|
import { useAuthStore } from '@/features/auth/stores/authStore.js';
|
|
import { useWorkspaceStore } from '@/features/workspaces/stores/workspaceStore.js';
|
|
import { useClient } from '@/plugins/api.js';
|
|
|
|
export const useContentItemsStore = defineStore('content-items', () => {
|
|
const authStore = useAuthStore();
|
|
const workspaceStore = useWorkspaceStore();
|
|
const client = useClient();
|
|
|
|
const items = ref([]);
|
|
const isLoading = ref(false);
|
|
const isCreating = ref(false);
|
|
const error = ref(null);
|
|
|
|
const activeCount = computed(() =>
|
|
items.value.filter(item => !['Approved', 'Scheduled', 'Published'].includes(item.status))
|
|
.length
|
|
);
|
|
|
|
async function fetchContentItems(filters = {}) {
|
|
if (!authStore.isAuthenticated) {
|
|
items.value = [];
|
|
error.value = null;
|
|
return;
|
|
}
|
|
|
|
isLoading.value = true;
|
|
error.value = null;
|
|
|
|
try {
|
|
const response = await client.get('/api/content-items', {
|
|
params: {
|
|
workspaceId: workspaceStore.activeWorkspaceId ?? undefined,
|
|
clientId: filters.clientId,
|
|
campaignId: filters.campaignId,
|
|
},
|
|
});
|
|
|
|
items.value = (response.data ?? []).filter(item =>
|
|
workspaceStore.isWorkspaceVisible(item.workspaceId)
|
|
);
|
|
} catch (fetchError) {
|
|
console.error('Failed to fetch content items:', fetchError);
|
|
items.value = [];
|
|
error.value = 'Failed to load content items.';
|
|
} finally {
|
|
isLoading.value = false;
|
|
}
|
|
}
|
|
|
|
async function createContentItem(payload) {
|
|
if (!authStore.isAuthenticated || !workspaceStore.activeWorkspaceId) {
|
|
throw new Error('You must be authenticated to create a content item.');
|
|
}
|
|
|
|
if (isCreating.value) {
|
|
throw new Error('A content item creation request is already in progress.');
|
|
}
|
|
|
|
isCreating.value = true;
|
|
error.value = null;
|
|
|
|
try {
|
|
const response = await client.post('/api/content-items', {
|
|
...payload,
|
|
workspaceId: workspaceStore.activeWorkspaceId,
|
|
});
|
|
|
|
if (response.data) {
|
|
items.value = [response.data, ...items.value];
|
|
}
|
|
|
|
return response.data;
|
|
} catch (createError) {
|
|
console.error('Failed to create content item:', createError);
|
|
error.value = 'Failed to create content item.';
|
|
throw createError;
|
|
} finally {
|
|
isCreating.value = false;
|
|
}
|
|
}
|
|
|
|
async function fetchContentItem(id) {
|
|
const response = await client.get(`/api/content-items/${id}`);
|
|
return response.data;
|
|
}
|
|
|
|
watch(
|
|
() => [authStore.isAuthenticated, workspaceStore.workspaceScopeKey],
|
|
async ([isAuthenticated]) => {
|
|
if (!isAuthenticated) {
|
|
items.value = [];
|
|
error.value = null;
|
|
return;
|
|
}
|
|
|
|
await fetchContentItems();
|
|
},
|
|
{ immediate: true }
|
|
);
|
|
|
|
return {
|
|
items,
|
|
isLoading,
|
|
isCreating,
|
|
error,
|
|
activeCount,
|
|
fetchContentItems,
|
|
fetchContentItem,
|
|
createContentItem,
|
|
};
|
|
});
|