Skip to content

Donations block: Update to utilise formatCurrency from @automattic/number-formatters #43418

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

Draft
wants to merge 2 commits into
base: trunk
Choose a base branch
from
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

Update donations block to utilise formatCurrency from @automattic/number-formatters
15 changes: 7 additions & 8 deletions projects/plugins/jetpack/extensions/blocks/donations/amount.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import formatCurrency, { CURRENCIES } from '@automattic/format-currency';
import { formatCurrency } from '@automattic/number-formatters';
import { RichText } from '@wordpress/block-editor';
import { useCallback, useEffect, useRef, useState } from '@wordpress/element';
import clsx from 'clsx';
Expand All @@ -14,7 +14,7 @@ const Amount = ( {
value = '',
} ) => {
const [ editedValue, setEditedValue ] = useState(
formatCurrency( value, currency, { symbol: '' } )
value ? formatCurrency( Number( value ), currency ) : null
);
const [ isFocused, setIsFocused ] = useState( false );
const [ isInvalid, setIsInvalid ] = useState( false );
Expand Down Expand Up @@ -62,7 +62,7 @@ const Amount = ( {
const onBlur = () => {
setIsFocused( false );
if ( ! editedValue ) {
setAmount( formatCurrency( defaultValue, currency, { symbol: '' } ) );
setAmount( defaultValue ? formatCurrency( Number( defaultValue ), currency ) : null );
}
};

Expand All @@ -78,11 +78,11 @@ const Amount = ( {
if ( isFocused || isInvalid ) {
return;
}
setEditedValue( formatCurrency( value, currency, { symbol: '' } ) );
setEditedValue( value ? formatCurrency( Number( value ), currency ) : null );
}, [ currency, isFocused, isInvalid, value ] );

useEffect( () => {
setAmount( formatCurrency( value, currency, { symbol: '' } ) );
setAmount( value ? formatCurrency( Number( value ), currency ) : null );
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ currency, value ] );

Expand All @@ -97,17 +97,16 @@ const Amount = ( {
onClick={ setFocus }
onKeyDown={ setFocus }
>
{ CURRENCIES[ currency ].symbol }
{ disabled ? (
<div className="donations__amount-value">
{ formatCurrency( value ? value : defaultValue, currency, { symbol: '' } ) }
{ formatCurrency( value ? Number( value ) : Number( defaultValue ), currency ) }
</div>
) : (
<RichText
allowedFormats={ [] }
aria-label={ label }
onChange={ amount => setAmount( amount, true ) }
placeholder={ formatCurrency( defaultValue, currency, { symbol: '' } ) }
placeholder={ defaultValue ? formatCurrency( Number( defaultValue ), currency ) : null }
ref={ richTextRef }
value={ editedValue }
withoutInteractiveFormatting
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import formatCurrency, { CURRENCIES } from '@automattic/format-currency';
import { formatCurrency } from '@automattic/number-formatters';
import { RichText } from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { minimumTransactionAmountForCurrency } from '../../../../shared/currencies';
Expand Down Expand Up @@ -148,14 +148,12 @@ export default {
<>
<RichText.Content tagName="p" value={ customAmountText } />
<div className="donations__amount donations__custom-amount">
{ CURRENCIES[ currency ].symbol }
<div
className="donations__amount-value"
data-currency={ currency }
data-empty-text={ formatCurrency(
minimumTransactionAmountForCurrency( currency ) * 100,
currency,
{ symbol: '' }
currency
) }
/>
</div>
Expand Down
6 changes: 2 additions & 4 deletions projects/plugins/jetpack/extensions/blocks/donations/view.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import formatCurrency from '@automattic/format-currency';
import { formatCurrency } from '@automattic/number-formatters';
import domReady from '@wordpress/dom-ready';
import { addQueryArgs, removeQueryArgs } from '@wordpress/url';
import { minimumTransactionAmountForCurrency, parseAmount } from '../../shared/currencies';
Expand Down Expand Up @@ -181,9 +181,7 @@ class JetpackDonations {
}

// Formats the entered amount.
input.innerHTML = formatCurrency( this.amount, input.dataset.currency, {
symbol: '',
} );
input.innerHTML = formatCurrency( Number( this.amount ), input.dataset.currency );
} );

input.addEventListener( 'input', () => this.updateAmountFromCustomAmountInput() );
Expand Down
3 changes: 3 additions & 0 deletions projects/plugins/jetpack/tools/check-block-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
'wp-compose',
'wp-element',
),
'donations' => array(
'wp-date',
),
'podcast-player' => array(
'lodash',
'react',
Expand Down
Loading