Skip to content

Commit

Permalink
fix(tripPlanner): Restore error message if from and to are the same
Browse files Browse the repository at this point in the history
  • Loading branch information
joshlarson committed Jan 8, 2025
1 parent 4cdc6e8 commit c332659
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
4 changes: 0 additions & 4 deletions lib/dotcom/trip_plan/input_form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ defmodule Dotcom.TripPlan.InputForm do
alias OpenTripPlannerClient.PlanParams

@error_messages %{
from: "Please specify an origin location.",
to: "Please add a destination.",
from_to_same: "Please select a destination at a different location from the origin.",
modes: "Please select at least one mode of transit.",
datetime: "Please specify a date and time in the future or select 'Now'."
Expand Down Expand Up @@ -68,8 +66,6 @@ defmodule Dotcom.TripPlan.InputForm do
|> cast_embed(:modes, required: true)
|> update_change(:from, &update_location_change/1)
|> update_change(:to, &update_location_change/1)
|> validate_required(:from, message: error_message(:from))
|> validate_required(:to, message: error_message(:to))
|> validate_required(:modes, message: error_message(:modes))
|> validate_required([:datetime_type, :wheelchair])
|> validate_same_locations()
Expand Down
3 changes: 3 additions & 0 deletions lib/dotcom_web/components/trip_planner/input_form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ defmodule DotcomWeb.Components.TripPlanner.InputForm do
name={location_f[subfield].name}
/>
</.inputs_for>
<.error_container :for={{msg, _} <- f[field].errors}>
{msg}
</.error_container>
</.algolia_autocomplete>
</fieldset>
<fieldset class="mb-sm">
Expand Down
11 changes: 1 addition & 10 deletions lib/dotcom_web/live/trip_planner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ defmodule DotcomWeb.Live.TripPlanner do
socket
|> assign(@state)
|> assign(:input_form, Map.put(@state.input_form, :changeset, changeset))
|> maybe_submit_form()
|> submit_changeset(changeset)

{:ok, new_socket}
end
Expand Down Expand Up @@ -317,15 +317,6 @@ defmodule DotcomWeb.Live.TripPlanner do
socket
end

# Check the input form change set for validity and submit the form if it is.
defp maybe_submit_form(socket) do
if socket.assigns.input_form.changeset.valid? do
submit_changeset(socket, socket.assigns.input_form.changeset)
else
socket
end
end

# Round the current time to the nearest 5 minutes.
defp nearest_5_minutes do
datetime = Timex.now("America/New_York")
Expand Down
43 changes: 43 additions & 0 deletions test/dotcom_web/live/trip_planner_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,49 @@ defmodule DotcomWeb.Live.TripPlannerTest do
# end
end

describe "Trip Planner location validations" do
setup %{conn: conn} do
[username: username, password: password] =
Application.get_env(:dotcom, DotcomWeb.Router)[:basic_auth_readonly]

%{
conn:
conn
|> put_req_header("authorization", "Basic " <> Base.encode64("#{username}:#{password}"))
}
end

test "shows error if origin and destination are the same", %{conn: conn} do
latitude = Faker.Address.latitude()
longitude = Faker.Address.longitude()

params = %{
"plan" => %{
"from_latitude" => "#{latitude}",
"from_longitude" => "#{longitude}",
"to_latitude" => "#{latitude}",
"to_longitude" => "#{longitude}"
}
}

{:ok, view, _html} =
conn
|> live(~p"/preview/trip-planner?#{params}")

assert render_async(view) =~
"Please select a destination at a different location from the origin."
end

test "does not show errors if origin or destination are missing", %{conn: conn} do
{:ok, view, _html} =
conn
|> live(~p"/preview/trip-planner")

refute render_async(view) =~ "Please specify an origin location."
refute render_async(view) =~ "Please add a destination."
end
end

describe "Trip Planner with no results" do
setup %{conn: conn} do
[username: username, password: password] =
Expand Down

0 comments on commit c332659

Please sign in to comment.