From 3771de9d24b97b8f5b5b630c9806db0d5a822d0e Mon Sep 17 00:00:00 2001 From: kotva006 Date: Mon, 20 Nov 2023 09:54:20 -0600 Subject: [PATCH] feat(TripPlanner): Use Stop ID to query OTP --- .../js/trip-planner-location-controls.js | 19 +++++++++++++++++-- apps/site/lib/site/trip_plan/location.ex | 4 +++- .../partial/_trip_planner_widget.html.eex | 4 ++-- .../trip_plan/_to_from_inputs.html.eex | 2 ++ .../lib/trip_plan/api/open_trip_planner.ex | 4 ++-- .../api/open_trip_planner/builder.ex | 4 ++++ 6 files changed, 30 insertions(+), 7 deletions(-) diff --git a/apps/site/assets/js/trip-planner-location-controls.js b/apps/site/assets/js/trip-planner-location-controls.js index 9b84debf6e..10ff328f2c 100644 --- a/apps/site/assets/js/trip-planner-location-controls.js +++ b/apps/site/assets/js/trip-planner-location-controls.js @@ -22,6 +22,10 @@ export class TripPlannerLocControls { this.toLng = this.getById(TripPlannerLocControls.SELECTORS.to.lng); this.fromLat = this.getById(TripPlannerLocControls.SELECTORS.from.lat); this.fromLng = this.getById(TripPlannerLocControls.SELECTORS.from.lng); + this.fromStopId = this.getById( + TripPlannerLocControls.SELECTORS.from.stop_id + ); + this.toStopId = this.getById(TripPlannerLocControls.SELECTORS.to.stop_id); this.controller = null; this.toInputDirty = false; this.fromInputDirty = false; @@ -315,6 +319,7 @@ export class TripPlannerLocControls { }) => { switch (type) { case "stops": + this.setStopValue(ac, hit); this.setAutocompleteValue( ac, hit.stop.name, @@ -342,6 +347,7 @@ export class TripPlannerLocControls { ac.useMyLocationSearch(); break; case "popular": + this.setStopValue(ac, hit); this.setAutocompleteValue( ac, hit.name, @@ -357,6 +363,13 @@ export class TripPlannerLocControls { }; } + setStopValue(ac, hit) { + if (hit.stop?.id) { + const stopIdEl = this.getById(ac._selectors.stop_id); + stopIdEl.value = hit.stop.id; + } + } + setAutocompleteValue(ac, name, latEl, lngEl, lat, lng) { ac.setValue(name); latEl.value = lat; @@ -429,7 +442,8 @@ TripPlannerLocControls.SELECTORS = { locationLoadingIndicator: "trip-plan__loading-indicator--to", required: "trip-plan__required--to", locationError: "trip-plan__location-error--to", - announcer: "trip-plan__announcer--to" + announcer: "trip-plan__announcer--to", + stop_id: "to_stop_id" }, from: { input: "from", @@ -440,7 +454,8 @@ TripPlannerLocControls.SELECTORS = { locationLoadingIndicator: "trip-plan__loading-indicator--from", required: "trip-plan__required--from", locationError: "trip-plan__location-error--from", - announcer: "trip-plan__announcer--from" + announcer: "trip-plan__announcer--from", + stop_id: "from_stop_id" }, map: "trip-plan-map--initial" }; diff --git a/apps/site/lib/site/trip_plan/location.ex b/apps/site/lib/site/trip_plan/location.ex index 6cbd39b491..e33897e134 100644 --- a/apps/site/lib/site/trip_plan/location.ex +++ b/apps/site/lib/site/trip_plan/location.ex @@ -68,6 +68,7 @@ defmodule Site.TripPlan.Location do field = Atom.to_string(field_atom) {lat_bin, params} = Map.pop(params, field <> "_latitude") {lng_bin, params} = Map.pop(params, field <> "_longitude") + {stop_id, params} = Map.pop(params, field <> "_stop_id") with {lat, ""} <- Float.parse(lat_bin), {lng, ""} <- Float.parse(lng_bin) do @@ -76,7 +77,8 @@ defmodule Site.TripPlan.Location do position = %NamedPosition{ latitude: lat, longitude: lng, - name: encode_name(name) + name: encode_name(name), + stop_id: stop_id } query diff --git a/apps/site/lib/site_web/templates/partial/_trip_planner_widget.html.eex b/apps/site/lib/site_web/templates/partial/_trip_planner_widget.html.eex index de03e85f58..207676d02f 100644 --- a/apps/site/lib/site_web/templates/partial/_trip_planner_widget.html.eex +++ b/apps/site/lib/site_web/templates/partial/_trip_planner_widget.html.eex @@ -8,8 +8,8 @@ from_error = "" to_error = "" - from_position = %{name: "", latitude: "", longitude: ""} - to_position = %{name: "", latitude: "", longitude: ""} + from_position = %{name: "", latitude: "", longitude: "", stop_id: ""} + to_position = %{name: "", latitude: "", longitude: "", stop_id: ""} %>
diff --git a/apps/site/lib/site_web/templates/trip_plan/_to_from_inputs.html.eex b/apps/site/lib/site_web/templates/trip_plan/_to_from_inputs.html.eex index c84f57f3a0..62f71313c8 100644 --- a/apps/site/lib/site_web/templates/trip_plan/_to_from_inputs.html.eex +++ b/apps/site/lib/site_web/templates/trip_plan/_to_from_inputs.html.eex @@ -24,6 +24,7 @@ > +
@@ -58,6 +59,7 @@ > +
diff --git a/apps/trip_plan/lib/trip_plan/api/open_trip_planner.ex b/apps/trip_plan/lib/trip_plan/api/open_trip_planner.ex index 794a38d32e..f0b07ed345 100644 --- a/apps/trip_plan/lib/trip_plan/api/open_trip_planner.ex +++ b/apps/trip_plan/lib/trip_plan/api/open_trip_planner.ex @@ -16,7 +16,7 @@ defmodule TripPlan.Api.OpenTripPlanner do with {:ok, params} <- build_params(from, to, opts) do param_string = Enum.map_join(params, "\n", fn {key, val} -> ~s{#{key}: #{val}} end) - graph_ql_query = """ + graphql_query = """ { plan( #{param_string} @@ -28,7 +28,7 @@ defmodule TripPlan.Api.OpenTripPlanner do root_url = Keyword.get(opts, :root_url, nil) || pick_url(connection_opts) graphql_url = "#{root_url}/otp/routers/default/index/" - send_request(graphql_url, graph_ql_query, accessible?, &parse_ql/2) + send_request(graphql_url, graphql_query, accessible?, &parse_ql/2) end end diff --git a/apps/trip_plan/lib/trip_plan/api/open_trip_planner/builder.ex b/apps/trip_plan/lib/trip_plan/api/open_trip_planner/builder.ex index 34e2e4ff7b..350c59baa1 100644 --- a/apps/trip_plan/lib/trip_plan/api/open_trip_planner/builder.ex +++ b/apps/trip_plan/lib/trip_plan/api/open_trip_planner/builder.ex @@ -20,6 +20,10 @@ defmodule TripPlan.Api.OpenTripPlanner.Builder do }) end + defp location(%NamedPosition{stop_id: stop_id} = np) when not is_nil(stop_id) do + "\"#{np.name}::mbta-ma-us:#{stop_id}\"" + end + defp location(%NamedPosition{} = np) do "\"#{np.name}::#{Position.latitude(np)},#{Position.longitude(np)}\"" end