Skip to content

Commit e62910c

Browse files
committed
fix: sort categories
1 parent a476418 commit e62910c

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/model/post-cat.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class PostCat {
1010
childCount = 0
1111
visibleChildCount = 0
1212
parent?: PostCat | null
13-
children?: PostCat | null
13+
children?: PostCat[] | null
1414

1515
flattenParents(includeSelf: boolean): PostCat[] {
1616
// eslint-disable-next-line @typescript-eslint/no-this-alias

src/service/post/post-cat.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { UserService } from '../user.service'
88
// TODO: need better cache impl
99
let siteCategoryCache: SiteCat[] | null = null
1010

11+
const DEFAULT_ORDER = 999999
12+
1113
async function getAuthedPostCatReq() {
1214
const token = await AuthManager.acquireToken()
1315
// TODO: need better solution
@@ -31,17 +33,24 @@ export namespace PostCatService {
3133

3234
export async function getFlatAll() {
3335
const categories = await getAll()
36+
if (categories == null || categories.length === 0) return []
3437

3538
const flat = []
3639
const queue = categories
3740
while (queue.length > 0) {
3841
const current = queue.pop()
42+
if (current == null) continue
3943
flat.push(current)
40-
41-
if (current?.children != null) for (const child of current.children) queue.unshift(child)
44+
if (current.children != null) for (const child of current.children) queue.unshift(child)
4245
}
4346

44-
return flat
47+
return flat.sort((x, y) => {
48+
const order1 = x.order ?? DEFAULT_ORDER
49+
const order2 = y.order ?? DEFAULT_ORDER
50+
if (order1 > order2) return 1
51+
else if (order1 < order2) return -1
52+
else return x.title.localeCompare(y.title)
53+
})
4554
}
4655

4756
export async function getOne(categoryId: number) {

0 commit comments

Comments
 (0)