Skip to content

Commit

Permalink
All Products block: Try hydrating
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Jul 28, 2022
1 parent 22bbecf commit fe64a58
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 154 deletions.
51 changes: 51 additions & 0 deletions assets/js/blocks/products/all-products/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 1,
"textdomain": "woo-gutenberg-products-block",
"name": "woocommerce/all-products",
"title": "All Products",
"category": "woocommerce",
"keywords": [ "WooCommerce" ],
"description": "Display products from your store in a grid layout.",
"supports": {
"align": [ "wide", "full" ],
"html": false,
"multiple": false
},
"example": {
"attributes": {
"isPreview": true
}
},
"attributes": {
"columns": {
"type": "number",
"public": "true"
},
"rows": {
"type": "number",
"public": "true"
},
"alignButtons": {
"type": "boolean",
"public": "true"
},
"contentVisibility": {
"type": "object",
"public": "true"
},
"orderby": {
"type": "string",
"public": "true"
},
"layoutConfig": {
"type": "array",
"public": "true"
},
"isPreview": {
"type": "boolean",
"default": false,
"public": "true"
}
}
}
21 changes: 21 additions & 0 deletions assets/js/blocks/products/all-products/defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* External dependencies
*/
import { getSetting } from '@woocommerce/settings';

/**
* Internal dependencies
*/
import { DEFAULT_PRODUCT_LIST_LAYOUT } from '../base-utils';

export default {
columns: getSetting( 'default_columns', 3 ),
rows: getSetting( 'default_rows', 3 ),
alignButtons: false,
contentVisibility: {
orderBy: true,
},
orderby: 'date',
layoutConfig: DEFAULT_PRODUCT_LIST_LAYOUT,
isPreview: false,
};
36 changes: 36 additions & 0 deletions assets/js/blocks/products/all-products/deprecated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* External dependencies
*/
import { InnerBlocks } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import metadata from './block.json';
import { getBlockClassName } from '../utils.js';

const { attributes: attributeDefinitions } = metadata;

const v1 = {
attributes: Object.assign( {}, attributeDefinitions, {
rows: { type: 'number', default: 1 },
} ),
save( { attributes } ) {
const data = {
'data-attributes': JSON.stringify( attributes ),
};
return (
<div
className={ getBlockClassName(
'wc-block-all-products',
attributes
) }
{ ...data }
>
<InnerBlocks.Content />
</div>
);
},
};

export default [ v1 ];
101 changes: 14 additions & 87 deletions assets/js/blocks/products/all-products/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { InnerBlocks } from '@wordpress/block-editor';
import { registerBlockType } from '@wordpress/blocks';
import { Icon, grid } from '@wordpress/icons';
import '@woocommerce/atomic-blocks';

/**
* Internal dependencies
*/
import Editor from './edit';
import { attributes as sharedAttributes, defaults } from '../attributes';
import { getBlockClassName } from '../utils.js';
import metadata from './block.json';
import deprecated from './deprecated';
import edit from './edit';
import save from './save';
import defaults from './defaults';

export const blockSettings = {
title: __( 'All Products', 'woo-gutenberg-products-block' ),
const { name } = metadata;
export { metadata, name };

const settings = {
icon: {
src: (
<Icon
Expand All @@ -24,86 +26,11 @@ export const blockSettings = {
/>
),
},
category: 'woocommerce',
keywords: [ __( 'WooCommerce', 'woo-gutenberg-products-block' ) ],
description: __(
'Display products from your store in a grid layout.',
'woo-gutenberg-products-block'
),
supports: {
align: [ 'wide', 'full' ],
html: false,
multiple: false,
},
example: {
attributes: {
isPreview: true,
},
},
attributes: sharedAttributes,
defaults,
/**
* Renders and manages the block.
*
* @param {Object} props Props to pass to block.
*/
edit( props ) {
return <Editor { ...props } />;
},
edit,
// Save the props to post content.
save( { attributes } ) {
const dataAttributes = {};
Object.keys( attributes )
.sort()
.forEach( ( key ) => {
dataAttributes[ key ] = attributes[ key ];
} );
const data = {
'data-attributes': JSON.stringify( dataAttributes ),
};
return (
<div
className={ getBlockClassName(
'wc-block-all-products',
attributes
) }
{ ...data }
>
<InnerBlocks.Content />
</div>
);
},
save,
deprecated,
defaults,
};

/**
* Register and run the "All Products" block.
*/
registerBlockType( 'woocommerce/all-products', {
...blockSettings,
/**
* Deprecation rule to handle the previous default rows which was 1 instead of 3.
*/
deprecated: [
{
attributes: Object.assign( {}, blockSettings.attributes, {
rows: { type: 'number', default: 1 },
} ),
save( { attributes } ) {
const data = {
'data-attributes': JSON.stringify( attributes ),
};
return (
<div
className={ getBlockClassName(
'wc-block-all-products',
attributes
) }
{ ...data }
>
<InnerBlocks.Content />
</div>
);
},
},
],
} );
registerBlockType( name, settings );
22 changes: 22 additions & 0 deletions assets/js/blocks/products/all-products/save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* External dependencies
*/
import { InnerBlocks } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import { getBlockClassName } from '../utils.js';

export default function save( { attributes } ) {
return (
<div
className={ getBlockClassName(
'wc-block-all-products',
attributes
) }
>
<InnerBlocks.Content />
</div>
);
}
67 changes: 0 additions & 67 deletions assets/js/blocks/products/attributes.js

This file was deleted.

Loading

0 comments on commit fe64a58

Please sign in to comment.