thainv-dev: tạo lại cấu trúc folder và làm UI
This commit is contained in:
+92
-62
@@ -1,72 +1,102 @@
|
||||
import type { Category } from "~/server/models/category";
|
||||
import type { Category, CategoryTree } from "~/server/models/category";
|
||||
|
||||
export const useCategoryStore = defineStore("category-v2", () => {
|
||||
const categories = ref<Category[]>([]);
|
||||
// export const useCategoryStore = defineStore("category-v2", () => {
|
||||
// const categories = ref<Category[]>([]);
|
||||
// const categoryTree = ref<any[]>([]);
|
||||
// const currentCategoryTree = ref<any[]>([])
|
||||
|
||||
async function fetchCategories() {
|
||||
const { data, error } = await useFetch<Category[]>("/api/v2/categories");
|
||||
if (error.value) {
|
||||
return [] as Category[];
|
||||
}
|
||||
// const findBySiteId = async () => {
|
||||
// try {
|
||||
// const { data } = await useFetch(`/api/services/category-tree`)
|
||||
// categoryTree.value = data.value
|
||||
// return categoryTree.value
|
||||
// } catch (error) {}
|
||||
// }
|
||||
|
||||
categories.value = Object.assign([], data.value);
|
||||
// async function fetchCategories() {
|
||||
// const { data, error } = await useFetch<Category[]>("/api/v2/categories");
|
||||
// if (error.value) {
|
||||
// return [] as Category[];
|
||||
// }
|
||||
|
||||
return categories.value;
|
||||
// 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, categoryTree, currentCategoryTree, findBySiteId, fetchCategories, findByCode, findById, findParents,findSubTree, findChildren };
|
||||
// });
|
||||
|
||||
export const useCategoryStore = defineStore('usecategorystore', () => {
|
||||
const categoryTree = shallowRef<CategoryTree[]>([])
|
||||
const currentCategoryTree = shallowRef<any[]>([])
|
||||
|
||||
async function fetchBySiteId() {
|
||||
const { data }: any = await useFetch(`/api/services/category-tree`)
|
||||
categoryTree.value = data.value.items
|
||||
return categoryTree.value
|
||||
}
|
||||
|
||||
function findByCode(code?: string) {
|
||||
if (code) return categories.value.find((c) => c.code === code);
|
||||
return {
|
||||
categoryTree,
|
||||
currentCategoryTree,
|
||||
|
||||
fetchBySiteId
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
Reference in New Issue
Block a user