refactor: replace hardcoded categories with Directus-powered category tree and translations

This commit is contained in:
2026-02-07 11:23:39 +01:00
parent 4f00b303e8
commit bb50615e0a
12 changed files with 391 additions and 196 deletions

View File

@@ -10,6 +10,7 @@ class CategoriesService {
this.cache = null
this.cacheTimestamp = 0
this.cacheTimeout = 10 * 60 * 1000 // 10 minutes
this._pending = null
}
async getAll() {
@@ -17,10 +18,19 @@ class CategoriesService {
return this.cache
}
const categories = await directus.getCategories()
this.cache = categories
this.cacheTimestamp = Date.now()
return categories
if (this._pending) return this._pending
this._pending = directus.getCategories().then(categories => {
this.cache = categories
this.cacheTimestamp = Date.now()
this._pending = null
return categories
}).catch(err => {
this._pending = null
throw err
})
return this._pending
}
async getById(id) {
@@ -32,7 +42,17 @@ class CategoriesService {
}
async getTree() {
return directus.getCategoryTree()
const all = await this.getAll()
return this.buildTree(all)
}
buildTree(categories, parentId = null) {
return categories
.filter(cat => (cat.parent?.id || cat.parent) === parentId)
.map(cat => ({
...cat,
children: this.buildTree(categories, cat.id)
}))
}
async getSubcategories(parentId) {