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

Get registration location map working with OSM #12495

Merged
merged 4 commits into from
May 31, 2024

Conversation

cillian
Copy link
Contributor

@cillian cillian commented May 17, 2024

What? Why?

This add two things to get a bit closer to #5542

(1) It prevents a broken map from appearing during registration if an instance has a GOOGLE_MAPS_API_KEY environment variable present i.e. they already have Google Maps enabled AND that instance then enables Open Street Map (OSM). This issue was spotted here. The fix for this is in the using_google_maps? method.

(2) It gets the registration location map working with OSM, see:

osm-registration-map.mp4

What should we test?

  • Step 1: Enable OSM
    • Sign in as a super admin and go to the Configuration > Content section.
    • Fill in the OSM settings like in screenshot below. With OSM you can choose between a number of different tile providers. In this example MapBox is used, you can create an API key with them for free)
      osm-settings
  • Step 2: Register as a new enterprise and check you can set your location on the map like you can when Google Maps is enabled.

Release notes

Changelog Category (reviewers may add a label for the release notes):

  • User facing changes

@rioug rioug added the user facing changes Thes pull requests affect the user experience label May 20, 2024
Copy link
Collaborator

@rioug rioug left a comment

Choose a reason for hiding this comment

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

It's a nice addition, thanks.
I would like to see some test here, it's probably tricky to test the stimulus controller with Jest given the interaction with the map.But, I think we should at a minimum test that the OSM map is displayed when configured properly, which should be doable via system spec. Could you give it a go ?

@cillian
Copy link
Contributor Author

cillian commented May 24, 2024

No problem, thanks for the review. We could add a system test like below to https://github.com/openfoodfoundation/openfoodnetwork/blob/master/spec/system/consumer/registration_spec.rb ....

it "displays the location map using Open Street Maps" do
  visit registration_path
  switch_to_login_tab
 
  fill_in "Email", with: user.email
  fill_in "Password", with: user.password
  click_button "Login"
  click_button "Let's get started!"

  # The .leaflet-container class is only present if map is initialised
  expect(page).to have_selector("#open-street-map.leaflet-container")
end

But then I added view and helper specs instead because I was wondering would a system spec abuse OSM tile servers by sending requests everytime the tests are ran. Not sure if that's okay?

Copy link
Collaborator

@rioug rioug left a comment

Choose a reason for hiding this comment

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

I was wondering would a system spec abuse OSM tile servers by sending requests everytime the tests are ran. Not sure if that's okay?

Indeed that's an issue, and we don't want to rely on a third party when running system specs. There is always the option of stubbing the request to OSM/google map, but I like your idea of using a view test. System test are expensive to run and it's not strictly necessary for this.
Thanks for your help 🙏 !

@dacook dacook self-requested a review May 27, 2024 03:30
Copy link
Member

@dacook dacook left a comment

Choose a reason for hiding this comment

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

Great, thanks for implementing Leaflet/OSM!
I wondered if this new implementation could also be used for Google Maps (https://stackoverflow.com/questions/281264/remove-empty-elements-from-an-array-in-javascript).. but I think it's better to keep the official Google API.

Comment on lines +56 to +57
if !$scope.latLong && openStreetMap && openStreetMap.dataset.latitude && openStreetMap.dataset.longitude
$scope.latLong = { latitude: openStreetMap.dataset.latitude, longitude: openStreetMap.dataset.longitude }
Copy link
Member

Choose a reason for hiding this comment

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

FYI this could be a little neater with Optional chaining
(but it's totally fine as-is)

Comment on lines +83 to +94
#displayMapWhenAtRegistrationDetailsStep() {
const observer = new IntersectionObserver(
([intersectionObserverEntry]) => {
if(intersectionObserverEntry.target.offsetParent !== null) {
this.#displayMap();
observer.disconnect()
}
},
{ threshold: [0] }
);
observer.observe(document.getElementById("registration-details"));
}
Copy link
Member

Choose a reason for hiding this comment

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

Clever fix! I'm not sure if we'll need it for other purposes, but I think it could easily become a generic method (with the element ID as a parameter).

@filipefurtad0 filipefurtad0 self-assigned this May 31, 2024
@filipefurtad0 filipefurtad0 added pr-staged-fr staging.coopcircuits.fr and removed pr-staged-fr staging.coopcircuits.fr labels May 31, 2024
@filipefurtad0
Copy link
Contributor

Hey @cillian ,

Thank you for the detailed instructions. Proceeded as you've suggested, and was able to verify that the Open Street map is displayed when creating an enterprise:

image

Also, I've verified that the a change is persistent when one adjusts the suggested location (after typing in an address and clicking Locate on Map). Below, a change from the initially displayed position 1) from 2) was made (when creating an enterprise), which is then shown, when visiting the /map tab:

image

(apologies, no video... one was recorded with Loom, but somehow it is still uploading, and I think it does not make sense to wait longer...)

I've made a quick test to verify that no regression is introduced, i.e., that having the open street maps disabled, allows using the previous google-maps as before - all good 👍

Also, verified that enabling OpenStreetMaps with an invalid config does not break the page anymore:

image

This looks great, merging this PR. Thank you 💪

@filipefurtad0 filipefurtad0 merged commit 925d257 into openfoodfoundation:master May 31, 2024
52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
user facing changes Thes pull requests affect the user experience
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

4 participants