Skip to content

Commit

Permalink
chore: review save strategy
Browse files Browse the repository at this point in the history
All is now orchestrated from a single method, instead of chaining
calls.
  • Loading branch information
yohanboniface committed Oct 4, 2024
1 parent 73c83cf commit 0fdb70c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
3 changes: 1 addition & 2 deletions umap/static/umap/js/modules/data/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -1091,7 +1091,6 @@ export class DataLayer {
await this.map.server.post(this.getDeleteUrl())
}
this.isDirty = false
this.map.continueSaving()
}

getMap() {
Expand Down
7 changes: 3 additions & 4 deletions umap/static/umap/js/modules/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -205,7 +205,6 @@ export class MapPermissions {
if (!error) {
this.commit()
this.isDirty = false
this.map.continueSaving()
this.map.fire('postsync')
}
}
Expand Down Expand Up @@ -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(
Expand All @@ -300,7 +300,6 @@ export class DataLayerPermissions {
if (!error) {
this.commit()
this.isDirty = false
this.datalayer.map.continueSaving()
}
}

Expand Down
28 changes: 13 additions & 15 deletions umap/static/umap/js/umap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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
}
Expand All @@ -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!'),
Expand Down Expand Up @@ -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 () {
Expand Down

1 comment on commit 0fdb70c

@almet
Copy link
Member

@almet almet commented on 0fdb70c Oct 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work on this 🙌🏼 🙏🏼

Please sign in to comment.