From d68a34e591a61dbc03acf0e37dbc7ffc9488b377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20M=C3=A9taireau?= Date: Wed, 23 Oct 2024 12:37:55 +0200 Subject: [PATCH] fixup! Introduce a _future_uuid property to track not-yet-synced datalayers --- umap/static/umap/js/modules/data/layer.js | 33 +++++++++++--------- umap/static/umap/js/modules/schema.js | 5 +++ umap/static/umap/js/modules/sync/updaters.js | 13 ++++---- umap/static/umap/js/umap.js | 12 ++++--- 4 files changed, 39 insertions(+), 24 deletions(-) diff --git a/umap/static/umap/js/modules/data/layer.js b/umap/static/umap/js/modules/data/layer.js index 3dc07ad6..b4f1a34b 100644 --- a/umap/static/umap/js/modules/data/layer.js +++ b/umap/static/umap/js/modules/data/layer.js @@ -36,7 +36,7 @@ const LAYER_MAP = LAYER_TYPES.reduce((acc, klass) => { }, {}) export class DataLayer { - constructor(map, data, sync) { + constructor(map, data, sync, future_uuid) { this.map = map this.sync = map.sync_engine.proxy(this) this._index = Array() @@ -61,6 +61,13 @@ export class DataLayer { this._isDirty = false this._isDeleted = false this.setUmapId(data.id) + + if (!this.umap_id) { + // Generate a random uuid if none is provided + this._future_uuid = future_uuid !== undefined ? future_uuid : crypto.randomUUID() + console.log('Future UUID for datalayer', this._future_uuid) + } + this.setOptions(data) if (!Utils.isObject(this.options.remoteData)) { @@ -79,8 +86,9 @@ export class DataLayer { this.connectToMap() this.permissions = new DataLayerPermissions(this) - if (!this.createdOnServer) { - // When importing data, show the layer immediately if applicable + console.debug("createdOnServer", this.createdOnServer) + if (!this.umap_id) { + console.debug("showAtLoad", this.showAtLoad()) if (this.showAtLoad()) this.show() this.isDirty = true } @@ -92,8 +100,7 @@ export class DataLayer { } get createdOnServer(){ - console.log("reference version", this._reference_version) - return this._reference_version !== undefined + return this.umap_id !== undefined } set isDirty(status) { @@ -127,6 +134,7 @@ export class DataLayer { subject: 'datalayer', metadata: { id: this.umap_id, + future_uuid: this._future_uuid, }, } } @@ -219,13 +227,14 @@ export class DataLayer { } async fetchData() { - if (!this.createdOnServer) return + console.trace("fetchData", this.umap_id) + if (!this.umap_id) return if (this._loading) return this._loading = true const [geojson, response, error] = await this.map.server.get(this._dataUrl()) if (!error) { this._reference_version = response.headers.get('X-Datalayer-Version') - // FIXME: for now this property is set dynamically from backend + // FIXME: for now the _umap_options property is set dynamically from backend // And thus it's not in the geojson file in the server // So do not let all options to be reset // Fix is a proper migration so all datalayers settings are @@ -311,7 +320,7 @@ export class DataLayer { } isLoaded() { - return !this.createdOnServer || this._loaded + return !this.umap_id || this._loaded } hasDataLoaded() { @@ -321,11 +330,6 @@ export class DataLayer { setUmapId(id) { // Datalayer ID is null when listening creation form if (!this.umap_id && id) this.umap_id = id - else { - // Generate a random uuid if none is provided - this.umap_id = crypto.randomUUID() - console.log('Generating random UUID for datalayer', this.umap_id) - } } backupOptions() { @@ -909,6 +913,7 @@ export class DataLayer { async show() { this.map.addLayer(this.layer) + if (!this.isLoaded()) await this.fetchData() this.propagateShow() } @@ -1054,7 +1059,7 @@ export class DataLayer { formData.append('geojson', blob) const saveURL = this.map.urls.get('datalayer_save', { map_id: this.map.options.umap_id, - pk: this.umap_id, + pk: this.umap_id || this._future_uuid, created: this.createdOnServer, }) console.log("saveUrl", saveURL) diff --git a/umap/static/umap/js/modules/schema.js b/umap/static/umap/js/modules/schema.js index f2dab903..e4a93d40 100644 --- a/umap/static/umap/js/modules/schema.js +++ b/umap/static/umap/js/modules/schema.js @@ -563,4 +563,9 @@ export const SCHEMA = { type: Object, impacts: ['data'], }, + + _reference_version: { + type: Number, + impacts: ['data'], + }, } diff --git a/umap/static/umap/js/modules/sync/updaters.js b/umap/static/umap/js/modules/sync/updaters.js index 77992acc..ac7740a0 100644 --- a/umap/static/umap/js/modules/sync/updaters.js +++ b/umap/static/umap/js/modules/sync/updaters.js @@ -31,8 +31,8 @@ class BaseUpdater { } } - getDataLayerFromID(layerId) { - if (layerId) return this.map.getDataLayerByUmapId(layerId) + getDataLayerFromID(layerId, future_uuid) { + if (layerId) return this.map.getDataLayerByUmapId(layerId, future_uuid) return this.map.defaultEditDataLayer() } @@ -53,14 +53,15 @@ export class MapUpdater extends BaseUpdater { } export class DataLayerUpdater extends BaseUpdater { - upsert({ value }) { - // Inserts does not happen (we use multiple updates instead). - this.map.createDataLayer(value, false) + upsert({ value, metadata }) { + console.log('upsert', value, metadata) + // Upsert only happens when a new datalayer is created. + this.map.createDataLayer(value, false, metadata.future_uuid) this.map.render([]) } update({ key, metadata, value }) { - const datalayer = this.getDataLayerFromID(metadata.id) + const datalayer = this.getDataLayerFromID(metadata.id, metadata.future_uuid) if (fieldInSchema(key)) { this.updateObjectValue(datalayer, key, value) } else { diff --git a/umap/static/umap/js/umap.js b/umap/static/umap/js/umap.js index 11305e96..01b7319e 100644 --- a/umap/static/umap/js/umap.js +++ b/umap/static/umap/js/umap.js @@ -817,9 +817,10 @@ U.Map = L.Map.extend({ return L.Map.prototype.setMaxBounds.call(this, bounds) }, - createDataLayer: function (options = {}, sync = true) { + createDataLayer: function (options = {}, sync = true, future_uuid = undefined) { + console.log("Create Datalayer", options) options.name = options.name || `${L._('Layer')} ${this.datalayers_index.length + 1}` - const datalayer = new U.DataLayer(this, options, sync) + const datalayer = new U.DataLayer(this, options, sync, future_uuid) if (sync !== false) { datalayer.sync.upsert(datalayer.options) @@ -828,7 +829,7 @@ U.Map = L.Map.extend({ }, newDataLayer: function () { - const datalayer = this.createDataLayer({}) + const datalayer = this.createDataLayer({}, sync=true) datalayer.edit() }, @@ -1192,7 +1193,10 @@ U.Map = L.Map.extend({ return this.createDataLayer() }, - getDataLayerByUmapId: function (umap_id) { + getDataLayerByUmapId: function (umap_id, future_uuid) { + if (future_uuid !== undefined) { + return this.findDataLayer((d) => d._future_uuid === future_uuid) + } return this.findDataLayer((d) => d.umap_id === umap_id) },