Skip to content

Commit

Permalink
feat(TripPlanner): Use Stop ID to query OTP
Browse files Browse the repository at this point in the history
  • Loading branch information
kotva006 committed Nov 20, 2023
1 parent cf626b6 commit 3771de9
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
19 changes: 17 additions & 2 deletions apps/site/assets/js/trip-planner-location-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -315,6 +319,7 @@ export class TripPlannerLocControls {
}) => {
switch (type) {
case "stops":
this.setStopValue(ac, hit);
this.setAutocompleteValue(
ac,
hit.stop.name,
Expand Down Expand Up @@ -342,6 +347,7 @@ export class TripPlannerLocControls {
ac.useMyLocationSearch();
break;
case "popular":
this.setStopValue(ac, hit);
this.setAutocompleteValue(
ac,
hit.name,
Expand All @@ -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;
Expand Down Expand Up @@ -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",
Expand All @@ -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"
};
Expand Down
4 changes: 3 additions & 1 deletion apps/site/lib/site/trip_plan/location.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: ""}
%>
<div class="c-trip-plan-widget">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
></input>
<input id="from_latitude" name="plan[from_latitude]" type="hidden" value="<%= @from_position.latitude %>">
<input id="from_longitude" name="plan[from_longitude]" type="hidden" value="<%= @from_position.longitude %>">
<input id="from_stop_id" name="plan[from_stop_id]" type="hidden" value="<%= @from_position.stop_id %>">
<i aria-hidden="true" id="trip-plan__loading-indicator--from" class="fa fa-cog fa-spin c-search-bar__loading-indicator"></i>
<i id="trip-plan__reset--from" class="c-form__reset-icon fa fa-times-circle"></i>
<div id="trip-plan__announcer--from" aria-live="polite" class="sr-only"></div>
Expand Down Expand Up @@ -58,6 +59,7 @@
></input>
<input id="to_latitude" name="plan[to_latitude]" type="hidden" value="<%= @to_position.latitude %>">
<input id="to_longitude" name="plan[to_longitude]" type="hidden" value="<%= @to_position.longitude %>">
<input id="to_stop_id" name="plan[to_stop_id]" type="hidden" value="<%= @to_position.stop_id %>">
<i aria-hidden="true" id="trip-plan__loading-indicator--to" class="fa fa-cog fa-spin c-search-bar__loading-indicator"></i>
<i id="trip-plan__reset--to" class="c-form__reset-icon fa fa-times-circle"></i>
<div id="trip-plan__announcer--to" aria-live="polite" class="sr-only"></div>
Expand Down
4 changes: 2 additions & 2 deletions apps/trip_plan/lib/trip_plan/api/open_trip_planner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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

Expand Down
4 changes: 4 additions & 0 deletions apps/trip_plan/lib/trip_plan/api/open_trip_planner/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3771de9

Please sign in to comment.