From c26783187b411490b80327c82a6fb557648d4b18 Mon Sep 17 00:00:00 2001 From: pietervdvn Date: Tue, 13 Jul 2021 00:40:27 +0200 Subject: [PATCH] Attempt to fix #422 --- Logic/Osm/Changes.ts | 18 ++++++++--- Logic/Osm/ChangesetHandler.ts | 59 ++++++++++++++++++++++------------- Logic/Osm/OsmConnection.ts | 5 +-- Models/Constants.ts | 2 +- 4 files changed, 55 insertions(+), 29 deletions(-) diff --git a/Logic/Osm/Changes.ts b/Logic/Osm/Changes.ts index 39b9f4e02f..2865041748 100644 --- a/Logic/Osm/Changes.ts +++ b/Logic/Osm/Changes.ts @@ -29,10 +29,11 @@ export class Changes implements FeatureSource { /** * All the pending new objects to upload - * @private */ private readonly newObjects = LocalStorageSource.GetParsed<{ id: number, lat: number, lon: number }[]>("newObjects", []) + private readonly isUploading = new UIEventSource(false); + /** * Adds a change to the pending changes */ @@ -190,6 +191,12 @@ export class Changes implements FeatureSource { console.log("No changes in any object"); return; } + const self = this; + + if (this.isUploading.data) { + return; + } + this.isUploading.setData(true) console.log("Beginning upload..."); // At last, we build the changeset and upload @@ -235,9 +242,12 @@ export class Changes implements FeatureSource { }, () => { console.log("Upload successfull!") - this.newObjects.setData([]) - this.pending.setData([]); - }); + self.newObjects.setData([]) + self.pending.setData([]); + self.isUploading.setData(false) + }, + () => self.isUploading.setData(false) + ); }; diff --git a/Logic/Osm/ChangesetHandler.ts b/Logic/Osm/ChangesetHandler.ts index 98d45a790c..8fba438037 100644 --- a/Logic/Osm/ChangesetHandler.ts +++ b/Logic/Osm/ChangesetHandler.ts @@ -27,7 +27,7 @@ export class ChangesetHandler { } } - private static parseUploadChangesetResponse(response: XMLDocument, allElements: ElementStorage) : void{ + private static parseUploadChangesetResponse(response: XMLDocument, allElements: ElementStorage): void { const nodes = response.getElementsByTagName("node"); // @ts-ignore for (const node of nodes) { @@ -70,7 +70,8 @@ export class ChangesetHandler { layout: LayoutConfig, allElements: ElementStorage, generateChangeXML: (csid: string) => string, - whenDone : (csId: string) => void) { + whenDone: (csId: string) => void, + onFail: () => void) { if (this.userDetails.data.csCount == 0) { // The user became a contributor! @@ -98,8 +99,11 @@ export class ChangesetHandler { whenDone, (e) => { console.error("UPLOADING FAILED!", e) + onFail() } ) + }, { + onFail: onFail }) } else { // There still exists an open changeset (or at least we hope so) @@ -114,8 +118,7 @@ export class ChangesetHandler { // Mark the CS as closed... this.currentChangeset.setData(""); // ... and try again. As the cs is closed, no recursive loop can exist - self.UploadChangeset(layout, allElements, generateChangeXML, whenDone); - + self.UploadChangeset(layout, allElements, generateChangeXML, whenDone, onFail); } ) @@ -161,18 +164,22 @@ export class ChangesetHandler { const self = this; this.OpenChangeset(layout, (csId: string) => { - // The cs is open - let us actually upload! - const changes = generateChangeXML(csId) + // The cs is open - let us actually upload! + const changes = generateChangeXML(csId) - self.AddChange(csId, changes, allElements, (csId) => { - console.log("Successfully deleted ", object.id) - self.CloseChangeset(csId, continuation) - }, (csId) => { - alert("Deletion failed... Should not happend") - // FAILED - self.CloseChangeset(csId, continuation) - }) - }, true, reason) + self.AddChange(csId, changes, allElements, (csId) => { + console.log("Successfully deleted ", object.id) + self.CloseChangeset(csId, continuation) + }, (csId) => { + alert("Deletion failed... Should not happend") + // FAILED + self.CloseChangeset(csId, continuation) + }) + }, { + isDeletionCS: true, + deletionReason: reason + } + ) } private CloseChangeset(changesetId: string = undefined, continuation: (() => void) = () => { @@ -204,15 +211,20 @@ export class ChangesetHandler { private OpenChangeset( layout: LayoutConfig, continuation: (changesetId: string) => void, - isDeletionCS: boolean = false, - deletionReason: string = undefined) { - + options?: { + isDeletionCS?: boolean, + deletionReason?: string, + onFail?: () => void + } + ) { + options = options ?? {} + options.isDeletionCS = options.isDeletionCS ?? false const commentExtra = layout.changesetmessage !== undefined ? " - " + layout.changesetmessage : ""; let comment = `Adding data with #MapComplete for theme #${layout.id}${commentExtra}` - if (isDeletionCS) { + if (options.isDeletionCS) { comment = `Deleting a point with #MapComplete for theme #${layout.id}${commentExtra}` - if (deletionReason) { - comment += ": " + deletionReason; + if (options.deletionReason) { + comment += ": " + options.deletionReason; } } @@ -221,7 +233,7 @@ export class ChangesetHandler { const metadata = [ ["created_by", `MapComplete ${Constants.vNumber}`], ["comment", comment], - ["deletion", isDeletionCS ? "yes" : undefined], + ["deletion", options.isDeletionCS ? "yes" : undefined], ["theme", layout.id], ["language", Locale.language.data], ["host", window.location.host], @@ -244,6 +256,9 @@ export class ChangesetHandler { }, function (err, response) { if (response === undefined) { console.log("err", err); + if(options.onFail){ + options.onFail() + } return; } else { continuation(response); diff --git a/Logic/Osm/OsmConnection.ts b/Logic/Osm/OsmConnection.ts index cba332447f..37c8fa1d2e 100644 --- a/Logic/Osm/OsmConnection.ts +++ b/Logic/Osm/OsmConnection.ts @@ -111,8 +111,9 @@ export class OsmConnection { layout: LayoutConfig, allElements: ElementStorage, generateChangeXML: (csid: string) => string, - whenDone: (csId: string) => void) { - this.changesetHandler.UploadChangeset(layout, allElements, generateChangeXML, whenDone); + whenDone: (csId: string) => void, + onFail: () => {}) { + this.changesetHandler.UploadChangeset(layout, allElements, generateChangeXML, whenDone, onFail); } public GetPreference(key: string, prefix: string = "mapcomplete-"): UIEventSource { diff --git a/Models/Constants.ts b/Models/Constants.ts index 48c52eebfd..e54e0ea412 100644 --- a/Models/Constants.ts +++ b/Models/Constants.ts @@ -2,7 +2,7 @@ import { Utils } from "../Utils"; export default class Constants { - public static vNumber = "0.8.4-rc1"; + public static vNumber = "0.8.4-rc2"; // The user journey states thresholds when a new feature gets unlocked public static userJourney = {