From 821f6db1ae9c4ed0eed043598e4689e886e1416e Mon Sep 17 00:00:00 2001 From: Adam Boro Date: Mon, 19 Jul 2021 19:17:26 +0200 Subject: [PATCH] feat(nrh): donations handling (#805) add regenerator-runtime - since WP 5.8, it's no longer added by default. --- package-lock.json | 1 + package.json | 1 + src/blocks/donate/edit.js | 59 ++++++++++++-------------------- src/blocks/donate/streamlined.js | 7 ++++ src/blocks/donate/view.php | 6 ---- 5 files changed, 31 insertions(+), 43 deletions(-) diff --git a/package-lock.json b/package-lock.json index 12a13c867..d6d2910df 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,6 +27,7 @@ "react": "^16.12.0", "redux": "^4.0.5", "redux-saga": "^1.1.3", + "regenerator-runtime": "^0.13.7", "swiper": "4.5.1" }, "devDependencies": { diff --git a/package.json b/package.json index ab0201a3b..ac16abcbd 100755 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/blocks/donate/edit.js b/src/blocks/donate/edit.js index 77590212b..032fda87a 100644 --- a/src/blocks/donate/edit.js +++ b/src/blocks/donate/edit.js @@ -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 }, () => { @@ -130,7 +129,6 @@ class Edit extends Component { currencySymbol, tiered, created, - force_manual: forceManual, } = settings; this.setState( { suggestedAmounts, @@ -138,7 +136,6 @@ class Edit extends Component { currencySymbol, tiered, created, - forceManual, isLoading: false, customDonationAmounts: { once: tiered ? 12 * suggestedAmounts[ 1 ] : 12 * suggestedAmountUntiered, @@ -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( { @@ -496,39 +484,36 @@ class Edit extends Component { render() { const { setAttributes } = this.props; - const { forceManual } = this.state; const { manual, campaign, defaultFrequency } = this.blockData(); return ( { this.renderPlaceholder() } { this.renderForm() } - { ! forceManual && ( - - - { ! manual && ( - -

- { __( - 'The Donate Block allows you to collect donations from readers. The fields are automatically defined based on your donation settings.', - 'newspack-blocks' - ) } -

+ + + { ! manual && ( + +

+ { __( + 'The Donate Block allows you to collect donations from readers. The fields are automatically defined based on your donation settings.', + 'newspack-blocks' + ) } +

- - { __( 'Edit donation settings.', 'newspack-blocks' ) } - -
- ) } -
- ) } + + { __( 'Edit donation settings.', 'newspack-blocks' ) } + +
+ ) } +
{ manual && ( - + { this.renderManualControls() } ) } diff --git a/src/blocks/donate/streamlined.js b/src/blocks/donate/streamlined.js index 06da9cf61..4be67f6df 100644 --- a/src/blocks/donate/streamlined.js +++ b/src/blocks/donate/streamlined.js @@ -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 @@ -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 ) { @@ -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 ); } diff --git a/src/blocks/donate/view.php b/src/blocks/donate/view.php index 0b54a0ea5..51aa8f7ec 100644 --- a/src/blocks/donate/view.php +++ b/src/blocks/donate/view.php @@ -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 );