refactor: replace hardcoded categories with Directus-powered category tree and translations
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user