chore: review save strategy

All is now orchestrated from a single method, instead of chaining
calls.
This commit is contained in:
Yohan Boniface 2024-10-04 10:11:34 +02:00
parent 73c83cfa86
commit 0fdb70ce66
3 changed files with 17 additions and 21 deletions

View file

@ -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() {

View file

@ -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()
}
}

View file

@ -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 () {