-
Notifications
You must be signed in to change notification settings - Fork 813
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
Donations block: Make currency and amounts editable #16593
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
56c2599
Donations block: Make currency and amounts editable
mmtr 1f5cf89
Edit currency in block toolbar
mmtr 0108276
Remove currency code from choose amount text
mmtr 78a4b2b
Make amounts directly editable in editor
mmtr 214f15a
Address feedback from PR:
mmtr f05641e
Show default amount on placeholder
mmtr c5c6769
Remove unused prop
mmtr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { CURRENCIES } from '@automattic/format-currency'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { RichText } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { minimumTransactionAmountForCurrency } from '../../shared/currencies'; | ||
|
||
const parseAmount = ( amount, currency ) => { | ||
if ( ! amount ) { | ||
return null; | ||
} | ||
|
||
amount = parseFloat( | ||
amount | ||
// Remove any thousand grouping separator. | ||
.replace( new RegExp( '\\' + CURRENCIES[ currency ].grouping, 'g' ), '' ) | ||
// Replace the localized decimal separator with a dot (the standard decimal separator in float numbers). | ||
.replace( new RegExp( '\\' + CURRENCIES[ currency ].decimal, 'g' ), '.' ) | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it's well formatted, it's fine to leave this as a string value on the client, and let the server parse. But okay to defer here too, if other pieces need heavy refactoring to do this. |
||
|
||
if ( isNaN( amount ) ) { | ||
return null; | ||
} | ||
|
||
return amount; | ||
}; | ||
|
||
const setAmount = ( | ||
amount, | ||
currency, | ||
tier, | ||
amounts, | ||
editedAmounts, | ||
setAttributes, | ||
setEditedAmounts | ||
) => { | ||
const newEditedAmounts = [ ...editedAmounts ]; | ||
newEditedAmounts[ tier ] = amount; | ||
setEditedAmounts( newEditedAmounts ); | ||
|
||
const parsedAmount = parseAmount( amount, currency ); | ||
if ( parsedAmount && parsedAmount >= minimumTransactionAmountForCurrency( currency ) ) { | ||
const newAmounts = [ ...amounts ]; | ||
newAmounts[ tier ] = parsedAmount; | ||
setAttributes( { amounts: newAmounts } ); | ||
} | ||
}; | ||
|
||
const AmountInput = ( { | ||
amount = '', | ||
amounts = null, | ||
className = '', | ||
currency = null, | ||
editedAmounts = null, | ||
label = '', | ||
placeholder = '', | ||
setAttributes = null, | ||
setEditedAmounts = null, | ||
tier = null, | ||
} ) => ( | ||
<div className={ `wp-block-button donations__amount ${ className }` }> | ||
<div className="wp-block-button__link"> | ||
{ CURRENCIES[ currency ].symbol } | ||
<RichText | ||
value={ amount } | ||
placeholder={ placeholder } | ||
aria-label={ label } | ||
onChange={ value => | ||
setAmount( | ||
value, | ||
currency, | ||
tier, | ||
amounts, | ||
editedAmounts, | ||
setAttributes, | ||
setEditedAmounts | ||
) | ||
} | ||
multiline={ false } | ||
withoutInteractiveFormatting | ||
allowedFormats={ [] } | ||
keepPlaceholderOnFocus={ true } | ||
/> | ||
</div> | ||
</div> | ||
); | ||
|
||
export default AmountInput; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this gets complicated, it wouldn't be unreasonable to let the server do the heavy lifting here. Note that there will be corner cases with negative values, random characters, and so on. Unit tests will be helpful for both a client/server library
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean with an API call every time an amount changes? Or as part of the server render callback?
A new API endpoint seems a bit complex at this time, and while we still need to validate the amounts in the server render callback (a user could enter anything when in the code editor mode which will bypass any client check), not including any validation in the client will leave users unaware of invalid amounts and might get confused when they see the published post does not contain the amount they entered.