Skip to content

Commit

Permalink
Keep in sync the donate buttons labels across all intervals
Browse files Browse the repository at this point in the history
  • Loading branch information
mmtr committed Aug 5, 2020
1 parent acda908 commit 672d7d2
Showing 1 changed file with 29 additions and 27 deletions.
56 changes: 29 additions & 27 deletions extensions/blocks/donations/tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ const Tab = ( { activeTab, attributes, setAttributes } ) => {
customAmountText,
} = attributes;

const [ previousCurrency, setPreviousCurrency ] = useState( currency );
const minAmount = minimumTransactionAmountForCurrency( currency );
const [ defaultAmounts, setDefaultAmounts ] = useState( [
minAmount * 10, // 1st tier (USD 5)
minAmount * 30, // 2nd tier (USD 15)
minAmount * 200, // 3rd tier (USD 100)
] );

const donationAttributes = {
'one-time': 'oneTimeDonation',
'1 month': 'monthlyDonation',
Expand All @@ -49,42 +41,52 @@ const Tab = ( { activeTab, attributes, setAttributes } ) => {
} );
};

const amounts = getDonationValue( 'amounts' );

const setAmount = ( amount, tier ) => {
const newAmounts = [ ...amounts ];
newAmounts[ tier ] = amount;
setDonationValue( 'amounts', newAmounts );
};

// Updates the amounts whenever there are new defaults due to a currency change.
const [ previousCurrency, setPreviousCurrency ] = useState( currency );
const minAmount = minimumTransactionAmountForCurrency( currency );
const defaultAmounts = [
minAmount * 10, // 1st tier (USD 5)
minAmount * 30, // 2nd tier (USD 15)
minAmount * 200, // 3rd tier (USD 100)
];
useEffect( () => {
if ( previousCurrency === currency ) {
return;
}
setPreviousCurrency( currency );

const newDefaultAmounts = [
minAmount * 10, // 1st tier (USD 5)
minAmount * 30, // 2nd tier (USD 15)
minAmount * 200, // 3rd tier (USD 100)
];
setDefaultAmounts( newDefaultAmounts );
setAttributes( {
oneTimeDonation: { ...oneTimeDonation, amounts: newDefaultAmounts },
monthlyDonation: { ...monthlyDonation, amounts: newDefaultAmounts },
annualDonation: { ...annualDonation, amounts: newDefaultAmounts },
oneTimeDonation: { ...oneTimeDonation, amounts: defaultAmounts },
monthlyDonation: { ...monthlyDonation, amounts: defaultAmounts },
annualDonation: { ...annualDonation, amounts: defaultAmounts },
} );
}, [
currency,
previousCurrency,
minAmount,
defaultAmounts,
oneTimeDonation,
monthlyDonation,
annualDonation,
setAttributes,
] );

const amounts = getDonationValue( 'amounts' );

const setAmount = ( amount, tier ) => {
const newAmounts = [ ...amounts ];
newAmounts[ tier ] = amount;
setDonationValue( 'amounts', newAmounts );
};

// Keeps in sync the donate buttons labels across all intervals once the default value is overridden in one of them.
const setButtonText = buttonText => {
setAttributes( {
oneTimeDonation: { ...oneTimeDonation, buttonText: buttonText },
monthlyDonation: { ...monthlyDonation, buttonText: buttonText },
annualDonation: { ...annualDonation, buttonText: buttonText },
} );
};

return (
<div className="donations__tab">
<RichText
Expand Down Expand Up @@ -144,7 +146,7 @@ const Tab = ( { activeTab, attributes, setAttributes } ) => {
className="wp-block-button__link"
placeholder={ __( 'Write a message…', 'jetpack' ) }
value={ getDonationValue( 'buttonText' ) }
onChange={ value => setDonationValue( 'buttonText', value ) }
onChange={ value => setButtonText( value ) }
/>
</div>
</div>
Expand Down

0 comments on commit 672d7d2

Please sign in to comment.