Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/model/post-cat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions src/service/post/post-cat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
Loading