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

Simple Payments: pre-populate attributes when converting from classic block #11731

Merged
merged 3 commits into from
Apr 2, 2019
Merged
Changes from all 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
44 changes: 18 additions & 26 deletions extensions/blocks/simple-payments/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { __, _n, sprintf } from '@wordpress/i18n';
import { Component } from '@wordpress/element';
import { compose, withInstanceId } from '@wordpress/compose';
import { dispatch, withSelect } from '@wordpress/data';
import { get, isEqual, pick, trimEnd } from 'lodash';
import { get, isEmpty, isEqual, pick, trimEnd } from 'lodash';
import { getCurrencyDefaults } from '@automattic/format-currency';
import {
Disabled,
Expand Down Expand Up @@ -89,34 +89,26 @@ class SimplePaymentsEdit extends Component {
* When subsequent saves occur, we should avoid injecting attributes so that we do not
* overwrite changes that the user has made with stale state from the previous save.
*/
if ( ! this.shouldInjectPaymentAttributes ) {

const { simplePayment } = this.props;
if ( ! this.shouldInjectPaymentAttributes || isEmpty( simplePayment ) ) {
return;
}

const { attributes, setAttributes, simplePayment } = this.props;
const {
content,
currency,
email,
featuredMediaId,
multiple,
price,
productId,
title,
} = attributes;
const { attributes, setAttributes } = this.props;
const { content, currency, email, featuredMediaId, multiple, price, title } = attributes;

setAttributes( {
content: get( simplePayment, [ 'content', 'raw' ], content ),
currency: get( simplePayment, [ 'meta', 'spay_currency' ], currency ),
email: get( simplePayment, [ 'meta', 'spay_email' ], email ),
featuredMediaId: get( simplePayment, [ 'featured_media' ], featuredMediaId ),
multiple: Boolean( get( simplePayment, [ 'meta', 'spay_multiple' ], Boolean( multiple ) ) ),
price: get( simplePayment, [ 'meta', 'spay_price' ], price || undefined ),
title: get( simplePayment, [ 'title', 'raw' ], title ),
} );

if ( productId && simplePayment ) {
setAttributes( {
content: get( simplePayment, [ 'content', 'raw' ], content ),
currency: get( simplePayment, [ 'meta', 'spay_currency' ], currency ),
email: get( simplePayment, [ 'meta', 'spay_email' ], email ),
featuredMediaId: get( simplePayment, [ 'featured_media' ], featuredMediaId ),
multiple: Boolean( get( simplePayment, [ 'meta', 'spay_multiple' ], Boolean( multiple ) ) ),
price: get( simplePayment, [ 'meta', 'spay_price' ], price || undefined ),
title: get( simplePayment, [ 'title', 'raw' ], title ),
} );
this.shouldInjectPaymentAttributes = ! this.shouldInjectPaymentAttributes;
}
this.shouldInjectPaymentAttributes = ! this.shouldInjectPaymentAttributes;
}

toApi() {
Expand Down Expand Up @@ -420,7 +412,7 @@ class SimplePaymentsEdit extends Component {
* The only disabled state that concerns us is when we expect a product but don't have it in
* local state.
*/
const isDisabled = productId && ! simplePayment;
const isDisabled = productId && isEmpty( simplePayment );

if ( ! isSelected && isDisabled ) {
return (
Expand Down