Skip to content

Gutenberg: Simple payment read/save/update - #28065

Merged
simison merged 21 commits into
masterfrom
add/gutenpack-simplepayments-edit-ui
Oct 30, 2018
Merged

simison merged 21 commits into
masterfrom
add/gutenpack-simplepayments-edit-ui

Conversation

@sirreal

@sirreal sirreal commented Oct 24, 2018 •

Copy link
Copy Markdown
Member

Changes proposed in this Pull Request

  • Add some simple UI to modify block attributes
  • Read, create and update simple payment posts 😵
  • This is not intended as a finalized UI, but a step to help with development.
  • Existing wpcom flows are used, they should be swapped for more current Gutenberg approaches in the future.
  • There are lots of FIXME and TODO comments, but this works behind the gutenberg/block/simple-payments feature flag 👍
  • This works in both local Jetpack's wp-admin Gutenberg as well Calypso-Gutenberg.
  • Depends on Jetpack changes to Simple Payments API:

Testing instructions

Test in Jetpack

Test in Calypso

Manual checking

Apply these patches and cross-test the block in different editors:

Checklist:

  • Play with the Simple Payments block and see if attributes update
  • A new payment should save a new post. Watch network requests.
  • Update and watch it save again.
  • The save/update are triggered when the block is deselected or editor's post gets updated
  • Save the post and see the page: you should see simple payments button
  • Refresh the editor page, note how block pulls in info from API and ignores block attributes: you can test this by removing or mocking attributes in the editor's code-editor, saving the post while in code-editor and reloading the page again.

@sirreal sirreal added Simple Payments [Goal] Gutenberg Working towards full integration with Gutenberg [Type] Feature progress labels Oct 24, 2018
@sirreal sirreal self-assigned this Oct 24, 2018
@matticbot

matticbot commented Oct 24, 2018 •

Copy link
Copy Markdown
Contributor

@sirreal
sirreal requested a review from simison October 24, 2018 19:54
@sirreal sirreal changed the title Add simple fields Gutenberg: Add basic simple payments fields Oct 24, 2018
@sirreal sirreal added the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Oct 24, 2018
@sirreal
sirreal requested a review from a team October 24, 2018 20:01
@sirreal sirreal changed the title Gutenberg: Add basic simple payments fields Gutenberg: Simple payment save/update Oct 25, 2018

@sirreal sirreal left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Lots of todos and a few blockers, like finding the selected site id from within a block.

cc: @vindl

status: 'publish',
content: description,
featured_media: '',
// @FIXME: meta isn't saving. Tried { spay_price: … } simple object literal and it didn't work either :(

@simison simison Oct 25, 2018 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We need to use register_meta to whitelist these meta fields in the API for reading+writing (PR Automattic/jetpack#10402).

https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/

title,
status: 'publish',
content: description,
featured_media: '',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This has to default to 0, otherwise, we'll get a rest_invalid_param error Invalid parameter(s): featured_media

const { id } = await apiFetch( {
path,
method: 'POST',
body: JSON.stringify( attributesToPost( attributes ) ),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is sending the wrong content type. We need to send application/json, so this line has to be:

data: attributesToPost( attributes ),

content: description,
featured_media: '',
// @FIXME: meta isn't saving. Tried { spay_price: … } simple object literal and it didn't work either :(
meta: [

@rodrigoi rodrigoi Oct 26, 2018 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The backend expects the meta with a different format. We need to send:

meta: {
    'spay_price': price,
    'spay_currency': 'USD',
    'spay_multiple': 0,
    'spay_email': email,
    'spay_formatted_price': '$' + price.toFixed( 2 ),
},

@rodrigoi

Copy link
Copy Markdown
Contributor

I can confirm that, once applied the changes from Automattic/jetpack#10402 on WordPress.com, that it works as expected in Calypso for sites with a business plan.

I think we can safely add import 'gutenberg/extensions/simple-payments/editor'; to the Jetpack presets on client/gutenberg/extensions/presets/jetpack/editor.js.

@simison simison changed the title Gutenberg: Simple payment save/update Gutenberg: Simple payment read/save/update Oct 26, 2018

@tyxla tyxla left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is great work 👏

The PR is testing well for me! I realize the UI is still a WIP, so I've refrained from any feedback on that bit.

The only thing I've noticed is when saving and then refreshing the post that has a payment button, I get the following:

I've also left some comments, mostly minor stuff.

Nicely done! 💯

/**
* External dependencies
*/
import get from 'lodash/get';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: Wanna 🔤 imports?

* External dependencies
*/
import get from 'lodash/get';
import { __ } from '@wordpress/i18n';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Noting that once #28088 lands, we'll have to use our own i18n wrappers.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Done in d49d93c

this.props.setAttributes( { paymentId } );
constructor() {
super( ...arguments );
this.state = {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we alter:

		this.state = {
			savingProduct: false,
		};

to a class instance property, defined outside of constructor():

		state = {
			savingProduct: false,
		};

we could totally ditch the constructor() method.


if ( ! prevProps.simplePayment && simplePayment ) {
setAttributes( {
description: get( simplePayment, 'content.raw', attributes.description ),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We could save _.get() some cycles if we use arrays instead of strings in all these calls, e.g. [ 'content', 'raw' ] instead of 'content.raw'


// @TODO field validation

const path = `/wp/v2/${ SIMPLE_PAYMENTS_PRODUCT_POST_TYPE }/${ paymentId ? paymentId : '' }`;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ditch the trailing / if we don't have an ID yet?

*/
import { SIMPLE_PAYMENTS_PRODUCT_POST_TYPE } from 'lib/simple-payments/constants';

class Edit extends Component {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Perhaps a little less-broad name for this component?


if ( ! isSelected ) {
// @TODO component
return (

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm expecting this is a WIP at this time? Would be even better if we split it off in a separate component before merging, to make it easier for iteration with less chance for conflicts.

Essentially, the more we componentize here, the better it becomes for iteration and reusability.

category: 'jetpack',

keywords: [ 'simple payments', 'PayPal' ],
keywords: [ __( 'simple payments', 'jetpack' ), __( 'PayPal', 'jetpack' ) ],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Noting that after #28088 is merged, we won't need the textdomains.

// @TODO move everything else except `paymentId` to state instead?
currency: {
type: 'string',
default: 'USD',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

FWIW, any block instances that use the default values are removed from the serialized representation of the block, so we should either be sure that we're using the right default values, or try to rely as little as possible on default attribute values at block registration level.

const titleId = `${ baseId }__title`;

if ( ! isSelected ) {
// @TODO component

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm assuming that all TODOs and FIXMEs will be handled in subsequent PRs?

@simison
simison force-pushed the add/gutenpack-simplepayments-edit-ui branch from 382bc16 to d49d93c Compare October 29, 2018 21:54
@simison

simison commented Oct 30, 2018

Copy link
Copy Markdown
Member

The only thing I've noticed is when saving and then refreshing the post that has a payment button, I get the following:

Yap, known issue independent from the block Automattic/jetpack#10481

As per Gutenberg docs:

_Note:_ While it is possible to return a string value from `save`, it _will be escaped_. If the string includes HTML markup, the markup will be shown on the front of the site verbatim, not as the equivalent HTML node content. If you must return raw HTML from `save`, use `wp.element.RawHTML`. As the name implies, this is prone to [cross-site scripting](https://en.wikipedia.org/wiki/Cross-site_scripting) and therefore is discouraged in favor of a WordPress Element hierarchy whenever possible.

Kudos @sirreal
@simison
simison force-pushed the add/gutenpack-simplepayments-edit-ui branch from 506b6eb to b924b01 Compare October 30, 2018 14:32
@simison

simison commented Oct 30, 2018 •

Copy link
Copy Markdown
Member

@tyxla I made some changes you suggested, left a few for the next PR (#28169) and rebased.

Since I removed this from jetpack preset again and put it behind feature flag in Calypso, to test you can now just:

Saving some fields depends on D19907-code

@simison simison mentioned this pull request Oct 30, 2018
9 of 15 tasks

@tyxla tyxla left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks good and works well 👍

Let's :shipit: considering that:

  • This has dependencies that haven't been merged yet
  • There are many bits that are a WIP and will be iterated on in following PRs.

@simison
simison merged commit 3bc58e4 into master Oct 30, 2018
@simison
simison deleted the add/gutenpack-simplepayments-edit-ui branch October 30, 2018 15:35
@matticbot matticbot removed [Status] In Progress [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. labels Oct 30, 2018
@rodrigoi

rodrigoi commented Nov 2, 2018

Copy link
Copy Markdown
Contributor

@tyxla Automattic/jetpack#10531 should fix the PayPal execution error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Goal] Gutenberg Working towards full integration with Gutenberg Simple Payments

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants