diff --git a/changelog/fix-multi-currency-unload-confirmation-dialog b/changelog/fix-multi-currency-unload-confirmation-dialog new file mode 100644 index 00000000000..2508369174e --- /dev/null +++ b/changelog/fix-multi-currency-unload-confirmation-dialog @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +fix: ensure the confirmation dialog doesn't show up on the multi-currency UI diff --git a/includes/multi-currency/Settings.php b/includes/multi-currency/Settings.php index 8fe0a4aafcd..ea96ed42924 100644 --- a/includes/multi-currency/Settings.php +++ b/includes/multi-currency/Settings.php @@ -80,7 +80,7 @@ public function wcpay_multi_currency_settings_page() { // Hide original save button. $GLOBALS['hide_save_button'] = true; ?> -
+
{ const [ currencyCodeToShowSettingsFor, - setCurrencyCodeToShowSettingsFor, + _setCurrencyCodeToShowSettingsFor, ] = useState( null ); + const [ isCurrentScreenDirty, setIsCurrentScreenDirty ] = useState( false ); + useEffect( + useConfirmNavigation( () => { + if ( isCurrentScreenDirty ) { + return __( + 'There are unsaved changes on this page. Are you sure you want to leave and discard the unsaved changes?', + 'woocommerce-payments' + ); + } + } ), + [ isCurrentScreenDirty ] + ); + useEffect( () => { + setIsCurrentScreenDirty( false ); + }, [ currencyCodeToShowSettingsFor ] ); + const setCurrencyCodeToShowSettingsFor = useCallback( + ( currency ) => { + if ( + confirm( + __( + 'There are unsaved changes on this page. Are you sure you want to leave and discard the unsaved changes?', + 'woocommerce-payments' + ) + ) + ) { + _setCurrencyCodeToShowSettingsFor( currency ); + } + }, + [ isCurrentScreenDirty ] + ); return ( { ! currencyCodeToShowSettingsFor ? ( diff --git a/includes/multi-currency/client/settings/multi-currency/store-settings/__tests__/index.test.js b/includes/multi-currency/client/settings/multi-currency/store-settings/__tests__/index.test.js index 0aadcd7720d..df85e306e85 100644 --- a/includes/multi-currency/client/settings/multi-currency/store-settings/__tests__/index.test.js +++ b/includes/multi-currency/client/settings/multi-currency/store-settings/__tests__/index.test.js @@ -9,6 +9,7 @@ import { render, screen, fireEvent } from '@testing-library/react'; */ import { useStoreSettings } from 'multi-currency/data'; import StoreSettings from '..'; +import MultiCurrencySettingsContext from 'multi-currency/context'; jest.mock( 'multi-currency/data', () => ( { useStoreSettings: jest.fn(), @@ -29,7 +30,13 @@ useStoreSettings.mockReturnValue( { } ); const createContainer = () => { - const { container } = render( ); + const { container } = render( + null } } + > + + + ); return container; }; @@ -60,7 +67,7 @@ describe( 'Multi-Currency store settings', () => { } ); } ); - test( 'store settings button is diabled by default', () => { + test( 'store settings button is disabled by default', () => { createContainer(); expect( screen.getByRole( 'button', { name: /Save changes/ } ) diff --git a/includes/multi-currency/client/settings/multi-currency/store-settings/index.js b/includes/multi-currency/client/settings/multi-currency/store-settings/index.js index 3fc628a80b3..9b2b18e5e27 100644 --- a/includes/multi-currency/client/settings/multi-currency/store-settings/index.js +++ b/includes/multi-currency/client/settings/multi-currency/store-settings/index.js @@ -1,7 +1,12 @@ /** * External dependencies */ -import React, { useEffect, useState } from 'react'; +import React, { useContext, useEffect, useState } from 'react'; +import { Button } from 'wcpay/components/wp-components-wrapped/components/button'; +import { Card } from 'wcpay/components/wp-components-wrapped/components/card'; +import { CardBody } from 'wcpay/components/wp-components-wrapped/components/card-body'; +import { CheckboxControl } from 'wcpay/components/wp-components-wrapped/components/checkbox-control'; +import { ExternalLink } from 'wcpay/components/wp-components-wrapped/components/external-link'; import { __, sprintf } from '@wordpress/i18n'; import { createInterpolateElement } from '@wordpress/element'; @@ -19,6 +24,7 @@ import { SettingsSection, } from 'multi-currency/interface/components'; import PreviewModal from 'multi-currency/components/preview-modal'; +import MultiCurrencySettingsContext from 'multi-currency/context'; const StoreSettingsDescription = () => ( <> @@ -60,7 +66,10 @@ const StoreSettings = () => { const [ isPreviewModalOpen, setPreviewModalOpen ] = useState( false ); - const [ isDirty, setIsDirty ] = useState( false ); + const { + setIsCurrentScreenDirty: setIsDirty, + isCurrentScreenDirty: isDirty, + } = useContext( MultiCurrencySettingsContext ); useEffect( () => { if ( Object.keys( storeSettings ).length ) { diff --git a/includes/multi-currency/client/settings/single-currency/__tests__/__snapshots__/index.test.js.snap b/includes/multi-currency/client/settings/single-currency/__tests__/__snapshots__/index.test.js.snap index 4d67bad6e30..01c562b22f1 100644 --- a/includes/multi-currency/client/settings/single-currency/__tests__/__snapshots__/index.test.js.snap +++ b/includes/multi-currency/client/settings/single-currency/__tests__/__snapshots__/index.test.js.snap @@ -777,7 +777,6 @@ exports[`Single currency settings screen Page renders correctly 1`] = ` >