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

MBS-13320 (II): Warn on form unload with rel changes #3064

Merged
merged 1 commit into from
Oct 31, 2023

Conversation

derat
Copy link
Contributor

@derat derat commented Oct 27, 2023

MBS-13320 (II)

Make the beforeunload listener installed by MB.installFormUnloadWarning additionally use CSS selectors to check for pending relationship or URL changes.

Previously, only changes to input elements were detected.

Problem

#3054 only warns when navigating away from edit forms if a change event has been observed. This detects changes to text fields or checkboxes, but it doesn't catch relationship or URL changes. As a result, editors could still lose work if they accidentally closed or navigated away from forms when making relationship or URL edits.

Solution

@jesus2099 pointed out that the relationship editor uses rel-add, rel-edit, and rel-remove CSS classes when relationships are being changed. This change queries for those classes in the beforeunload handler to additionally detect relationship and URL changes.

Just to mention it, I think that changes to the artist form's "area" input still aren't detected, but I don't see an obvious way to detect those without adding special code for that one specific case.

Testing

I verified that I now get prompted when navigating away from the artist and release group forms after adding/editing/removing relationships and adding/editing/removing URLs.

I suspect that I'll need to update this PR to update fix Selenium tests that end with pending relationship/URL changes, but I don't think I have any easy way of figuring out which ones need to be updated until the checks run (since I'm using musicbrainz-docker, I can't run Selenium tests locally).

Action

Nothing special needed here, I don't think.

@derat
Copy link
Contributor Author

derat commented Oct 27, 2023

(I've started the process of updating Selenium tests that leave pending changes lying around. Hopefully there'll be fewer of them this time.)

@derat
Copy link
Contributor Author

derat commented Oct 27, 2023

@mwiencek and @reosarevok, this is ready for review now.

@jesus2099
Copy link
Contributor

jesus2099 commented Oct 28, 2023

About this loop:

// Check if there are pending relationship or URL changes.
    if (!inputsChanged) {
      let relsChanged = false;
      for (const sel of [
        '#relationship-editor .rel-add',
        '#relationship-editor .rel-edit',
        '#relationship-editor .rel-remove',
        '#external-links-editor .rel-add',
        '#external-links-editor .rel-edit',
        '#external-links-editor .rel-remove',
      ]) {
        if (!!document.querySelector(sel)) {
          relsChanged = true;
          break;
        }
      }
      if (!relsChanged) {
        return false;
      }
    }

I think I would have simply used a single querySelector but maybe it's for layout purpose? Eslint or something?

Something like:

// Check if there are pending relationship or URL changes.
    if (!inputsChanged) {
      if (!document.querySelector("form[class^='edit'] .rel-add, form[class^='edit'] .rel-edit, form[class^='edit'] .rel-remove")) {
        return false;
      }
    }

Or, if the form variable is available there:

// Check if there are pending relationship or URL changes.
    if (!inputsChanged) {
      if (!form.querySelector(".rel-add, .rel-edit, .rel-remove")) {
        return false;
      }
    }

I had difficulty understanding the code, otherwise... But I'm not a reference. 🤭

@derat
Copy link
Contributor Author

derat commented Oct 28, 2023

I think I would have simply used a single querySelector but maybe it's for layout purpose? Eslint or something?

I was concerned that more-generic selectors could unintentionally match other elements (especially if someone makes CSS changes in the future). Happy to switch it if the maintainers think it's safe to do so, though. :-)

@jesus2099
Copy link
Contributor

Yes or instead of a loop, we could join them:

form.querySelector([
  '#relationship-editor .rel-add',
  '#relationship-editor .rel-edit',
  '#relationship-editor .rel-remove',
  '#external-links-editor .rel-add',
  '#external-links-editor .rel-edit',
  '#external-links-editor .rel-remove',
].join(", "))

It's just me maybe, I found a loop was kind of complex.

Make the beforeunload listener installed by
MB.installFormUnloadWarning additionally use CSS selectors
to check for pending relationship or URL changes.

Previously, only changes to input elements were detected.
@derat
Copy link
Contributor Author

derat commented Oct 28, 2023

Thanks, that feels cleaner to me too! I've pushed an updated version.

Copy link
Member

@reosarevok reosarevok left a comment

Choose a reason for hiding this comment

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

Tested locally, seems to work nicely :)

Copy link
Member

@mwiencek mwiencek left a comment

Choose a reason for hiding this comment

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

LGTMBDNT

@reosarevok reosarevok merged commit ebbadda into metabrainz:master Oct 31, 2023
3 checks passed
reosarevok added a commit that referenced this pull request Nov 1, 2023
* master:
  Update POT file from the current database
  Upgrade Flow to 0.220.0
  MBS-13320 (II): Warn on form unload with rel changes (#3064)
  Add labels to all tests in Data::Editor
  Give a name to remaining 'all' test and add labels
  Split various_edit_counts into separate test
  Split editor subscription methods into separate test
  MBS-13334: Also find emails with capitalization differences
  Split and expand email checking tests
  MBS-13320: Prompt before unloading modified forms (#3054)
  Split new editor creation test to new subtest
  Upgrade Flow to 0.219.3
  Remove a few $FlowIgnores
  Remove uses of any in SeriesIndex.js
  Replace $MakeReadOnly type with one using infer
  Upgrade Flow to 0.219.2
  Upgrade Flow to 0.219.0
  Upgrade Flow to 0.218.1
  Upgrade Flow to 0.218.0
  Upgrade Flow to 0.217.2
  Upgrade Flow to 0.217.1
  Upgrade Flow to 0.217.0
  Remove unnecessary check in concatStringMatch
  Fix indentation in PhraseVarArgs class
  Upgrade Flow to 0.216.1
  Upgrade Flow to 0.216.0
  Upgrade Flow to 0.215.1
  Upgrade Flow to 0.215.0
  MBS-13319: Show help bubble for language/script on release editor (#3053)
  Use our new contains_ functions in more places (#3051)
  MBS-8952: Can rename recordings via autocomplete (#3039)
  MBS-13317: Remove Kget.jp from lyrics whitelist (#3052)
  Fix newly detected eslint issues
  Upgrade ESLint and associated plugins
  Upgrade Babel dependencies
  Regenerate yarn.lock
  Upgrade Flow to 0.214.0
  Upgrade Flow to 0.213.1
  Upgrade Flow to 0.213.0
  Upgrade Flow to 0.212.0
  Upgrade Flow to 0.211.1
  Upgrade Flow to 0.211.0
  Upgrade Flow to 0.210.2
  Upgrade Flow to 0.210.1
  Upgrade Flow to 0.210.0
  Upgrade Flow to 0.209.0
  Upgrade Flow to 0.208.1
  Upgrade Flow to 0.208.0
  Switch Babel & ESLint parsers to hermes
  Upgrade Flow to 0.207.0
  Upgrade Flow to 0.206.0
  Upgrade Flow to 0.205.1
  Upgrade Flow to 0.205.0
  Upgrade Flow to 0.204.1
  Upgrade Flow to 0.204.0
  Upgrade Flow to 0.203.1
  Upgrade Flow to 0.203.0
  Upgrade Flow to 0.202.1
  Upgrade Flow to 0.202.0
  Remove all uses of the switch feature (given/when)
  Remove all uses of the smartmatch operator
  Link directly to the anchor in Markdown
  Update INSTALL.md's building static resources section (#3042)
yvanzo added a commit that referenced this pull request Nov 13, 2023
* beta:
  Translated using Weblate (Italian)
  Translated using Weblate (Lithuanian)
  Added translation using Weblate (Thai)
  Added translation using Weblate (Thai)
  Translated using Weblate (Russian)
  Translated using Weblate (French)
  Update translation files
  Update POT files using the production database
  Translated using Weblate (Italian)
  Update translation files
  Fix wrongly ordered calls l(addColonText(()
  Update POT files using the production database
  MBS-13336: Add a script to rebuild all indexes using collations (#3062)
  MBS-13309: Restrict cross-origin requests to /ws/js/edit (#3075)
  MBS-13348: Fix edit_data_idx_link_type (#3073)
  MBS-13349: Support LibraryThing disambiguation URLs (#3077)
  MBS-13347: Relationship Type edit search times out (#3074)
  MBS-5987: Add "Submit votes & edit notes" to top (#3072)
  MBS-13350: Return all barcode matches in release editor (#3076)
  Translated using Weblate (Chinese (Simplified))
  Translated using Weblate (German)
  Translated using Weblate (Italian)
  Translated using Weblate (French)
  Update translation files
  Add test for isObjectEmpty
  Add test for isBlank
  Add test for escapeLuceneValue
  Add test for escapeRegExp
  Add test for relationshipDateText
  Add test for sortByEntityName
  Move getSortName entities to constants
  Add test for isDisabledLink
  Add test for isFutureDate
  Add tests for strings
  Make renderMergeCheckboxElement function a component
  Fix require sort order
  Add test for isDateEmpty
  Add test for formatEndDate
  Add test for entityHref
  Make generic entity consts reusable
  Add test for isGuid
  Add test for isolateText
  Add test for natatime
  Add test for primaryAreaCode
  Add test for isDatabaseRowId
  Add test for getSortName
  More precise comparison for null barcode
  Add test for formatBarcode
  Add tests for clean
  Add tests for arrays.js functions
  Add test for calculateFullToc
  Add test for bracketed
  Add renderToStaticMarkup wrapper
  Use compareDates as the base for areDatesEqual
  Add test for areDatePeriodsEqual
  Use exactCount for newline-after-import eslint rule (#3071)
  MBS-12727: Show genre alias connections for tags (#2757)
  MBS-4822: Drop duplicate colon strings
  Add context to "Cancelled" and drop duplicate colons
  Add context to "Ended" and drop duplicate colons
  Add context to "Location" and drop duplicate colons
  Add context to EditReleaseEvents headings
  Standardize election headers and drop duplicate colons
  Add context to "Old" and drop duplicate colons
  Add context to "Status" and drop duplicate colons
  Add context to "Merge" and drop duplicate colons
  Add context to "Preview" and drop duplicate colons
  MBS-13207 (II): Move "Add a new recording" above suggestions (#3069)
  MBS-12893: Add places column to country statistics
  MBS-12852: Add events column to country statistics
  Ensure release stats are calculated for every country
  Use "create", "add", "enter" in more specific ways (#3070)
  Bump hermes-eslint to 0.17.1
  Bump eslint-plugin-ft-flow to 3.0.1
  Move eslint-plugin-simple-import-sort to devDependencies
  Migrate to Yarn v4
  Fix permission errors in run_selenium_tests.sh
  Bump SIR_TAG to v3.0.1 in the test images
  Switch to chrome-for-testing in tests
  Bump chromedriver to 119.0.6045.105
  Create the pgtap extension as a superuser
  Bump musicbrainz-tests-perl-5-dot-30 image
  Fix phusion/baseimage focal tag
  Fix PGDATA permission issues
  MBS-13261: Upgrade the required version of Node.js to 20
  Move duplicated CircleCI steps to script
  Regenerate cpanfile.snapshot
  Fix apt-key deprecation warning
  Upgrade the Docker base images to jammy
  Bump ARTWORK_REDIRECT_COMMIT
  Copy Dockerfile.tests to Dockerfile.perl5.30.tests
  Explicitly list the .gitignore'd Dockerfiles
  Replace xgettext-js with forked copy using hermes-parser (#3067)
  Use Unicode quotes for string being changed already
  Use Unicode quotes for string being changed already
  Remove i.e. from user-facing strings
  Remove e.g. from user-facing strings
  Capitalize URL in user-facing string (#3058)
  Normalize "track duration" to "track length" in UI (#3057)
  Normalize "pending edits" to "open edits" in UI (#3056)
  Update POT file from the current database
  Translated using Weblate (Turkish)
  Translated using Weblate (German)
  Translated using Weblate (Japanese)
  Added translation using Weblate (Chinese (Traditional))
  Translated using Weblate (Japanese)
  Translated using Weblate (Spanish)
  Translated using Weblate (Portuguese)
  Translated using Weblate (Chinese (Simplified))
  Translated using Weblate (Russian)
  Translated using Weblate (French)
  Translated using Weblate (Bengali)
  Translated using Weblate (Portuguese)
  Translated using Weblate (German)
  Translated using Weblate (Italian)
  Translated using Weblate (Lithuanian)
  Use "remove" consistently for non-editor cases
  Translated using Weblate (Turkish)
  Translated using Weblate (German)
  Translated using Weblate (Japanese)
  Added translation using Weblate (Chinese (Traditional))
  Translated using Weblate (Japanese)
  Translated using Weblate (Spanish)
  Translated using Weblate (Portuguese)
  Translated using Weblate (Chinese (Simplified))
  Translated using Weblate (Russian)
  Translated using Weblate (French)
  Translated using Weblate (Bengali)
  Translated using Weblate (Portuguese)
  Translated using Weblate (German)
  Translated using Weblate (Italian)
  Translated using Weblate (Lithuanian)
  Upgrade Flow to 0.220.0
  MBS-13320 (II): Warn on form unload with rel changes (#3064)
  Add labels to all tests in Data::Editor
  Give a name to remaining 'all' test and add labels
  Split various_edit_counts into separate test
  Split editor subscription methods into separate test
  MBS-13334: Also find emails with capitalization differences
  Split and expand email checking tests
  MBS-13320: Prompt before unloading modified forms (#3054)
  Split new editor creation test to new subtest
  Upgrade Flow to 0.219.3
  Remove a few $FlowIgnores
  Remove uses of any in SeriesIndex.js
  Replace $MakeReadOnly type with one using infer
  Upgrade Flow to 0.219.2
  Upgrade Flow to 0.219.0
  Upgrade Flow to 0.218.1
  Upgrade Flow to 0.218.0
  Upgrade Flow to 0.217.2
  Upgrade Flow to 0.217.1
  Upgrade Flow to 0.217.0
  Remove unnecessary check in concatStringMatch
  Fix indentation in PhraseVarArgs class
  Upgrade Flow to 0.216.1
  Upgrade Flow to 0.216.0
  Upgrade Flow to 0.215.1
  Upgrade Flow to 0.215.0
  MBS-13319: Show help bubble for language/script on release editor (#3053)
  Use our new contains_ functions in more places (#3051)
  MBS-8952: Can rename recordings via autocomplete (#3039)
  MBS-13317: Remove Kget.jp from lyrics whitelist (#3052)
  Fix newly detected eslint issues
  Upgrade ESLint and associated plugins
  Upgrade Babel dependencies
  Regenerate yarn.lock
  Upgrade Flow to 0.214.0
  Upgrade Flow to 0.213.1
  Upgrade Flow to 0.213.0
  Upgrade Flow to 0.212.0
  Upgrade Flow to 0.211.1
  Upgrade Flow to 0.211.0
  Upgrade Flow to 0.210.2
  Upgrade Flow to 0.210.1
  Upgrade Flow to 0.210.0
  Upgrade Flow to 0.209.0
  Upgrade Flow to 0.208.1
  Upgrade Flow to 0.208.0
  Switch Babel & ESLint parsers to hermes
  Upgrade Flow to 0.207.0
  Upgrade Flow to 0.206.0
  Upgrade Flow to 0.205.1
  Upgrade Flow to 0.205.0
  Upgrade Flow to 0.204.1
  Upgrade Flow to 0.204.0
  Upgrade Flow to 0.203.1
  Upgrade Flow to 0.203.0
  Upgrade Flow to 0.202.1
  Upgrade Flow to 0.202.0
  Remove all uses of the switch feature (given/when)
  Remove all uses of the smartmatch operator
  Link directly to the anchor in Markdown
  Update INSTALL.md's building static resources section (#3042)
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.

4 participants