From 0fdb70ce669838f9fd66eb5de570ffde78e3653d Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Fri, 4 Oct 2024 10:11:34 +0200 Subject: [PATCH] chore: review save strategy All is now orchestrated from a single method, instead of chaining calls. --- umap/static/umap/js/modules/data/layer.js | 3 +-- umap/static/umap/js/modules/permissions.js | 7 +++--- umap/static/umap/js/umap.js | 28 ++++++++++------------ 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/umap/static/umap/js/modules/data/layer.js b/umap/static/umap/js/modules/data/layer.js index 5668b2f5..f02c73c5 100644 --- a/umap/static/umap/js/modules/data/layer.js +++ b/umap/static/umap/js/modules/data/layer.js @@ -1026,7 +1026,7 @@ export class DataLayer { } async save() { - if (this.isDeleted) return this.saveDelete() + if (this.isDeleted) return await this.saveDelete() if (!this.isLoaded()) { return } @@ -1091,7 +1091,6 @@ export class DataLayer { await this.map.server.post(this.getDeleteUrl()) } this.isDirty = false - this.map.continueSaving() } getMap() { diff --git a/umap/static/umap/js/modules/permissions.js b/umap/static/umap/js/modules/permissions.js index f66de593..70c25301 100644 --- a/umap/static/umap/js/modules/permissions.js +++ b/umap/static/umap/js/modules/permissions.js @@ -182,7 +182,7 @@ export class MapPermissions { } async save() { - if (!this.isDirty) return this.map.continueSaving() + if (!this.isDirty) return const formData = new FormData() if (!this.isAnonymousMap() && this.options.editors) { const editors = this.options.editors.map((u) => u.id) @@ -205,7 +205,6 @@ export class MapPermissions { if (!error) { this.commit() this.isDirty = false - this.map.continueSaving() this.map.fire('postsync') } } @@ -288,8 +287,9 @@ export class DataLayerPermissions { pk: this.datalayer.umap_id, }) } + async save() { - if (!this.isDirty) return this.datalayer.map.continueSaving() + if (!this.isDirty) return const formData = new FormData() formData.append('edit_status', this.options.edit_status) const [data, response, error] = await this.datalayer.map.server.post( @@ -300,7 +300,6 @@ export class DataLayerPermissions { if (!error) { this.commit() this.isDirty = false - this.datalayer.map.continueSaving() } } diff --git a/umap/static/umap/js/umap.js b/umap/static/umap/js/umap.js index 2faaa658..516b5489 100644 --- a/umap/static/umap/js/umap.js +++ b/umap/static/umap/js/umap.js @@ -1025,11 +1025,6 @@ U.Map = L.Map.extend({ } }, - continueSaving: function () { - if (this.dirty_datalayers.length) this.dirty_datalayers[0].save() - else this.fire('saved') - }, - exportOptions: function () { const properties = {} for (const option of Object.keys(U.SCHEMA)) { @@ -1059,7 +1054,7 @@ U.Map = L.Map.extend({ return } if (data.login_required) { - window.onLogin = () => this.saveSelf() + window.onLogin = () => this.save() window.open(data.login_required) return } @@ -1069,7 +1064,7 @@ U.Map = L.Map.extend({ this.options.umap_id = data.id this.permissions.setOptions(data.permissions) this.permissions.commit() - if (data?.permissions?.anonymous_edit_url) { + if (data.permissions?.anonymous_edit_url) { this.once('saved', () => { U.AlertCreation.info( L._('Your map has been created with an anonymous account!'), @@ -1102,22 +1097,25 @@ U.Map = L.Map.extend({ } else { window.location = data.url } - this.permissions.save() + return true }, - save: function () { + save: async function () { if (!this.isDirty) return if (this._default_extent) this._setCenterAndZoom() this.backup() - this.once('saved', () => { - this.isDirty = false - }) if (this.options.editMode === 'advanced') { // Only save the map if the user has the rights to do so. - this.saveSelf() - } else { - this.permissions.save() + const ok = await this.saveSelf() + if (!ok) return } + await this.permissions.save() + for (const datalayer of this.dirty_datalayers) { + await datalayer.save() + } + this.isDirty = false + this.renderEditToolbar() + this.fire('saved') }, star: async function () {