Skip to content
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 7 commits into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
96 changes: 96 additions & 0 deletions extensions/blocks/donations/amount-input.js
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 ) => {
Copy link
Contributor

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let the server do the heavy lifting here

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.

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' ), '.' )
);
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
9 changes: 8 additions & 1 deletion extensions/blocks/donations/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ export default {
type: 'string',
default: 'USD',
},
amounts: {
type: 'array',
items: {
type: 'number',
},
default: [ 5, 15, 100 ],
},
oneTimePlanId: {
type: 'number',
default: null,
Expand Down Expand Up @@ -38,7 +45,7 @@ export default {
},
chooseAmountText: {
type: 'string',
default: __( 'Choose an amount (USD)', 'jetpack' ),
default: __( 'Choose an amount', 'jetpack' ),
},
customAmountText: {
type: 'string',
Expand Down
125 changes: 96 additions & 29 deletions extensions/blocks/donations/controls.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,107 @@
/**
* WordPress dependencies
*/
import { ExternalLink, PanelBody, ToggleControl } from '@wordpress/components';
import { BlockControls, InspectorControls } from '@wordpress/block-editor';
import {
Button,
Dashicon,
Dropdown,
ExternalLink,
MenuGroup,
MenuItem,
PanelBody,
ToggleControl,
ToolbarGroup,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { InspectorControls } from '@wordpress/block-editor';
import { DOWN } from '@wordpress/keycodes';

/**
* Internal dependencies
*/
import { SUPPORTED_CURRENCIES } from '../../shared/currencies';
import { CURRENCIES } from '@automattic/format-currency';

const Controls = props => {
const { attributes, setAttributes, products, siteSlug } = props;
const { monthlyPlanId, annuallyPlanId, showCustomAmount } = attributes;
const { currency, monthlyPlanId, annuallyPlanId, showCustomAmount } = attributes;

return (
<InspectorControls>
<PanelBody title={ __( 'Settings', 'jetpack' ) }>
<ToggleControl
checked={ !! monthlyPlanId }
onChange={ value =>
setAttributes( { monthlyPlanId: value ? products[ '1 month' ] : null } )
}
label={ __( 'Show monthly donations', 'jetpack' ) }
/>
<ToggleControl
checked={ !! annuallyPlanId }
onChange={ value =>
setAttributes( { annuallyPlanId: value ? products[ '1 year' ] : null } )
}
label={ __( 'Show annual donations', 'jetpack' ) }
/>
<ToggleControl
checked={ showCustomAmount }
onChange={ value => setAttributes( { showCustomAmount: value } ) }
label={ __( 'Show custom amount option', 'jetpack' ) }
/>
<ExternalLink href={ `https://wordpress.com/earn/payments/${ siteSlug }` }>
{ __( 'View donation earnings', 'jetpack' ) }
</ExternalLink>
</PanelBody>
</InspectorControls>
<>
<BlockControls>
<ToolbarGroup>
<Dropdown
contentClassName="jetpack-donations__currency-popover"
renderToggle={ ( { onToggle, isOpen } ) => {
const openOnArrowDown = event => {
if ( ! isOpen && event.keyCode === DOWN ) {
event.preventDefault();
event.stopPropagation();
onToggle();
}
};

return (
<Button
className="jetpack-donations__currency-toggle"
icon={
<>
{ CURRENCIES[ currency ].symbol + ' - ' + currency }
<Dashicon icon="arrow-down" />
</>
}
label={ __( 'Change currency', 'jetpack' ) }
onClick={ onToggle }
onKeyDown={ openOnArrowDown }
/>
);
} }
renderContent={ ( { onClose } ) => (
<MenuGroup>
{ Object.keys( SUPPORTED_CURRENCIES ).map( ccy => (
<MenuItem
isSelected={ ccy === currency }
onClick={ () => {
setAttributes( { currency: ccy } );
onClose();
} }
key={ `jetpack-donations-currency-${ ccy }` }
>
{ CURRENCIES[ ccy ].symbol + ' - ' + ccy }
</MenuItem>
) ) }
</MenuGroup>
) }
/>
</ToolbarGroup>
</BlockControls>
<InspectorControls>
<PanelBody title={ __( 'Settings', 'jetpack' ) }>
<ToggleControl
checked={ !! monthlyPlanId }
onChange={ value =>
setAttributes( { monthlyPlanId: value ? products[ '1 month' ] : null } )
}
label={ __( 'Show monthly donations', 'jetpack' ) }
/>
<ToggleControl
checked={ !! annuallyPlanId }
onChange={ value =>
setAttributes( { annuallyPlanId: value ? products[ '1 year' ] : null } )
}
label={ __( 'Show annual donations', 'jetpack' ) }
/>
<ToggleControl
checked={ showCustomAmount }
onChange={ value => setAttributes( { showCustomAmount: value } ) }
label={ __( 'Show custom amount option', 'jetpack' ) }
/>
<ExternalLink href={ `https://wordpress.com/earn/payments/${ siteSlug }` }>
{ __( 'View donation earnings', 'jetpack' ) }
</ExternalLink>
</PanelBody>
</InspectorControls>
</>
);
};

Expand Down
1 change: 0 additions & 1 deletion extensions/blocks/donations/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ const Edit = props => {
return (
<Tabs
{ ...props }
className={ className }
products={ products }
shouldUpgrade={ shouldUpgrade }
siteSlug={ siteSlug }
Expand Down
36 changes: 29 additions & 7 deletions extensions/blocks/donations/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,32 @@
background-color: $white;
color: $dark-gray-800;
border: 1px solid $light-gray-700;
cursor: default;
}

.donations__custom-amount {
margin-bottom: 30px;
.donations__amount .block-editor-rich-text__editable {
display: inline-block;
text-align: left;

&:hover {
box-shadow: 0 0 0 1px #fff, 0 0 0 3px $blue-wordpress-700;
outline: 2px solid transparent;
outline-offset: -2px;
}
}

.donations__custom-amount .wp-block-button__link {
cursor: default;
.donations__amount.wp-block-button:not(.has-text-color):not(.is-style-outline) [data-rich-text-placeholder]:after {
color: $light-gray-700;
}

.donations__custom-amount {
margin-bottom: 30px;
}

.donations__custom-amount-placeholder {
.donations__custom-amount .block-editor-rich-text__editable {
pointer-events: none;
margin-left: 8px;
color: $light-gray-700;
padding-right: 40px;
min-width: 60px;
}

.donations__separator {
Expand All @@ -90,3 +102,13 @@
max-width: none;
}
}

.jetpack-donations__currency-toggle {
font-weight: bold;
line-height: 100%;
width: max-content;
}

.jetpack-donations__currency-popover .components-popover__content {
min-width: 120px;
}
Loading