Skip to content
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

Add the new metro map component to the preview trip planner #2208

Merged
merged 15 commits into from
Nov 5, 2024

Conversation

anthonyshull
Copy link
Contributor

Screenshot Capture - 2024-10-30 - 13-09-00

Adding and removing markers works. When all markers are removed, the map recenters itself.

@anthonyshull anthonyshull marked this pull request as ready for review October 31, 2024 19:35
@anthonyshull anthonyshull requested a review from a team as a code owner October 31, 2024 19:35
defp update_from_pin(socket, %{
"from" => %{"longitude" => from_longitude, "latitude" => from_latitude}
}) do
update_pin_in_socket(socket, [from_longitude, from_latitude], 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change lat/long from a named map to an array?

My concern is that while geojson does have the standard of [long, lat], [lat, long] is a lot more common in colloquial usage. (See Google Maps screenshot below, for instance). Because of that, just a list of numbers risks a very easy-to-make and hard-to-protect-against error of accidentally typing in [lat, long] instead of [long, lat].

Screenshot 2024-11-01 at 11 45 15 AM

Unless there's a good reason to follow the geojson standard, I'd strongly prefer to keep the named map all the way through.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maplibregl uses lng, lat. Since we're using that library, it makes sense to use that too. See the definition of center in the map config.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah - my comment wasn't clear enough. I saw that MapLibre (which we don't control) and Metro (which we do) use geojson format. What I'd prefer is to move the geojson usage to be closer to the underlying library call, rather than leaking it all the way through the live component.

I made a suggested refactor that does address this, but if you don't like that refactor, then let's revisit this conversation.

Copy link
Contributor

@joshlarson joshlarson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put a few comments in. I think all of them (at least discussing them) are approval-blockers for me.

@anthonyshull
Copy link
Contributor Author

I replaced the integers with atoms if that makes it easier to read.

id="trip-planner-map"
class="h-96 w-full relative overflow-none"
config={@map_config}
pins={@pins}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pins={@pins}
pins={[@from, @to]}

This, along with changing update_pin_in_socket to simply:

defp update_pin_in_socket(socket, [longitude, latitude], direction)
     when longitude != "" and latitude != "" do
  socket
  |> assign(direction, [String.to_float(longitude), String.to_float(latitude)])
end

defp update_pin_in_socket(socket, [longitude, latitude], direction)
     when longitude == "" or latitude == "" do
  socket
  |> assign(direction, [])
end

And adding

|> assign(:from, [])
|> assign(:to, [])

To the socket initialization in mount...

Would allow removing all of the place_pin logic, and most of the more complicated guts of the update_pin_in_socket functions as well.

(Net reduction of 41 lines of code 😇)

I think it would also be possible to combine update_pin_in_socket with update_*_pin, potentially for an even greater LoC reduction (and maybe clearer code), but this feels like a start.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooh actually, yes. If we replace all of the pin stuff with

 defp update_from_pin(socket, %{"from" => from}) do
   assign(socket, :from, to_geojson(from))
 end

 defp update_to_pin(socket, %{"to" => to}) do
   assign(socket, :to, to_geojson(to))
 end

 defp to_geojson(%{"longitude" => longitude, "latitude" => latitude})
      when longitude != "" and latitude != "" do
   [String.to_float(longitude), String.to_float(latitude)]
 end

 defp to_geojson(_coordinates) do
   []
 end

That gets us a net reduction of 53 lines of code (-54, but +1 because we have to set two assigns in the mount), and it resolves my concern with abruptly changing to geojson format in the middle of the live view, since this way we can keep the geojson format closer to the component where it's actually being used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

had to add in another 6 lines of code because you have to have defaults for update_from_pin and update_to_pin for when the pattern doesn't match.

Copy link
Contributor

@joshlarson joshlarson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of thoughts:

  • I stand by not loving the pins logic. I put a suggestion for how to improve that logic into the code. Happy to pair on that if you'd like, and open to other ideas.
  • Either as part of this PR or as a fast-follow, I think we need to add something about the CORS issue into the README, because otherwise local development will be broken.

Also, there's an issue where once the trip results appear, if :from and :to are close to lying on an east-west line, then the map won't change the zoom when it starts taking up just half of the screen, which means that the markers fall off of it.

IMO that's not blocking for this PR, but a bug to be aware of before releasing, so I wanted to flag it.

@@ -21,7 +21,7 @@ module.exports = {
coverageThreshold: {
global: {
branches: 89,
functions: 95,
functions: 90,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📓 Lowering this because it was failing even though I am not editing any js/ts in this PR.

@anthonyshull anthonyshull enabled auto-merge (squash) November 5, 2024 15:46
@anthonyshull anthonyshull merged commit c11b6cf into main Nov 5, 2024
17 checks passed
@anthonyshull anthonyshull deleted the ags/new-map branch November 5, 2024 15:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants