phongdt:header footer

This commit is contained in:
Duong Truong Phong
2024-05-30 22:57:45 +07:00
parent dd09434deb
commit 3b435e22ea
18 changed files with 787 additions and 467 deletions
+73
View File
@@ -0,0 +1,73 @@
import type { Category } from "~/server/models/category";
export const useCategoryStore = defineStore("category-v2", () => {
const categories = ref<Category[]>([]);
async function fetchCategories() {
const { data, error } = await useFetch<Category[]>("/api/v2/categories");
if (error.value) {
return [] as Category[];
}
categories.value = Object.assign([], data.value);
return categories.value;
}
function findByCode(code?: string) {
if (code) return categories.value.find((c) => c.code === code);
}
function findById(id?: number) {
return categories.value.find((c) => c.id === id);
}
function findParents(category?: Category) {
if (!category) return [];
const parents = [];
let parent = findById(category.parentId);
while (parent) {
parents.push(parent);
parent = findById(parent.parentId);
}
return parents.reverse().concat(category);
}
function findSubTree(category?: Category) {
if (!category) return [];
let subTree = [] as Category[];
function findChildren(category: Category) {
const children = categories.value.filter((c:Category) => c.parentId === category.id);
if (children.length === 0) return;
subTree.push(...children,category);
}
if(category.parentId === 41){
findChildren(category);
}else{
const parent = findById(category.parentId);
if(parent){
findChildren(parent);
}
}
return subTree.reverse();
}
function findChildren(category: Category) {
const children = categories.value.filter((c:Category) => c.parentId === category.id);
if (children.length === 0) return;
else return [...children]
}
return { categories, fetchCategories, findByCode, findById, findParents,findSubTree, findChildren };
});
if (import.meta.hot) {
import.meta.hot.accept(acceptHMRUpdate(useCategoryStore, import.meta.hot));
}