Skip to content

Commit

Permalink
handle string datetime_type
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonyshull committed Oct 17, 2024
1 parent 223c9d3 commit c99efb8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
4 changes: 2 additions & 2 deletions lib/dotcom/trip_plan/input_form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ defmodule Dotcom.TripPlan.InputForm do
%{
fromPlace: PlanParams.to_place_param(from),
toPlace: PlanParams.to_place_param(to),
arriveBy: datetime_type == :arrive_by,
arriveBy: datetime_type == "arrive_by",
date: PlanParams.to_date_param(datetime),
time: PlanParams.to_time_param(datetime),
transportModes: __MODULE__.Modes.selected_mode_keys(modes) |> PlanParams.to_modes_param(),
Expand Down Expand Up @@ -110,7 +110,7 @@ defmodule Dotcom.TripPlan.InputForm do

defp validate_chosen_datetime(changeset) do
case get_field(changeset, :datetime_type) do
:now ->
"now" ->
force_change(changeset, :datetime, Util.now())

_ ->
Expand Down
27 changes: 14 additions & 13 deletions lib/dotcom_web/components/live_components/trip_planner_form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,13 @@ defmodule DotcomWeb.Components.LiveComponents.TripPlannerForm do

@impl true
def mount(socket) do
form =
%InputForm{}
|> InputForm.changeset(@form_defaults)
|> to_form()

{:ok,
assign(socket, %{
form: form,
location_keys: InputForm.Location.fields(),
show_datepicker: false
})}
defaults = %{
form: %InputForm{} |> InputForm.changeset(@form_defaults) |> to_form(),
location_keys: InputForm.Location.fields(),
show_datepicker: false
}

{:ok, assign(socket, defaults)}
end

@impl true
Expand All @@ -46,7 +42,7 @@ defmodule DotcomWeb.Components.LiveComponents.TripPlannerForm do
phx-submit="save_form"
phx-target={@myself}
>
<div :for={field <- [:from, :to]} class="mb-1">
<div :for={field <- [:from, :to]} class="mb-1" id="trip-planner-locations" phx-update="ignore">
<.algolia_autocomplete
config_type="trip-planner"
placeholder="Enter a location"
Expand Down Expand Up @@ -149,7 +145,12 @@ defmodule DotcomWeb.Components.LiveComponents.TripPlannerForm do
If the user selects arrive by or leave at, then we show the datepicker and set the time to the nearest 5 minutes.
"""
def handle_event("toggle_datepicker", %{"input_form" => %{"datetime_type" => "now"}}, socket) do
{:noreply, assign(socket, show_datepicker: false)}
new_socket =
socket
|> assign(show_datepicker: false)
|> push_event("set-datetime", %{datetime: nearest_5_minutes()})

{:noreply, new_socket}
end

def handle_event("toggle_datepicker", _, socket) do
Expand Down
2 changes: 1 addition & 1 deletion lib/dotcom_web/live/trip_planner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ defmodule DotcomWeb.Live.TripPlanner do
to <strong><%= @submitted_values.to.name %></strong>
<br /> using <strong><%= Modes.selected_modes(@submitted_values.modes) %></strong>,
<strong>
<%= if @submitted_values.datetime_type == :arrive_by, do: "Arriving by", else: "Leaving" %> <%= @submitted_values.datetime
<%= if @submitted_values.datetime_type == "arrive_by", do: "Arriving by", else: "Leaving" %> <%= @submitted_values.datetime
|> Timex.format!("{Mfull} {D}, {h12}:{m} {AM}") %>
</strong>
</p>
Expand Down

0 comments on commit c99efb8

Please sign in to comment.