diff --git a/src/model/post-cat.ts b/src/model/post-cat.ts index dd4ae1b4..122a02c8 100644 --- a/src/model/post-cat.ts +++ b/src/model/post-cat.ts @@ -10,7 +10,7 @@ export class PostCat { childCount = 0 visibleChildCount = 0 parent?: PostCat | null - children?: PostCat | null + children?: PostCat[] | null flattenParents(includeSelf: boolean): PostCat[] { // eslint-disable-next-line @typescript-eslint/no-this-alias diff --git a/src/service/post/post-cat.ts b/src/service/post/post-cat.ts index 2578d515..236fc449 100644 --- a/src/service/post/post-cat.ts +++ b/src/service/post/post-cat.ts @@ -8,6 +8,8 @@ import { UserService } from '../user.service' // TODO: need better cache impl let siteCategoryCache: SiteCat[] | null = null +const DEFAULT_ORDER = 999999 + async function getAuthedPostCatReq() { const token = await AuthManager.acquireToken() // TODO: need better solution @@ -31,17 +33,24 @@ export namespace PostCatService { export async function getFlatAll() { const categories = await getAll() + if (categories == null || categories.length === 0) return [] const flat = [] const queue = categories while (queue.length > 0) { const current = queue.pop() + if (current == null) continue flat.push(current) - - if (current?.children != null) for (const child of current.children) queue.unshift(child) + if (current.children != null) for (const child of current.children) queue.unshift(child) } - return flat + return flat.sort((x, y) => { + const order1 = x.order ?? DEFAULT_ORDER + const order2 = y.order ?? DEFAULT_ORDER + if (order1 > order2) return 1 + else if (order1 < order2) return -1 + else return x.title.localeCompare(y.title) + }) } export async function getOne(categoryId: number) {