Skip to content

Clean up javascript#365

Open
matt-bernhardt wants to merge 1 commit intomainfrom
better-js
Open

Clean up javascript#365
matt-bernhardt wants to merge 1 commit intomainfrom
better-js

Conversation

@matt-bernhardt
Copy link
Member

@matt-bernhardt matt-bernhardt commented Mar 3, 2026

This is some cleanup to the javascript that I ended up doing while debugging Matomo, and thought we might want to consider the change for real.

The motivation was seeing some errors in the USE console, which this tries to resolve. I'm not sure that the strategy followed makes the most sense - particularly with search_form.js, because the change on line 71 feels like it might not be necessary. I'm proposing this as-is to get feedback from reviewers. Details of the change are in the commit message, not this PR text.

Please note - I requested the code review from Copilot as a test, not a commitment to make it happy. I'd welcome your thoughts.

Developer

Accessibility
  • ANDI or WAVE has been run in accordance to our guide.
  • This PR contains no changes to the view layer.
  • New issues flagged by ANDI or WAVE have been resolved.
  • New issues flagged by ANDI or WAVE have been ticketed (link in the Pull Request details above).
  • No new accessibility issues have been flagged.
New ENV
  • All new ENV is documented in README.
  • All new ENV has been added to Heroku Pipeline, Staging and Prod.
  • ENV has not changed.
Approval beyond code review
  • UXWS/stakeholder approval has been confirmed.
  • UXWS/stakeholder review will be completed retroactively.
  • UXWS/stakeholder review is not needed.
Additional context needed to review

E.g., if the PR includes updated dependencies and/or data
migration, or how to confirm the feature is working.

Code Reviewer

Code
  • I have confirmed that the code works as intended.
  • Any CodeClimate issues have been fixed or confirmed as
    added technical debt.
Documentation
  • The commit message is clear and follows our guidelines
    (not just this pull request message).
  • The documentation has been updated or is unnecessary.
  • New dependencies are appropriate or there were no changes.
Testing
  • There are appropriate tests covering any new functionality.
  • No additional test coverage is required.

** Why are these changes being introduced:

Currently there are some parts of the application that throw errors in
the console because they aren't meant to be executed. This seems to
happen mostly with UI elements that are meant only for GeoData, but are
still executed in USE.

** Relevant ticket(s):

n/a

** How does this address that need:

This slightly refactors the javascript for those elements that are
throwing errors - adding a guard clause for some filter elements, and
adding using optional chaining for some event listeners.

** Document any side effects to this change:

Hopefully none - the console should be somewhat cleaner for USE, and
unaffected for GeoData.
@matt-bernhardt matt-bernhardt requested a review from Copilot March 3, 2026 21:23
@matt-bernhardt matt-bernhardt marked this pull request as ready for review March 3, 2026 21:24
@coveralls
Copy link

Pull Request Test Coverage Report for Build 22643369980

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 98.14%

Totals Coverage Status
Change from base Build 22584961347: 0.0%
Covered Lines: 1319
Relevant Lines: 1344

💛 - Coveralls

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR reduces JavaScript console errors by ensuring event listeners are only attached when the related DOM elements exist, particularly on USE pages that don’t render GeoData-only UI.

Changes:

  • Use optional chaining when attaching the #advanced-summary click listener to avoid errors when the element is absent.
  • Add an early-return guard in filter initialization to skip listener registration when filters aren’t rendered.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
app/javascript/search_form.js Avoids addEventListener on a missing #advanced-summary element by using optional chaining.
app/javascript/filters.js Prevents errors on pages without filters by returning early when #filter-toggle / #filter-container are missing.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +71 to +75
document.getElementById('advanced-summary')?.addEventListener('click', () => {
togglePanelState(advancedPanel);
});
} else {
document.getElementById('advanced-summary').addEventListener('click', () => {
document.getElementById('advanced-summary')?.addEventListener('click', () => {
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

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

The advanced-summary click handler is identical in both the if and else branches. Consider registering this listener once (outside the conditional) to avoid duplication and reduce the chance of the two branches drifting over time.

Copilot uses AI. Check for mistakes.
});

document.getElementById('advanced-summary').addEventListener('click', () => {
document.getElementById('advanced-summary')?.addEventListener('click', () => {
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

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

The condition Array.from(allPanels).includes(geoboxPanel && geodistancePanel) is hard to read and doesn’t clearly express the intent (it relies on && returning the second operand). Consider rewriting it as an explicit presence check (e.g., if (geoboxPanel && geodistancePanel)) or two explicit includes(...) checks so it’s unambiguous.

Copilot uses AI. Check for mistakes.
Copy link
Contributor

@djanelle-mit djanelle-mit left a comment

Choose a reason for hiding this comment

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

From what I can see, this works as intended. I no longer see console errorsfrom those scripts on the PR build as I do on stage.

Is there any easy way to test this PR build with GeoData? I think the only missing piece would be if the filter panel interactions still work as expected there.

Copy link
Contributor

@jazairi jazairi left a comment

Choose a reason for hiding this comment

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

Thanks for this simple and effective refactor! It seems to be working in GeoData as well.

For what it's worth, I think CoPilot makes some good points. 👀 But, I'd consider them nonblocking, especially since this PR did not introduce those issues.

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.

6 participants