From 4369bb80c05631e19b6c183118859659e7cd948f Mon Sep 17 00:00:00 2001 From: Skylar Ittner Date: Tue, 29 Oct 2019 20:12:22 -0600 Subject: [PATCH] Better behavior when network/geocoding API is slow --- www/assets/js/packages.js | 71 +++++++++++++++++++++++++++------------ 1 file changed, 50 insertions(+), 21 deletions(-) diff --git a/www/assets/js/packages.js b/www/assets/js/packages.js index 6f131ae..16a9360 100644 --- a/www/assets/js/packages.js +++ b/www/assets/js/packages.js @@ -148,7 +148,7 @@ function addPackage(address, latitude, longitude, type, callback) { packageID: packageID }); } - + addAutofillEntry(address); } @@ -234,30 +234,59 @@ function countPackages() { } function addPackageByAddress(address, citystate, type, callback) { - $.getJSON(SETTINGS.geocodeapi, { - address: address + " " + citystate - }, function (resp) { - if (resp.status == "OK") { - if (resp.accuracy.ok) { - addPackage(resp.address.street, resp.coords[0], resp.coords[1], type, callback); + var requestfinished = false; + var searchingdialogopen = false; + $.ajax({ + url: SETTINGS.geocodeapi, + dataType: 'json', + data: { + address: address + " " + citystate + }, + timeout: 15 * 1000, + success: function (resp) { + if (searchingdialogopen) { + app.dialog.close(); + searchingdialogopen = false; + } + requestfinished = true; + if (resp.status == "OK") { + if (resp.accuracy.ok) { + addPackage(resp.address.street, resp.coords[0], resp.coords[1], type, callback); + } else { + playSound("error"); + app.dialog.confirm( + "The address \"" + address + "\" couldn't be reliably located. Add it anyways?", + "Accuracy Warning", + function (ok) { + if (resp.address.street == "") { + addPackage(address, resp.coords[0], resp.coords[1], type, callback); + } else { + addPackage(resp.address.street, resp.coords[0], resp.coords[1], type, callback); + } + } + ); + } } else { playSound("error"); - app.dialog.confirm( - "The address \"" + address + "\" couldn't be reliably located. Add it anyways?", - "Accuracy Warning", - function (ok) { - if (resp.address.street == "") { - addPackage(address, resp.coords[0], resp.coords[1], type, callback); - } else { - addPackage(resp.address.street, resp.coords[0], resp.coords[1], type, callback); - } - } - ); + app.dialog.alert(resp.message, "Error"); } - - } else { + }, + error: function (jqXHR, status, errorThrown) { + if (searchingdialogopen) { + app.dialog.close(); + searchingdialogopen = false; + } + requestfinished = true; playSound("error"); - app.dialog.alert(resp.message, "Error"); + app.dialog.alert("There was a network issue while finding the address. Please try adding the item again.", "Error"); } }); + + // Open a loading message if there's a delay finding the address + setTimeout(function () { + if (!requestfinished) { + app.dialog.preloader("Searching for address..."); + searchingdialogopen = true; + } + }, 750); } \ No newline at end of file