Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog/fix-multi-currency-unload-confirmation-dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

fix: ensure the confirmation dialog doesn't show up on the multi-currency UI
2 changes: 1 addition & 1 deletion includes/multi-currency/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function wcpay_multi_currency_settings_page() {
// Hide original save button.
$GLOBALS['hide_save_button'] = true;
?>
<div id="wcpay_multi_currency_settings_container" aria-describedby="wcpay_multi_currency_settings_container-description"></div>
<div id="wcpay_multi_currency_settings_container" aria-describedby="wcpay_multi_currency_settings_container-description" class="wc-settings-prevent-change-event"></div>
<?php
}

Expand Down
38 changes: 36 additions & 2 deletions includes/multi-currency/client/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/**
* External dependencies
*/
import React, { useState } from 'react';
import React, { useState, useEffect, useCallback } from 'react';
import ReactDOM from 'react-dom';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
Expand All @@ -11,18 +12,51 @@ import MultiCurrencySettings from './settings/multi-currency';
import SingleCurrencySettings from './settings/single-currency';
import MultiCurrencySettingsContext from './context';
import UnbundledWpComponentsProvider from 'wcpay/wordpress-components-context/unbundled-wp-components-provider';
import useConfirmNavigation from 'wcpay/utils/use-confirm-navigation';

const MultiCurrencySettingsPage = () => {
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 (
<MultiCurrencySettingsContext.Provider
value={ {
currencyCodeToShowSettingsFor,
setCurrencyCodeToShowSettingsFor,
isCurrentScreenDirty,
setIsCurrentScreenDirty,
} }
>
{ ! currencyCodeToShowSettingsFor ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -29,7 +30,13 @@ useStoreSettings.mockReturnValue( {
} );

const createContainer = () => {
const { container } = render( <StoreSettings /> );
const { container } = render(
<MultiCurrencySettingsContext.Provider
value={ { setIsCurrentScreenDirty: () => null } }
>
<StoreSettings />
</MultiCurrencySettingsContext.Provider>
);
return container;
};

Expand Down Expand Up @@ -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/ } )
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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 = () => (
<>
Expand Down Expand Up @@ -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 ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,6 @@ exports[`Single currency settings screen Page renders correctly 1`] = `
>
<button
class="components-button is-primary"
disabled=""
type="button"
>
Save changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ useStoreSettings.mockReturnValue( {
const containerContext = {
currencyCodeToShowSettingsFor: 'EUR',
setCurrencyCodeToShowSettingsFor: jest.fn(),
isCurrentScreenDirty: true,
setIsCurrentScreenDirty: jest.fn(),
};

const getContainer = () => {
Expand Down
62 changes: 39 additions & 23 deletions includes/multi-currency/client/settings/single-currency/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* External dependencies
*/
import React, { useContext, useEffect, useState } from 'react';
import React, { useContext, useEffect, useState, useCallback } from 'react';
import { dateI18n } from '@wordpress/date';
import { sprintf, __ } from '@wordpress/i18n';
import moment from 'moment';
Expand Down Expand Up @@ -39,6 +39,30 @@ import {
} from 'multi-currency/interface/components';
import interpolateComponents from '@automattic/interpolate-components';

const CurrencyPreviewDescription = ( {
storeCurrency,
targetCurrency,
isLoading,
} ) => (
<>
<h2>{ __( 'Preview', 'woocommerce-payments' ) }</h2>
<p>
{ ! isLoading
? sprintf(
__(
'Enter a price in your default currency (%s) to ' +
'see it converted to %s using the ' +
'exchange rate and formatting rules above.',
'woocommerce-payments'
),
storeCurrency,
targetCurrency
)
: '' }
</p>
</>
);

const CurrencySettingsDescription = () => (
<>
<h2>{ __( 'Currency settings', 'woocommerce-payments' ) }</h2>
Expand All @@ -57,10 +81,10 @@ const SingleCurrencySettings = () => {
const {
currencyCodeToShowSettingsFor: currency,
setCurrencyCodeToShowSettingsFor,
setIsCurrentScreenDirty: setIsDirty,
isCurrentScreenDirty: isDirty,
} = useContext( MultiCurrencySettingsContext );

const [ isDirty, setIsDirty ] = useState( false );

const { currencies } = useCurrencies();
const { enabledCurrencies } = useEnabledCurrencies();
const { storeSettings } = useStoreSettings();
Expand Down Expand Up @@ -132,25 +156,6 @@ const SingleCurrencySettings = () => {
moment.unix( targetCurrency.last_updated ).toISOString()
)
: '';
const CurrencyPreviewDescription = () => (
<>
<h2>{ __( 'Preview', 'woocommerce-payments' ) }</h2>
<p>
{ ! isLoading
? sprintf(
__(
'Enter a price in your default currency (%s) to ' +
'see it converted to %s using the ' +
'exchange rate and formatting rules above.',
'woocommerce-payments'
),
storeCurrency.name,
targetCurrency.name
)
: '' }
</p>
</>
);

const saveSingleCurrencySettings = () => {
setIsSaving( true );
Expand All @@ -172,6 +177,17 @@ const SingleCurrencySettings = () => {
setIsDirty( false );
};

const currencyPreviewDescription = useCallback(
() => (
<CurrencyPreviewDescription
storeCurrency={ storeCurrency.name }
targetCurrency={ targetCurrency.name }
isLoading={ isLoading }
/>
),
[ storeCurrency.name, targetCurrency.name, isLoading ]
);

return (
<div className="single-currency-settings">
<SettingsLayout>
Expand Down Expand Up @@ -387,7 +403,7 @@ const SingleCurrencySettings = () => {
</Card>
</LoadableBlock>
</SettingsSection>
<SettingsSection description={ CurrencyPreviewDescription }>
<SettingsSection description={ currencyPreviewDescription }>
<LoadableBlock isLoading={ isLoading } numLines={ 8 }>
<CurrencyPreview
className="single-currency-settings-currency-preview"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useCurrencies, useStoreSettings } from 'multi-currency/data';
import { useSettings, useMultiCurrency } from 'multi-currency/interface/data';
import { WizardTaskContext } from 'multi-currency/interface/functions';
import StoreSettingsTask from '..';
import MultiCurrencySettingsContext from 'multi-currency/context';

jest.mock( 'multi-currency/data', () => ( {
useStoreSettings: jest.fn(),
Expand Down Expand Up @@ -62,7 +63,14 @@ const createContainer = () => {
<WizardTaskContext.Provider
value={ { isActive: true, setCompleted: setCompletedMock } }
>
<StoreSettingsTask />
<MultiCurrencySettingsContext.Provider
value={ {
setIsCurrentScreenDirty: () => null,
isCurrentScreenDirty: true,
} }
>
<StoreSettingsTask />
</MultiCurrencySettingsContext.Provider>
</WizardTaskContext.Provider>
);
return container;
Expand Down
Loading