From 4074c1ef4775f1f45f5de16b1afab77a3d586427 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Wed, 11 Feb 2026 02:32:38 +0000
Subject: [PATCH] =testing upload manager racing condition for updating an image field to an empty string immediately after successfully setting the image

---
 assets/js/concise/CRUD.js |   47 +++++++++++++++++++++++++++++++++++------------
 1 files changed, 35 insertions(+), 12 deletions(-)

diff --git a/assets/js/concise/CRUD.js b/assets/js/concise/CRUD.js
index 7bad8c8..00d33cf 100644
--- a/assets/js/concise/CRUD.js
+++ b/assets/js/concise/CRUD.js
@@ -12,6 +12,7 @@
 		this.error = window.jvbError;
 		this.populate = window.jvbPopulate;
 		this.cache = new window.jvbCache(this.content);
+		this.uploadedFields = new Set(); //tracks which upload fields are currently uploading; so don't send any of these changes to server
 
 		this.activeItem = null;
 		this.isTimeline = false;
@@ -354,17 +355,17 @@
 				if (event === 'sent-to-queue' && data.field) {
 					const fieldName = data.field.config.name;
 					const itemId = data.field.config.itemID;
-					if (itemId && fieldName && this.changes.has(itemId)) {
-						delete this.changes.get(itemId)[fieldName];
-					}
-					// Also clear from IndexedDB store
-					this.changesStore.get(itemId).then(stored => {
-						if (stored && stored[fieldName] !== undefined) {
-							delete stored[fieldName];
-							this.changesStore.save(stored);
+					if (itemId && fieldName) {
+						this.uploadedFields.add(`${itemId}_${fieldName}`);
+						if (this.changes.has(itemId)) {
+							delete this.changes.get(itemId)[fieldName];
 						}
-					});
+					}
 				}
+
+				// if (event === 'upload_complete') {
+				// 	this.uploadedFields.delete(`${data['item_id']}_${data['field']}`);
+				// }
 			});
 		}
 		initModals() {
@@ -822,6 +823,21 @@
 		}
 		this.changes.get(itemId)[name] = value;
 
+		for (const key of this.uploadedFields) {
+			const [itemId, fieldName] = key.split('_');
+			if (this.changes.has(itemId)) {
+				delete this.changes.get(itemId)[fieldName];
+			}
+		}
+
+		// Don't schedule if only base keys remain
+		const change = this.changes.get(itemId);
+		const realKeys = Object.keys(change).filter(k => k !== 'id' && k !== 'content');
+		if (realKeys.length === 0) {
+			this.changes.delete(itemId);
+			return;
+		}
+
 		this.scheduleBackup();
 		//Only send actual itemIds to server. If this is a recently uploaded item, just store changes for now
 		if (typeof itemId === 'number' || !itemId.includes('group')) {
@@ -1258,10 +1274,17 @@
 
 		changes.forEach(change => {
 			let itemId = change.id;
+			const { id, content, ...fields } = change;
 
-			// Create a new object without the id field (don't mutate original!)
-			const { id, ...changeWithoutId } = change;
-			allChanges[itemId] = changeWithoutId;
+			// Filter out uploaded fields
+			for (const key of this.uploadedFields) {
+				const [uid, fieldName] = key.split('_');
+				if (uid === itemId) delete fields[fieldName];
+			}
+
+			if (Object.keys(fields).length > 0) {
+				allChanges[itemId] = { content, ...fields };
+			}
 
 			if (change.post_status && this.shouldRemoveItemUI(change.post_status)) {
 				remove.push(itemId);

--
Gitblit v1.10.0