Skip to content

Conversation

@miina
Copy link
Contributor

@miina miina commented May 3, 2018

Description

How has this been tested?

  • Tested the SSR component using Columns block with Archives.

Types of changes

  • Adds layout to each block's schema;
  • Makes use of Placeholder component;
  • Adds check for component being mounted before setting state.

This was referenced May 3, 2018
Copy link
Member

@noisysocks noisysocks left a comment

Choose a reason for hiding this comment

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

Nice work on this! 😄


componentWillMount() {
this.isStillMounted = true;
}
Copy link
Member

Choose a reason for hiding this comment

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

componentWillMount will be deprecated in a future React release. Could we do this in componentDidMount instead?

https://reactjs.org/docs/react-component.html#unsafe_componentwillmount


return wp.apiRequest( { path: path } ).then( ( response ) => {
if ( response && response.rendered ) {
return apiRequest( { path: path } ).then( ( response ) => {
Copy link
Member

Choose a reason for hiding this comment

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

minor: We could write { path } instead of { path: path } here.

render() {
const response = this.state.response;
if ( ! response || ! response.length ) {
if ( ! response ) {
Copy link
Member

Choose a reason for hiding this comment

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

Unrelated to this PR, but should we handle a possible error state here? If the API returns with a 404 or 500 response then we'll be stuck showing a spinner forever.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All the comments should be addressed within 9df912a, also added a possible handling for error response including displaying the error message from REST API in case of failure.
For example:
screen shot 2018-05-10 at 4 47 46 pm

Let me know if this approach seems OK.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah this works for me. It matches the error state that Shared Blocks have which is nice.

Going forward we probably will want to improve the error state for all of our dynamic blocks. For example, we should make sure that they all have actionable error messages and e.g. Try again buttons.

But I don't want that to block this PR so 👍 for now.

$attributes = array_merge( $block_type->attributes, $layout );
} else {
$attributes = $layout;
}
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, merging layout into the attributes in the controller doesn't feel right to me since it means that the controller is no longer a light layer on top of the WP_Block_Type_Registry API.

Maybe we could do this in a getter on WP_Block_Type? e.g.

class WP_Block_Type {
	...
	public function get_attributes() {
		if ( is_ array( $this->attributes ) ) {
			return array_merge( $this->attributes, array(
				'layout' => array(
					'type' => 'string',
				),
			) );
		}
		return $this->attributes;
	}
}

@noisysocks
Copy link
Member

Thanks @miina—this looks good. Is there a way to test the changes or do we need to wait for the Archives block to be merged?

@miina
Copy link
Contributor Author

miina commented May 11, 2018

@noisysocks Tested it also locally with replacing latest posts return logic with ServerSideRender (e.g. <ServerSideRender block="core/latest-posts" attributes={ this.props.attributes } />, not sure if there's a better way how to test it otherwise.

Copy link
Member

@noisysocks noisysocks left a comment

Choose a reason for hiding this comment

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

Tested this locally and it fixes the bug. Nice work 👍

Just need to fix up that i18n issue and this is good to go.

);
} else if ( response.error ) {
return (
<Placeholder>{ __( 'Error loading block: ' ) + response.errorMsg }</Placeholder>
Copy link
Member

Choose a reason for hiding this comment

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

We'll need to use wp.i18n.sprintf here so that this string is translatable into RTL languages like Hebrew.

<Placeholder>{ sprintf( __( 'Error loading block: %s' ), response.errorMsg ) }</Placeholder>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for spotting that, added within dcb8048.

render() {
const response = this.state.response;
if ( ! response || ! response.length ) {
if ( ! response ) {
Copy link
Member

Choose a reason for hiding this comment

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

Yeah this works for me. It matches the error state that Shared Blocks have which is nice.

Going forward we probably will want to improve the error state for all of our dynamic blocks. For example, we should make sure that they all have actionable error messages and e.g. Try again buttons.

But I don't want that to block this PR so 👍 for now.

@noisysocks noisysocks merged commit 4f5295d into WordPress:master May 15, 2018
@mtias mtias added this to the 2.9 milestone May 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants