From feb0141b371539cbbd7ffcc0aac749462599bd02 Mon Sep 17 00:00:00 2001 From: Josh Larson Date: Thu, 21 Nov 2024 11:45:10 -0500 Subject: [PATCH 01/10] feat: Toggle between summary and details view in itinerary panel --- .../trip_planner/itinerary_group.ex | 12 ++-- lib/dotcom_web/live/trip_planner.ex | 67 ++++++++++++++++++- 2 files changed, 71 insertions(+), 8 deletions(-) diff --git a/lib/dotcom_web/components/trip_planner/itinerary_group.ex b/lib/dotcom_web/components/trip_planner/itinerary_group.ex index 5dafdc127d..0e8a0d801e 100644 --- a/lib/dotcom_web/components/trip_planner/itinerary_group.ex +++ b/lib/dotcom_web/components/trip_planner/itinerary_group.ex @@ -4,8 +4,6 @@ defmodule DotcomWeb.Components.TripPlanner.ItineraryGroup do """ use DotcomWeb, :component - import DotcomWeb.Components.TripPlanner.ItineraryDetail - attr(:summary, :map, doc: "ItineraryGroups.summary()", required: true) attr(:itineraries, :list, doc: "List of %Dotcom.TripPlan.Itinerary{}", required: true) @@ -60,13 +58,15 @@ defmodule DotcomWeb.Components.TripPlanner.ItineraryGroup do Similar trips depart at <%= Enum.map(@summary.next_starts, &format_datetime_short/1) |> Enum.join(", ") %> - - """ end diff --git a/lib/dotcom_web/live/trip_planner.ex b/lib/dotcom_web/live/trip_planner.ex index ad9462ff08..f1a0d37ced 100644 --- a/lib/dotcom_web/live/trip_planner.ex +++ b/lib/dotcom_web/live/trip_planner.ex @@ -7,6 +7,7 @@ defmodule DotcomWeb.Live.TripPlanner do use DotcomWeb, :live_view + import DotcomWeb.Components.TripPlanner.ItineraryDetail import DotcomWeb.Components.TripPlanner.ItineraryGroup, only: [itinerary_group: 1] import MbtaMetro.Components.{Feedback, Spinner} @@ -28,6 +29,7 @@ defmodule DotcomWeb.Live.TripPlanner do |> assign(:from, []) |> assign(:to, []) |> assign(:submitted_values, nil) + |> assign(:itinerary_details_index, nil) |> assign_async(:results, fn -> {:ok, %{results: nil}} end) @@ -73,7 +75,7 @@ defmodule DotcomWeb.Live.TripPlanner do <.async_result :let={results} assign={@results}>
- <.itinerary_group :for={result <- results} {result} /> + <.itinerary_panel results={results} details_index={@itinerary_details_index} />
<.live_component @@ -88,7 +90,65 @@ defmodule DotcomWeb.Live.TripPlanner do """ end + defp itinerary_panel(%{details_index: details_index} = assigns) do + case details_index do + nil -> + ~H""" + <.itinerary_panel_with_all_results results={@results} /> + """ + + _ -> + ~H""" + <.itinerary_panel_with_specific_result results={@results} details_index={@details_index} /> + """ + end + end + + defp itinerary_panel_with_all_results(assigns) do + ~H""" + <.itinerary_group :for={{result, index} <- Enum.with_index(@results)} index={index} {result} /> + """ + end + + defp itinerary_panel_with_specific_result( + %{results: results, details_index: details_index} = assigns + ) do + assigns = + assigns + |> assign( + :itineraries, + results + |> Enum.at(details_index) + |> Map.get(:itineraries) + ) + + ~H""" +
+ + <.itinerary_detail :for={itinerary <- @itineraries} itinerary={itinerary} /> +
+ """ + end + @impl true + def handle_event("show_itinerary_details" = event, %{"index" => index_str} = params, socket) do + dbg(event) + dbg(params) + + {index, ""} = Integer.parse(index_str) + + {:noreply, socket |> assign(:itinerary_details_index, index)} + end + + def handle_event("show_itinerary_summary", _params, socket) do + {:noreply, socket |> assign(:itinerary_details_index, nil)} + end + def handle_event(_event, _params, socket) do {:noreply, socket} end @@ -112,7 +172,10 @@ defmodule DotcomWeb.Live.TripPlanner do |> assign_async(:results, fn -> case Dotcom.TripPlan.OpenTripPlanner.plan(data) do {:ok, itineraries} -> - {:ok, %{results: ItineraryGroups.from_itineraries(itineraries)}} + {:ok, + %{ + results: ItineraryGroups.from_itineraries(itineraries) + }} error -> error From c62bc3c6beb1e1b06c63d4632ace86e14a5d7050 Mon Sep 17 00:00:00 2001 From: Josh Larson Date: Thu, 21 Nov 2024 12:02:00 -0500 Subject: [PATCH 02/10] cleanup: Remove extraneous `dbg`'s --- lib/dotcom_web/live/trip_planner.ex | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/dotcom_web/live/trip_planner.ex b/lib/dotcom_web/live/trip_planner.ex index f1a0d37ced..2bb71ddd4d 100644 --- a/lib/dotcom_web/live/trip_planner.ex +++ b/lib/dotcom_web/live/trip_planner.ex @@ -136,10 +136,7 @@ defmodule DotcomWeb.Live.TripPlanner do end @impl true - def handle_event("show_itinerary_details" = event, %{"index" => index_str} = params, socket) do - dbg(event) - dbg(params) - + def handle_event("show_itinerary_details", %{"index" => index_str}, socket) do {index, ""} = Integer.parse(index_str) {:noreply, socket |> assign(:itinerary_details_index, index)} From 7227668dc0534e5c35395ba57c48dbe1f47c09da Mon Sep 17 00:00:00 2001 From: Josh Larson Date: Thu, 21 Nov 2024 12:02:59 -0500 Subject: [PATCH 03/10] cleanup: Remove useless formatting change --- lib/dotcom_web/live/trip_planner.ex | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/dotcom_web/live/trip_planner.ex b/lib/dotcom_web/live/trip_planner.ex index 2bb71ddd4d..11029f1024 100644 --- a/lib/dotcom_web/live/trip_planner.ex +++ b/lib/dotcom_web/live/trip_planner.ex @@ -169,10 +169,7 @@ defmodule DotcomWeb.Live.TripPlanner do |> assign_async(:results, fn -> case Dotcom.TripPlan.OpenTripPlanner.plan(data) do {:ok, itineraries} -> - {:ok, - %{ - results: ItineraryGroups.from_itineraries(itineraries) - }} + {:ok, %{results: ItineraryGroups.from_itineraries(itineraries)}} error -> error From 8505bc59783eb502d3930ea51bff911646f9d076 Mon Sep 17 00:00:00 2001 From: Josh Larson Date: Thu, 21 Nov 2024 14:08:38 -0500 Subject: [PATCH 04/10] fix: Add :index attr to <.itinerary_group /> definition --- lib/dotcom_web/components/trip_planner/itinerary_group.ex | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/dotcom_web/components/trip_planner/itinerary_group.ex b/lib/dotcom_web/components/trip_planner/itinerary_group.ex index 0e8a0d801e..0aa170d6bf 100644 --- a/lib/dotcom_web/components/trip_planner/itinerary_group.ex +++ b/lib/dotcom_web/components/trip_planner/itinerary_group.ex @@ -7,6 +7,11 @@ defmodule DotcomWeb.Components.TripPlanner.ItineraryGroup do attr(:summary, :map, doc: "ItineraryGroups.summary()", required: true) attr(:itineraries, :list, doc: "List of %Dotcom.TripPlan.Itinerary{}", required: true) + attr(:index, :integer, + doc: "Index into the full list where this itinerary group sits", + required: true + ) + @doc """ Renders a single itinerary group. """ From 47fc2ed0ca60284652b6d7fdd90703a782e26fae Mon Sep 17 00:00:00 2001 From: Josh Larson Date: Thu, 21 Nov 2024 14:09:15 -0500 Subject: [PATCH 05/10] cleanup: Refactor arguments to <.itinerary_panel_with_specific_result /> --- lib/dotcom_web/live/trip_planner.ex | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/lib/dotcom_web/live/trip_planner.ex b/lib/dotcom_web/live/trip_planner.ex index 11029f1024..e10624587f 100644 --- a/lib/dotcom_web/live/trip_planner.ex +++ b/lib/dotcom_web/live/trip_planner.ex @@ -90,7 +90,7 @@ defmodule DotcomWeb.Live.TripPlanner do """ end - defp itinerary_panel(%{details_index: details_index} = assigns) do + defp itinerary_panel(%{results: results, details_index: details_index} = assigns) do case details_index do nil -> ~H""" @@ -98,8 +98,10 @@ defmodule DotcomWeb.Live.TripPlanner do """ _ -> + assigns = assign(assigns, :result, Enum.at(results, details_index)) + ~H""" - <.itinerary_panel_with_specific_result results={@results} details_index={@details_index} /> + <.itinerary_panel_with_specific_result result={@result} /> """ end end @@ -110,17 +112,8 @@ defmodule DotcomWeb.Live.TripPlanner do """ end - defp itinerary_panel_with_specific_result( - %{results: results, details_index: details_index} = assigns - ) do - assigns = - assigns - |> assign( - :itineraries, - results - |> Enum.at(details_index) - |> Map.get(:itineraries) - ) + defp itinerary_panel_with_specific_result(%{result: result} = assigns) do + assigns = assign(assigns, :itineraries, Map.get(result, :itineraries)) ~H"""
From 340ac26b36a293781ff24b18985a030f1990b24f Mon Sep 17 00:00:00 2001 From: Josh Larson Date: Thu, 21 Nov 2024 21:02:14 -0500 Subject: [PATCH 06/10] refactor: Pull itinerary results section out as a LiveComponent --- .../trip_planner_results_section.ex | 76 +++++++++++++++++++ lib/dotcom_web/live/trip_planner.ex | 68 +++-------------- 2 files changed, 87 insertions(+), 57 deletions(-) create mode 100644 lib/dotcom_web/components/live_components/trip_planner_results_section.ex diff --git a/lib/dotcom_web/components/live_components/trip_planner_results_section.ex b/lib/dotcom_web/components/live_components/trip_planner_results_section.ex new file mode 100644 index 0000000000..f74831382d --- /dev/null +++ b/lib/dotcom_web/components/live_components/trip_planner_results_section.ex @@ -0,0 +1,76 @@ +defmodule DotcomWeb.Components.LiveComponents.TripPlannerResultsSection do + @moduledoc """ + The section of the trip planner page that shows the map and + the summary or details panel + """ + + use DotcomWeb, :live_component + + import DotcomWeb.Components.TripPlanner.ItineraryDetail + import DotcomWeb.Components.TripPlanner.ItineraryGroup, only: [itinerary_group: 1] + + def mount(_params, _session, socket) do + {:ok, socket} + end + + @impl true + def render(assigns) do + ~H""" +
+
+ <%= inspect(@error) %> +
+ <.async_result :let={results} assign={@results}> +
+ <.itinerary_panel results={results} details_index={@itinerary_details_index} /> +
+ + <.live_component + module={MbtaMetro.Live.Map} + id="trip-planner-map" + class="h-96 w-full relative overflow-none" + config={@map_config} + pins={[@from, @to]} + /> +
+ """ + end + + defp itinerary_panel(%{results: results, details_index: details_index} = assigns) do + case details_index do + nil -> + ~H""" + <.itinerary_panel_with_all_results results={@results} /> + """ + + _ -> + assigns = assign(assigns, :result, Enum.at(results, details_index)) + + ~H""" + <.itinerary_panel_with_specific_result result={@result} /> + """ + end + end + + defp itinerary_panel_with_all_results(assigns) do + ~H""" + <.itinerary_group :for={{result, index} <- Enum.with_index(@results)} index={index} {result} /> + """ + end + + defp itinerary_panel_with_specific_result(%{result: result} = assigns) do + assigns = assign(assigns, :itineraries, Map.get(result, :itineraries)) + + ~H""" +
+ + <.itinerary_detail :for={itinerary <- @itineraries} itinerary={itinerary} /> +
+ """ + end +end diff --git a/lib/dotcom_web/live/trip_planner.ex b/lib/dotcom_web/live/trip_planner.ex index e10624587f..a642bf45f3 100644 --- a/lib/dotcom_web/live/trip_planner.ex +++ b/lib/dotcom_web/live/trip_planner.ex @@ -7,11 +7,9 @@ defmodule DotcomWeb.Live.TripPlanner do use DotcomWeb, :live_view - import DotcomWeb.Components.TripPlanner.ItineraryDetail - import DotcomWeb.Components.TripPlanner.ItineraryGroup, only: [itinerary_group: 1] import MbtaMetro.Components.{Feedback, Spinner} - alias DotcomWeb.Components.LiveComponents.TripPlannerForm + alias DotcomWeb.Components.LiveComponents.{TripPlannerForm, TripPlannerResultsSection} alias Dotcom.TripPlan.{AntiCorruptionLayer, InputForm.Modes, ItineraryGroups} @form_id "trip-planner-form" @@ -69,61 +67,17 @@ defmodule DotcomWeb.Live.TripPlanner do <% end %> -
-
- <%= inspect(@error) %> -
- <.async_result :let={results} assign={@results}> -
- <.itinerary_panel results={results} details_index={@itinerary_details_index} /> -
- - <.live_component - module={MbtaMetro.Live.Map} - id="trip-planner-map" - class="h-96 w-full relative overflow-none" - config={@map_config} - pins={[@from, @to]} - /> -
-
- """ - end - - defp itinerary_panel(%{results: results, details_index: details_index} = assigns) do - case details_index do - nil -> - ~H""" - <.itinerary_panel_with_all_results results={@results} /> - """ - - _ -> - assigns = assign(assigns, :result, Enum.at(results, details_index)) - - ~H""" - <.itinerary_panel_with_specific_result result={@result} /> - """ - end - end - - defp itinerary_panel_with_all_results(assigns) do - ~H""" - <.itinerary_group :for={{result, index} <- Enum.with_index(@results)} index={index} {result} /> - """ - end - defp itinerary_panel_with_specific_result(%{result: result} = assigns) do - assigns = assign(assigns, :itineraries, Map.get(result, :itineraries)) - - ~H""" -
- - <.itinerary_detail :for={itinerary <- @itineraries} itinerary={itinerary} /> + <.live_component + module={TripPlannerResultsSection} + id="trip-planner-results" + itinerary_details_index={@itinerary_details_index} + results={@results} + error={@error} + map_config={@map_config} + from={@from} + to={@to} + />
""" end From 68e225ba02e3fb3ed0c51bbbcc22fea97b6b2e96 Mon Sep 17 00:00:00 2001 From: Josh Larson Date: Thu, 21 Nov 2024 21:24:22 -0500 Subject: [PATCH 07/10] refactor: Move itinerary_details_index into TripPlannerResultsSection ...and rename it to expanded_itinerary_index --- .../trip_planner_results_section.ex | 36 +++++++++++++++---- .../trip_planner/itinerary_group.ex | 3 ++ lib/dotcom_web/live/trip_planner.ex | 12 ------- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/lib/dotcom_web/components/live_components/trip_planner_results_section.ex b/lib/dotcom_web/components/live_components/trip_planner_results_section.ex index f74831382d..148dadd61e 100644 --- a/lib/dotcom_web/components/live_components/trip_planner_results_section.ex +++ b/lib/dotcom_web/components/live_components/trip_planner_results_section.ex @@ -9,8 +9,9 @@ defmodule DotcomWeb.Components.LiveComponents.TripPlannerResultsSection do import DotcomWeb.Components.TripPlanner.ItineraryDetail import DotcomWeb.Components.TripPlanner.ItineraryGroup, only: [itinerary_group: 1] - def mount(_params, _session, socket) do - {:ok, socket} + @impl true + def mount(socket) do + {:ok, socket |> assign(:expanded_itinerary_index, nil)} end @impl true @@ -22,7 +23,11 @@ defmodule DotcomWeb.Components.LiveComponents.TripPlannerResultsSection do <.async_result :let={results} assign={@results}>
- <.itinerary_panel results={results} details_index={@itinerary_details_index} /> + <.itinerary_panel + results={results} + details_index={@expanded_itinerary_index} + target={@myself} + />
<.live_component @@ -40,21 +45,26 @@ defmodule DotcomWeb.Components.LiveComponents.TripPlannerResultsSection do case details_index do nil -> ~H""" - <.itinerary_panel_with_all_results results={@results} /> + <.itinerary_panel_with_all_results results={@results} target={@target} /> """ _ -> assigns = assign(assigns, :result, Enum.at(results, details_index)) ~H""" - <.itinerary_panel_with_specific_result result={@result} /> + <.itinerary_panel_with_specific_result result={@result} target={@target} /> """ end end defp itinerary_panel_with_all_results(assigns) do ~H""" - <.itinerary_group :for={{result, index} <- Enum.with_index(@results)} index={index} {result} /> + <.itinerary_group + :for={{result, index} <- Enum.with_index(@results)} + index={index} + target={@target} + {result} + /> """ end @@ -63,7 +73,7 @@ defmodule DotcomWeb.Components.LiveComponents.TripPlannerResultsSection do ~H"""
-
""" end + + @impl true + def handle_event("show_itinerary_details", %{"index" => index_str}, socket) do + {index, ""} = Integer.parse(index_str) + + {:noreply, socket |> assign(:expanded_itinerary_index, index)} + end + + @impl true + def handle_event("show_itinerary_summary", _params, socket) do + {:noreply, socket |> assign(:expanded_itinerary_index, nil)} + end end diff --git a/lib/dotcom_web/components/trip_planner/itinerary_group.ex b/lib/dotcom_web/components/trip_planner/itinerary_group.ex index 0aa170d6bf..5626b701f7 100644 --- a/lib/dotcom_web/components/trip_planner/itinerary_group.ex +++ b/lib/dotcom_web/components/trip_planner/itinerary_group.ex @@ -12,6 +12,8 @@ defmodule DotcomWeb.Components.TripPlanner.ItineraryGroup do required: true ) + attr :target, :string, required: true + @doc """ Renders a single itinerary group. """ @@ -66,6 +68,7 @@ defmodule DotcomWeb.Components.TripPlanner.ItineraryGroup do