From d38d825e3484d822ea3c1f0fb1df37ecf386b18a Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Sun, 04 Jan 2026 19:54:16 +0000
Subject: [PATCH] =TaxonomyCreator.js debugging

---
 assets/js/concise/TaxonomyCreator.js |   70 ++++++++++++----------------------
 1 files changed, 25 insertions(+), 45 deletions(-)

diff --git a/assets/js/concise/TaxonomyCreator.js b/assets/js/concise/TaxonomyCreator.js
index 91ed836..3c5f24d 100644
--- a/assets/js/concise/TaxonomyCreator.js
+++ b/assets/js/concise/TaxonomyCreator.js
@@ -37,12 +37,12 @@
 		}
 
 		if (window.targetCheck(e, '.submit-term')) {
-			this.handleTermCreation(e);
+			this.handleTermCreation(e).then(()=>{});
 		}
 
 		// Handle autocomplete create button
 		if (window.targetCheck(e, '.create-term')) {
-			this.handleAutocompleteCreate(e);
+			this.handleAutocompleteCreate(e).then(()=>{});
 		}
 	}
 
@@ -56,28 +56,31 @@
 		if (!termName) return;
 
 		try {
-			this.form.querySelector('button').disabled = true;
+			const submitButton = this.form.querySelector('button');
+			if (submitButton) {
+				submitButton.disabled = true;
+			}
 			const response = await this.createTerm(termName, parentId, taxonomy);
 
 			if (response.success && response.term) {
 				let term = response.term;
+				const termPath = term.path || term.name;
 
 				this.createNew.open = false;
 				await this.selector.store.clearCache();
 
-				// Add to store's data BEFORE display update
 				this.selector.store.data.set(term.id, {
 					id: term.id,
 					name: term.name,
 					path: termPath,
-					taxonomy: field.taxonomy,
-					parent: 0,
+					taxonomy: taxonomy,
+					parent: parentId,
 					count: 0,
 					hasChildren: false,
 					slug: term.slug || termName.toLowerCase().replace(/\s+/g, '-')
 				});
 
-				this.selector.addSelectedTermToModal(term.id, term.name, term.path || term.name);
+				this.selector.addSelectedTermToModal(term.id, term.name, termPath);
 
 				const currentParent = this.selector.store.filters.parent || 0;
 				if (currentParent === parentId) {
@@ -94,7 +97,7 @@
 				if (suggestionContainer) {
 					suggestionContainer.hidden = true;
 				}
-				this.selector.store.cache.clear();
+
 			}
 		} catch (error) {
 			console.error('Error creating term:', error);
@@ -132,7 +135,8 @@
 				const termPath = term.path || term.name;
 
 				field.selectedTerms.add(parseInt(term.id));
-				// Add to store's data BEFORE display update
+
+				// Add to store's data map
 				this.selector.store.data.set(term.id, {
 					id: term.id,
 					name: term.name,
@@ -143,6 +147,7 @@
 					hasChildren: false,
 					slug: term.slug || termName.toLowerCase().replace(/\s+/g, '-')
 				});
+
 				this.selector.addTermToDisplay(field.id, term.id, term.name, termPath);
 
 				field.input.value = Array.from(field.selectedTerms).join(',');
@@ -151,15 +156,11 @@
 				field.autocompleteDropdown.hidden = true;
 				if (input) input.value = '';
 
-				this.selector.store.clearCache();
-				await this.selector.store.setFilters({
-					taxonomy: field.taxonomy,
-					page: 1,
-					search: '',
-					parent: 0
-				});
+				// Clear ALL cache for this taxonomy
+				// This forces next search to hit the server
+				await this.selector.store.clearCache();
+
 			} else if (response.reason === 'exists' && response.term) {
-				// Term already exists - just add it
 				const term = response.term;
 				field.selectedTerms.add(parseInt(term.id));
 				this.selector.addTermToDisplay(field.id, term.id, term.name, term.path || term.name);
@@ -172,12 +173,13 @@
 			}
 		} catch (error) {
 			console.error('Error creating term:', error);
-			button.innerHTML = originalHTML;
-			button.disabled = false;
 			this.selector.error?.log(error, {
 				component: 'TaxonomyCreator',
 				action: 'handleAutocompleteCreate'
 			});
+		} finally {
+			button.innerHTML = originalHTML;
+			button.disabled = false;
 		}
 	}
 
@@ -243,7 +245,7 @@
 
 	async createTerm(name, parent = 0, taxonomy) {
 		try {
-			// Search for the exact term first
+			// Search to ensure we have latest data for duplicate check
 			await this.selector.store.setFilters({
 				taxonomy: taxonomy,
 				search: name,
@@ -251,6 +253,9 @@
 				parent: 0
 			});
 
+			// Wait a bit for the data to load
+			await new Promise(resolve => setTimeout(resolve, 100));
+
 			// Check if exact match exists in results
 			const exactMatch = Array.from(this.selector.store.data.values())
 				.find(term =>
@@ -293,31 +298,6 @@
 	}
 
 	/**
-	 * Search for existing terms using the store
-	 */
-	async searchExistingTerms(searchQuery, taxonomy) {
-		return new Promise((resolve) => {
-			// Set up a one-time listener for the search results
-			const handleSearchResults = (event, data) => {
-				if (event === 'data-loaded') {
-					this.selector.store.unsubscribe(handleSearchResults);
-					resolve(data.data?.items || []);
-				}
-			};
-
-			this.selector.store.subscribe(handleSearchResults);
-
-			// Trigger search
-			this.selector.store.setFilters({
-				taxonomy: taxonomy,
-				search: searchQuery,
-				page: 1,
-				parent: 0
-			});
-		});
-	}
-
-	/**
 	 * Show term suggestions when similar terms exist
 	 */
 	showTermSuggestions(suggestions, isExact = false) {

--
Gitblit v1.10.0