+
-
@@ -696,15 +701,16 @@ class SearchBox extends HTMLElement {
if (filters.useCurrentLocation !== undefined) this.useCurrentLocation = filters.useCurrentLocation
this.saveFiltersToStorage()
- this.render()
- this.setupEventListeners()
+ if (this.categoryTree && this.categoryTree.length > 0) {
+ this.render()
+ this.setupEventListeners()
+ }
}
clearFilters() {
this.resetFilters()
localStorage.removeItem('searchFilters')
- this.render()
- this.setupEventListeners()
+ this.loadAndRender()
}
}
@@ -1174,4 +1180,4 @@ style.textContent = /* css */`
`
document.head.appendChild(style)
-export { SearchBox, CATEGORIES, COUNTRIES, RADIUS_OPTIONS }
+export { SearchBox, COUNTRIES, RADIUS_OPTIONS }
diff --git a/js/services/categories.js b/js/services/categories.js
index bc3fb66..64642f8 100644
--- a/js/services/categories.js
+++ b/js/services/categories.js
@@ -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) {
diff --git a/js/services/directus.js b/js/services/directus.js
index 0ae380e..72e4a3f 100644
--- a/js/services/directus.js
+++ b/js/services/directus.js
@@ -134,7 +134,7 @@ class DirectusService {
* @returns {Promise