Skip to content

Commit

Permalink
Use fetch to make overpass requests for the query tool
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhughes committed Feb 12, 2025
1 parent 46f642d commit 1527b18
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
35 changes: 19 additions & 16 deletions app/assets/javascripts/index/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,24 @@ OSM.Query = function (map) {
$ul.empty();
$section.show();

if ($section.data("ajax")) {
$section.data("ajax").abort();
if ($section.data("controller")) {
$section.data("controller").abort();
}

$section.data("ajax", $.ajax({
url: url,
$section.data("controller", new AbortController());

const request = new Request(url, {
method: "POST",
data: {
data: "[timeout:10][out:json];" + query
},
xhrFields: {
withCredentials: credentials
},
success: function (results) {
body: new URLSearchParams({
datax: "[timeout:10][out:json];" + query
}),
credentials: credentials ? "include" : "omit",
signal: $section.data("controller").signal
});

fetch(request)
.then(response => response.json())
.then(results => {
var elements;

$section.find(".loader").hide();
Expand Down Expand Up @@ -221,16 +225,15 @@ OSM.Query = function (map) {
.text(I18n.t("javascripts.query.nothing_found"))
.appendTo($ul);
}
},
error: function (xhr, status, error) {
})
.catch(error => {
$section.find(".loader").hide();

$("<li>")
.addClass("list-group-item")
.text(I18n.t("javascripts.query." + status, { server: url, error: error }))
.text(I18n.t("javascripts.query.error", { server: url, error: error.message }))
.appendTo($ul);
}
}));
});
}

function compareSize(feature1, feature2) {
Expand Down
1 change: 0 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3299,7 +3299,6 @@ en:
relation: Relation
nothing_found: No features found
error: "Error contacting %{server}: %{error}"
timeout: "Timeout contacting %{server}"
context:
directions_from: Directions from here
directions_to: Directions to here
Expand Down

0 comments on commit 1527b18

Please sign in to comment.