-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: display shape on map in shuttle definition pages #1037
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ defmodule ArrowWeb.ShuttleViewLive do | |
import Phoenix.HTML.Form | ||
alias Arrow.Shuttles | ||
alias Arrow.Shuttles.Shuttle | ||
alias ArrowWeb.ShapeView | ||
|
||
embed_templates "shuttle_live/*" | ||
|
||
|
@@ -14,6 +15,7 @@ defmodule ArrowWeb.ShuttleViewLive do | |
attr :http_action, :string | ||
attr :gtfs_disruptable_routes, :list, required: true | ||
attr :shapes, :list, required: true | ||
attr :map_props, :map, required: false, default: %{} | ||
|
||
def shuttle_form(assigns) do | ||
~H""" | ||
|
@@ -60,6 +62,7 @@ defmodule ArrowWeb.ShuttleViewLive do | |
/> | ||
</div> | ||
</div> | ||
<%= live_react_component("Components.ShapeViewMap", @map_props, id: "shuttle-view-map") %> | ||
<hr /> | ||
<h2>define route</h2> | ||
<.inputs_for :let={f_route} field={f[:routes]}> | ||
|
@@ -152,6 +155,14 @@ defmodule ArrowWeb.ShuttleViewLive do | |
""" | ||
end | ||
|
||
defp shapes_to_shapeviews(shapes) do | ||
shapes | ||
|> Enum.map(&Shuttles.get_shapes_upload/1) | ||
|> Enum.reject(&(&1 == {:ok, :disabled})) | ||
|> Enum.map(&ShapeView.shapes_map_view/1) | ||
|> Enum.map(&List.first(&1.shapes)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we ever expect to have more or less than one shape in As of now, if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can change the code to be more assertive. I think we should always get exactly one shape. When storing a KML, we only ever store one shape in one file, so we shouldn't get back multiple shapes per. If that ever doesn't hold true, it'll be bad for more than just this component. Given that Technically, we could probably have a shape record in the database that doesn't have a file in S3. It SHOULDN'T happen since we save to s3 before making the database entry, but since they are decoupled, nothing stops someone from coming along and deleting the shape file, for example. My thinking is in this case, it's OK to crash (which would happen in |
||
end | ||
|
||
def mount(%{"id" => id} = _params, session, socket) do | ||
logout_url = session["logout_url"] | ||
shuttle = Shuttles.get_shuttle!(id) | ||
|
@@ -160,6 +171,14 @@ defmodule ArrowWeb.ShuttleViewLive do | |
shapes = Shuttles.list_shapes() | ||
form = to_form(changeset) | ||
|
||
shuttle_shapes = | ||
shuttle | ||
|> Map.get(:routes) | ||
|> Enum.map(&Map.get(&1, :shape)) | ||
|> Enum.reject(&is_nil/1) | ||
|
||
shapes_map_view = shapes_to_shapeviews(shuttle_shapes) | ||
|
||
socket = | ||
socket | ||
|> assign(:form, form) | ||
|
@@ -170,6 +189,7 @@ defmodule ArrowWeb.ShuttleViewLive do | |
|> assign(:gtfs_disruptable_routes, gtfs_disruptable_routes) | ||
|> assign(:shapes, shapes) | ||
|> assign(:logout_url, logout_url) | ||
|> assign(:map_props, %{shapes: shapes_map_view}) | ||
|
||
{:ok, socket} | ||
end | ||
|
@@ -196,19 +216,31 @@ defmodule ArrowWeb.ShuttleViewLive do | |
|> assign(:gtfs_disruptable_routes, gtfs_disruptable_routes) | ||
|> assign(:shapes, shapes) | ||
|> assign(:logout_url, logout_url) | ||
|> assign(:map_props, %{shapes: []}) | ||
|
||
{:ok, socket} | ||
end | ||
|
||
def handle_event("validate", params, socket) do | ||
shuttle_params = params |> combine_params() | ||
|
||
form = | ||
socket.assigns.shuttle | ||
|> Shuttles.change_shuttle(shuttle_params) | ||
|> to_form(action: :validate) | ||
# A new shape is selected | ||
def handle_event( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assigning an |
||
"validate", | ||
%{"_target" => ["shuttle", "routes", _direction_id, "shape_id"]} = params, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I'm reading this right, aren't we validating only one direction? Will the shapes for both directions be in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The full form params are stored under the |
||
socket | ||
) do | ||
shapes = | ||
[ | ||
params["shuttle"]["routes"]["0"]["shape_id"], | ||
params["shuttle"]["routes"]["1"]["shape_id"] | ||
] | ||
|> Enum.reject(&(&1 == "")) | ||
|> Shuttles.get_shapes() | ||
|> shapes_to_shapeviews() | ||
|
||
validate(params, assign(socket, :map_props, %{socket.assigns.map_props | shapes: shapes})) | ||
end | ||
|
||
{:noreply, assign(socket, form: form)} | ||
def handle_event("validate", params, socket) do | ||
validate(params, socket) | ||
end | ||
|
||
def handle_event("edit", params, socket) do | ||
|
@@ -306,4 +338,15 @@ defmodule ArrowWeb.ShuttleViewLive do | |
route_changeset | ||
end | ||
end | ||
|
||
defp validate(params, socket) do | ||
shuttle_params = params |> combine_params() | ||
|
||
form = | ||
socket.assigns.shuttle | ||
|> Shuttles.change_shuttle(shuttle_params) | ||
|> to_form(action: :validate) | ||
|
||
{:noreply, assign(socket, form: form)} | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of curiosity, do we expect to ever need to disable shape storage? Or is the
:shape_storage_enabled?
config sort of a "dead feature flag" at this point? (Not exactly a feature flag since changing it requires a redeploy, but you get the gist)Not something to deal with in this PR, but it may be worth removing as part of a tech debt ticket.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's disabled by default for tests, which is why I made this deal with it gracefully. Otherwise any interaction with this form under test (without enabling shape storage first) was breaking.