Skip to content

Commit

Permalink
Fixes the nearest five minute bug and mobile datepicker issue (#2351)
Browse files Browse the repository at this point in the history
* fix time rounding issue

* docs and metro update
  • Loading branch information
anthonyshull authored Jan 31, 2025
1 parent 92b5a66 commit 1f46f0a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
29 changes: 16 additions & 13 deletions lib/dotcom_web/live/trip_planner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ defmodule DotcomWeb.Live.TripPlanner do
def handle_event(
"input_form_change",
%{"input_form" => params},
%{assigns: %{input_form: %{changeset: %{params: current_params}}}} = socket
%{assigns: %{input_form: %{changeset: %{params: previous_params}}}} = socket
) do
params_with_datetime =
current_params
previous_params
|> Map.merge(params)
|> add_datetime_if_needed()
|> add_datetime_if_needed(previous_params)

changeset = InputForm.changeset(params_with_datetime)

Expand Down Expand Up @@ -327,17 +327,20 @@ defmodule DotcomWeb.Live.TripPlanner do
{:noreply, socket}
end

# When the datetime_type is "leave_at" or "arrive_by", we need to
# have a "datetime" indicating when we want to "leave at" or "arrive
# by", but because the datepicker only appears after a rider clicks
# on "Leave at" or "Arrive by", the actual value of "datetime"
# doesn't always appear in params. When that happens, we want to set
# "datetime" to a reasonable default.
defp add_datetime_if_needed(%{"datetime_type" => "now"} = params),
do: params |> Map.put("datetime", Timex.now("America/New_York"))
# If we **switch** from 'now' to 'depart at' or 'arrive by', we need to update the datetime to the nearest 5 minutes.
# We default the datetime to `Timex.now/1` if the type is 'now'.
# Otherwise, we keep the datetime the user set.
defp add_datetime_if_needed(params, %{"datetime_type" => "now"} = _previous_params) do
params |> Map.put("datetime", nearest_5_minutes())
end

defp add_datetime_if_needed(%{"datetime_type" => "now"} = params, _previous_params) do
params |> Map.put("datetime", Timex.now("America/New_York"))
end

defp add_datetime_if_needed(%{"datetime" => datetime} = params) when datetime != nil, do: params
defp add_datetime_if_needed(params), do: params |> Map.put("datetime", nearest_5_minutes())
defp add_datetime_if_needed(params, _previous_params) do
params
end

# Run an OTP plan on the changeset data and return itinerary groups or an error.
defp get_itinerary_groups(%Ecto.Changeset{valid?: true} = changeset) do
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ defmodule DotCom.Mixfile do
{:jason, "1.4.4", override: true},
{:logster, "1.1.1"},
{:mail, "0.3.1"},
{:mbta_metro, "0.1.20"},
{:mbta_metro, "0.1.21"},
{:mock, "0.3.9", [only: :test]},
{:mox, "1.2.0", [only: :test]},
{:msgpack, "0.8.1"},
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"makeup_elixir": {:hex, :makeup_elixir, "1.0.1", "e928a4f984e795e41e3abd27bfc09f51db16ab8ba1aebdba2b3a575437efafc2", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "7284900d412a3e5cfd97fdaed4f5ed389b8f2b4cb49efc0eb3bd10e2febf9507"},
"makeup_erlang": {:hex, :makeup_erlang, "1.0.1", "c7f58c120b2b5aa5fd80d540a89fdf866ed42f1f3994e4fe189abebeab610839", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "8a89a1eeccc2d798d6ea15496a6e4870b75e014d1af514b1b71fa33134f57814"},
"makeup_html": {:hex, :makeup_html, "0.1.2", "19d4050c0978a4f1618ffe43054c0049f91fe5feeb9ae8d845b5dc79c6008ae5", [:mix], [{:makeup, "~> 1.2", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "b7fb9afedd617d167e6644a0430e49c1279764bfd3153da716d4d2459b0998c5"},
"mbta_metro": {:hex, :mbta_metro, "0.1.20", "9c0ca924d4d9686534d4b9dc87faffcb100b8fb198ccbb4d6bf8abdacc6ce582", [:mix], [{:cva, "~> 0.2", [hex: :cva, repo: "hexpm", optional: false]}, {:floki, "~> 0.36", [hex: :floki, repo: "hexpm", optional: false]}, {:heroicons, "~> 0.5", [hex: :heroicons, repo: "hexpm", optional: true]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.7", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_live_view, "~> 1.0", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}, {:phoenix_storybook, "~> 0.6", [hex: :phoenix_storybook, repo: "hexpm", optional: false]}, {:timex, "~> 3.7", [hex: :timex, repo: "hexpm", optional: false]}], "hexpm", "82ecc7a1f800368fa2d713e48650fb2537f73e705a073a53848de4e92f6f07c5"},
"mbta_metro": {:hex, :mbta_metro, "0.1.21", "2b626ffad6572fb92f38c8c43ef31b88685145551ac189e898d83f22a832fff2", [:mix], [{:cva, "~> 0.2", [hex: :cva, repo: "hexpm", optional: false]}, {:floki, "~> 0.36", [hex: :floki, repo: "hexpm", optional: false]}, {:heroicons, "~> 0.5", [hex: :heroicons, repo: "hexpm", optional: true]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.7", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_live_view, "~> 1.0", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}, {:phoenix_storybook, "~> 0.6", [hex: :phoenix_storybook, repo: "hexpm", optional: false]}, {:timex, "~> 3.7", [hex: :timex, repo: "hexpm", optional: false]}], "hexpm", "c297bad7ab5e8be245fc7fb8584fac0fbccde393b2510fc10c26c15b410b8d99"},
"meck": {:hex, :meck, "0.9.2", "85ccbab053f1db86c7ca240e9fc718170ee5bda03810a6292b5306bf31bae5f5", [:rebar3], [], "hexpm", "81344f561357dc40a8344afa53767c32669153355b626ea9fcbc8da6b3045826"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
"mime": {:hex, :mime, "2.0.6", "8f18486773d9b15f95f4f4f1e39b710045fa1de891fada4516559967276e4dc2", [:mix], [], "hexpm", "c9945363a6b26d747389aac3643f8e0e09d30499a138ad64fe8fd1d13d9b153e"},
Expand Down

0 comments on commit 1f46f0a

Please sign in to comment.