Skip to content

Commit

Permalink
feat(nrh): donations handling (#805)
Browse files Browse the repository at this point in the history
add regenerator-runtime - since WP 5.8, it's no longer added by default.
  • Loading branch information
adekbadek authored Jul 19, 2021
1 parent a37ff23 commit 821f6db
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 43 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"react": "^16.12.0",
"redux": "^4.0.5",
"redux-saga": "^1.1.3",
"regenerator-runtime": "^0.13.7",
"swiper": "4.5.1"
},
"scripts": {
Expand Down
59 changes: 22 additions & 37 deletions src/blocks/donate/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ class Edit extends Component {
};

getSettings() {
const { attributes, setAttributes } = this.props;
const path = '/newspack/v1/wizard/newspack-donations-wizard/donation';

this.setState( { isLoading: true }, () => {
Expand All @@ -130,15 +129,13 @@ class Edit extends Component {
currencySymbol,
tiered,
created,
force_manual: forceManual,
} = settings;
this.setState( {
suggestedAmounts,
suggestedAmountUntiered,
currencySymbol,
tiered,
created,
forceManual,
isLoading: false,
customDonationAmounts: {
once: tiered ? 12 * suggestedAmounts[ 1 ] : 12 * suggestedAmountUntiered,
Expand All @@ -147,15 +144,6 @@ class Edit extends Component {
},
activeTier: 1,
} );
if ( forceManual ) {
setAttributes( { manual: true } );
if (
attributes.suggestedAmounts.every( amount => amount === 0 ) &&
0 === attributes.suggestedAmountUntiered
) {
setAttributes( { suggestedAmounts, suggestedAmountUntiered } );
}
}
} )
.catch( error => {
this.setState( {
Expand Down Expand Up @@ -496,39 +484,36 @@ class Edit extends Component {

render() {
const { setAttributes } = this.props;
const { forceManual } = this.state;
const { manual, campaign, defaultFrequency } = this.blockData();
return (
<Fragment>
{ this.renderPlaceholder() }
{ this.renderForm() }
<InspectorControls>
{ ! forceManual && (
<PanelBody>
<ToggleControl
key="manual"
checked={ manual }
onChange={ this.manualChanged }
label={ __( 'Configure manually', 'newspack-blocks' ) }
/>
{ ! manual && (
<Fragment>
<p>
{ __(
'The Donate Block allows you to collect donations from readers. The fields are automatically defined based on your donation settings.',
'newspack-blocks'
) }
</p>
<PanelBody>
<ToggleControl
key="manual"
checked={ manual }
onChange={ this.manualChanged }
label={ __( 'Configure manually', 'newspack-blocks' ) }
/>
{ ! manual && (
<Fragment>
<p>
{ __(
'The Donate Block allows you to collect donations from readers. The fields are automatically defined based on your donation settings.',
'newspack-blocks'
) }
</p>

<ExternalLink href="/wp-admin/admin.php?page=newspack-reader-revenue-wizard#/donations">
{ __( 'Edit donation settings.', 'newspack-blocks' ) }
</ExternalLink>
</Fragment>
) }
</PanelBody>
) }
<ExternalLink href="/wp-admin/admin.php?page=newspack-reader-revenue-wizard#/donations">
{ __( 'Edit donation settings.', 'newspack-blocks' ) }
</ExternalLink>
</Fragment>
) }
</PanelBody>
{ manual && (
<PanelBody title={ ! forceManual && __( 'Manual Settings', 'newspack-blocks' ) }>
<PanelBody title={ __( 'Manual Settings', 'newspack-blocks' ) }>
{ this.renderManualControls() }
</PanelBody>
) }
Expand Down
7 changes: 7 additions & 0 deletions src/blocks/donate/streamlined.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { __, sprintf } from '@wordpress/i18n';
* External dependencies
*/
import { loadStripe } from '@stripe/stripe-js';
import 'regenerator-runtime'; // Required in WP >=5.8.

/**
* Internal dependencies
Expand Down Expand Up @@ -62,6 +63,9 @@ const renderMessages = ( messages, el, type = 'error' ) => {
if ( formValues.amount === 'other' ) {
formValues.amount = formValues[ `${ valueKey }_other` ];
}
if ( ! formValues.amount ) {
formValues.amount = formValues[ `${ valueKey }_untiered` ];
}

const validationErrors = Object.values( validateFormData( formValues ) );
if ( validationErrors.length > 0 ) {
Expand Down Expand Up @@ -90,6 +94,9 @@ const renderMessages = ( messages, el, type = 'error' ) => {
} ),
} );
const chargeResultData = await chargeResult.json();
if ( chargeResultData.data?.status !== 200 && chargeResultData.message ) {
renderMessages( [ chargeResultData.message ], messagesEl );
}
if ( chargeResultData.error ) {
renderMessages( [ chargeResultData.error ], messagesEl );
}
Expand Down
6 changes: 0 additions & 6 deletions src/blocks/donate/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,6 @@ function newspack_blocks_render_block_donate( $attributes ) {
return '';
}

$manual = isset( $attributes['manual'] ) ? $attributes['manual'] : false;

if ( 'nrh' === $settings['platform'] && ! $manual ) {
return '';
}

/* If block is in "manual" mode, override certain state properties with values stored in attributes */
if ( $attributes['manual'] ?? false ) {
$settings = array_merge( $settings, $attributes );
Expand Down

0 comments on commit 821f6db

Please sign in to comment.