Skip to content

Conversation

@joshuatf
Copy link
Contributor

Submission Review Guidelines:

Changes proposed in this Pull Request:

Checks for unsaved changes before leaving the product editor.

Note that some false positives were shown for making edits based on the entity data store and this PR corrects those:

  • We don't count content or blocks as part of changes since we aren't a typical content editor
  • We check for changes to the sale price before resetting sale dates to avoid making edits to these fields
Screen Shot 2023-05-24 at 10 11 56 AM

Closes #38253 .

How to test the changes in this Pull Request:

Using the WooCommerce Testing Instructions Guide, include your detailed testing instructions:

  1. Go to /wp-admin/tools.php?page=woocommerce-admin-test-helper
  2. Under Features tab make sure to enable product-block-editor
  3. Then visit /wp-admin/admin.php?page=wc-admin&path=/add-product
  4. Don't make any changes and navigate away from the page
  5. Make sure no confirmation for unsaved changes is shown
  6. Navigate back to the add product page
  7. Make some changes in the editor
  8. Navigate away to another page
  9. Make sure that the confirmation for unsaved changes is shown
  10. Navigate between product tabs and make sure that this does not trigger the unsaved changes confirmation

@joshuatf joshuatf requested a review from a team May 24, 2023 17:23
@joshuatf joshuatf self-assigned this May 24, 2023
@github-actions
Copy link
Contributor

Hi , @woocommerce/mothra

Apart from reviewing the code changes, please make sure to review the testing instructions as well.

You can follow this guide to find out what good testing instructions should look like:
https://github.com/woocommerce/woocommerce/wiki/Writing-high-quality-testing-instructions

@github-actions
Copy link
Contributor

github-actions bot commented May 24, 2023

Test Results Summary

Commit SHA: 8a1d935

Test 🧪Passed ✅Failed 🚨Broken 🚧Skipped ⏭️Unknown ❔Total 📊Duration ⏱️
API Tests25900202610m 48s
E2E Tests1940010020418m 31s

To view the full API test report, click here.
To view the full E2E test report, click here.
To view all test reports, visit the WooCommerce Test Reports Dashboard.

useEffect( () => {
if ( ! isSalePriceGreaterThanZero ) {
if (
edits.hasOwnProperty( 'sale_price' ) &&
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the following would be more readable...

'sale_price' in edits

Now, this is slightly different than using hasOwnProperty, as in will check the prototype chain. But, I personally think the readability gain makes it worth it.

If not, maybe we could return a function such as isFieldEdited from useProductEdits that could be used as follows:

isFieldEdited( 'sale_price' )

Copy link
Contributor

Choose a reason for hiding this comment

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

Or...

hasEdit( 'sale_price' )


return {
hasEdits: Object.keys( edits ).length > 0,
edits,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think returning a few convenience functions instead of edits directly could make code using this easier to read...

hasEdit( fieldName )
getEditedValue( fieldName )

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea! Updated to hasEdit in 8a1d935.

I've removed the edits for now since they're not being used elsewhere. We can introduce them again later if they are needed and decide on any convenience methods needed at that time.

useEffect( () => {
if ( ! isSalePriceGreaterThanZero ) {
if (
edits.hasOwnProperty( 'sale_price' ) &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Or...

hasEdit( 'sale_price' )

Copy link
Contributor

@mattsherman mattsherman left a comment

Choose a reason for hiding this comment

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

Tested and works as expected, with one minor UX inconsistency (see below).

Made a suggestion that I think could improve the readability of useProductEdits.

UX inconsistency...

Moving to a different WC page within Products results in the following modal, centered on the display:

Screenshot 2023-05-24 at 15 54 28

Moving to a different WC page outside of Products results in the following modal, attached at the top of the page:

Screenshot 2023-05-24 at 15 53 46

@joshuatf
Copy link
Contributor Author

Thanks for the review, @mattsherman! Updated based on your suggestion.

Moving to a different WC page within Products results in the following modal, centered on the display:

The difference you're seeing is probably from this page to other pages under our React router vs non-Reactified pages. I'm not sure if this is something we can change since the dialog between React pages is using the browser's window.confirm method while the other uses the underlying OS dialog. See this file for more context.

onbeforeunload only accepts a string for security reasons, so we can't really change that since some browsers ignore window.confirm inside of its callback. The other option would be to update to use the native OS prompt within the React router navigation, but I'm not sure if it's possible to trigger this. cc @mdperez86 in case you've already looked into this before I create an issue.

@mattsherman
Copy link
Contributor

Thanks for the review, @mattsherman! Updated based on your suggestion.

Moving to a different WC page within Products results in the following modal, centered on the display:

The difference you're seeing is probably from this page to other pages under our React router vs non-Reactified pages. I'm not sure if this is something we can change since the dialog between React pages is using the browser's window.confirm method while the other uses the underlying OS dialog. See this file for more context.

onbeforeunload only accepts a string for security reasons, so we can't really change that since some browsers ignore window.confirm inside of its callback. The other option would be to update to use the native OS prompt within the React router navigation, but I'm not sure if it's possible to trigger this. cc @mdperez86 in case you've already looked into this before I create an issue.

Ah, thanks for the explanation. Makes sense.

Copy link
Contributor

@mattsherman mattsherman 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 making the hasEdit upgrade. Re-reviewed and tested. LGTM. :shipit:

@joshuatf joshuatf merged commit a5e3637 into trunk May 31, 2023
@joshuatf joshuatf deleted the add/38253 branch May 31, 2023 17:14
@github-actions github-actions bot added this to the 7.9.0 milestone May 31, 2023
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.

Product Block Editor: Add redirect hook to show warning if users leave the product page with existing edits

3 participants