You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
892 B
32 lines
892 B
export const useCategoriesStore = defineStore('categories', () => {
|
|
const tree = ref<any[]>([]);
|
|
const tags = ref<any[]>([]);
|
|
|
|
async function fetchCategories() {
|
|
const res = await $fetch<any>('/api/categories');
|
|
if (res.code === 0) tree.value = res.data;
|
|
}
|
|
|
|
async function fetchTags() {
|
|
const res = await $fetch<any>('/api/tags');
|
|
if (res.code === 0) tags.value = res.data;
|
|
}
|
|
|
|
async function createCategory(data: any) {
|
|
const res = await $fetch<any>('/api/categories', {
|
|
method: 'POST',
|
|
body: data,
|
|
});
|
|
if (res.code === 0) await fetchCategories();
|
|
return res;
|
|
}
|
|
|
|
const topTags = computed(() => {
|
|
return tags.value
|
|
.filter((t: any) => t.itemCount > 0)
|
|
.sort((a: any, b: any) => b.itemCount - a.itemCount)
|
|
.slice(0, 10);
|
|
});
|
|
|
|
return { tree, tags, fetchCategories, fetchTags, createCategory, topTags };
|
|
});
|
|
|