Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Merge branch 'trunk' into add/9288_cart-checkout-order-received_fse_t…
Browse files Browse the repository at this point in the history
…emplates
  • Loading branch information
wavvves committed May 10, 2023
2 parents faf1b1f + 34ee199 commit 92d89b0
Show file tree
Hide file tree
Showing 35 changed files with 831 additions and 319 deletions.
24 changes: 0 additions & 24 deletions assets/js/atomic/blocks/product-elements/button/attributes.ts

This file was deleted.

61 changes: 61 additions & 0 deletions assets/js/atomic/blocks/product-elements/button/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"name": "woocommerce/product-button",
"version": "1.0.0",
"title": "Add to Cart Button",
"description": "Display a call to action button which either adds the product to the cart, or links to the product page.",
"category": "woocommerce",
"keywords": [ "WooCommerce" ],
"usesContext": [ "query", "queryId", "postId" ],
"textdomain": "woo-gutenberg-products-block",
"attributes": {
"productId": {
"type": "number",
"default": 0
},
"textAlign": {
"type": "string",
"default": ""
},
"width": {
"type": "number"
},
"isDescendentOfSingleProductBlock": {
"type": "boolean",
"default": false
},
"isDescendentOfQueryLoop": {
"type": "boolean",
"default": false
}
},
"supports": {
"align": [ "wide", "full" ],
"color": {
"background": false,
"link": true
},
"html": false,
"typography": {
"fontSize": true,
"lineHeight": true
}
},
"ancestor": [
"woocommerce/all-products",
"woocommerce/single-product",
"core/post-template"
],
"styles": [
{
"name": "fill",
"label": "Fill",
"isDefault": true
},
{
"name": "outline",
"label": "Outline"
}
],
"apiVersion": 2,
"$schema": "https://schemas.wp.org/trunk/block.json"
}
19 changes: 0 additions & 19 deletions assets/js/atomic/blocks/product-elements/button/constants.tsx

This file was deleted.

52 changes: 0 additions & 52 deletions assets/js/atomic/blocks/product-elements/button/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
/* eslint-disable @wordpress/no-unsafe-wp-apis */
/**
* External dependencies
*/
import { Icon, button } from '@wordpress/icons';
import { registerBlockType } from '@wordpress/blocks';
import { isFeaturePluginBuild } from '@woocommerce/block-settings';
import { __experimentalGetSpacingClassesAndStyles } from '@wordpress/block-editor';
/**
* Internal dependencies
*/
import edit from './edit';
import save from './save';
import metadata from './block.json';

export const supports = {
const featurePluginSupport = {
...metadata.supports,
...( isFeaturePluginBuild() && {
color: {
text: true,
Expand Down Expand Up @@ -47,3 +55,22 @@ export const supports = {
},
} ),
};
// @ts-expect-error: `metadata` currently does not have a type definition in WordPress core
registerBlockType( metadata, {
icon: {
src: (
<Icon
icon={ button }
className="wc-block-editor-components-block-icon"
/>
),
},
attributes: {
...metadata.attributes,
},
supports: {
...featurePluginSupport,
},
edit,
save,
} );
5 changes: 4 additions & 1 deletion assets/js/atomic/blocks/product-elements/button/save.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ type Props = {
};

const Save = ( { attributes }: Props ): JSX.Element | null => {
if ( attributes.isDescendentOfQueryLoop ) {
if (
attributes.isDescendentOfQueryLoop ||
attributes.isDescendentOfSingleProductBlock
) {
return null;
}

Expand Down
1 change: 1 addition & 0 deletions assets/js/atomic/blocks/product-elements/button/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface BlockAttributes {
className?: string | undefined;
textAlign?: string | undefined;
isDescendentOfQueryLoop?: boolean | undefined;
isDescendentOfSingleProductBlock?: boolean | undefined;
width?: number | undefined;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
defaultAddressFields,
ShippingAddress,
} from '@woocommerce/settings';
import { useSelect, useDispatch } from '@wordpress/data';
import { useSelect, useDispatch, dispatch } from '@wordpress/data';
import { VALIDATION_STORE_KEY } from '@woocommerce/block-data';
import { FieldValidationStatus } from '@woocommerce/types';

Expand Down Expand Up @@ -123,6 +123,23 @@ const AddressForm = ( {
} );
}, [ addressFormFields, onChange, values ] );

// Clear postcode validation error if postcode is not required.
useEffect( () => {
addressFormFields.forEach( ( field ) => {
if ( field.key === 'postcode' && field.required === false ) {
const store = dispatch( 'wc/store/validation' );

if ( type === 'shipping' ) {
store.clearValidationError( 'shipping_postcode' );
}

if ( type === 'billing' ) {
store.clearValidationError( 'billing_postcode' );
}
}
} );
}, [ addressFormFields, type, clearValidationError ] );

useEffect( () => {
if ( type === 'shipping' ) {
validateShippingCountry(
Expand Down Expand Up @@ -265,11 +282,13 @@ const AddressForm = ( {
} )
}
customValidation={ ( inputObject: HTMLInputElement ) =>
customValidationHandler(
inputObject,
field.key,
values
)
field.required || inputObject.value
? customValidationHandler(
inputObject,
field.key,
values
)
: true
}
errorMessage={ field.errorMessage }
required={ field.required }
Expand Down
58 changes: 55 additions & 3 deletions assets/js/blocks/classic-template/archive-product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from '../product-query/constants';
import { VARIATION_NAME as productsVariationName } from '../product-query/variations/product-query';
import { createArchiveTitleBlock, createRowBlock } from './utils';
import { type InheritedAttributes } from './types';
import { OnClickCallbackParameter, type InheritedAttributes } from './types';

const createProductsBlock = ( inheritedAttributes: InheritedAttributes ) =>
createBlock(
Expand Down Expand Up @@ -71,7 +71,7 @@ const getDescriptionAllowingConversion = ( templateTitle: string ) =>
sprintf(
/* translators: %s is the template title */
__(
"This block serves as a placeholder for your %s. We recommend upgrading to the Products block for more features to edit your products visually. Don't worry, you can always revert back.",
'Transform this template into multiple blocks so you can add, remove, reorder, and customize your %s template.',
'woo-gutenberg-products-block'
),
templateTitle
Expand All @@ -96,17 +96,69 @@ const getDescription = ( templateTitle: string, canConvert: boolean ) => {
};

const getButtonLabel = () =>
__( 'Upgrade to Products block', 'woo-gutenberg-products-block' );
__( 'Transform into blocks', 'woo-gutenberg-products-block' );

const onClickCallback = ( {
clientId,
attributes,
getBlocks,
replaceBlock,
selectBlock,
}: OnClickCallbackParameter ) => {
replaceBlock( clientId, getBlockifiedTemplate( attributes ) );

const blocks = getBlocks();

const groupBlock = blocks.find(
( block ) =>
block.name === 'core/group' &&
block.innerBlocks.some(
( innerBlock ) =>
innerBlock.name === 'woocommerce/store-notices'
)
);

if ( groupBlock ) {
selectBlock( groupBlock.clientId );
}
};

const onClickCallbackWithTermDescription = ( {
clientId,
attributes,
getBlocks,
replaceBlock,
selectBlock,
}: OnClickCallbackParameter ) => {
replaceBlock( clientId, getBlockifiedTemplate( attributes, true ) );

const blocks = getBlocks();

const groupBlock = blocks.find(
( block ) =>
block.name === 'core/group' &&
block.innerBlocks.some(
( innerBlock ) =>
innerBlock.name === 'woocommerce/store-notices'
)
);

if ( groupBlock ) {
selectBlock( groupBlock.clientId );
}
};

export const blockifiedProductCatalogConfig = {
getBlockifiedTemplate,
isConversionPossible,
getDescription,
getButtonLabel,
onClickCallback,
};

export const blockifiedProductTaxonomyConfig = {
getBlockifiedTemplate: getBlockifiedTemplateWithTermDescription,
onClickCallback: onClickCallbackWithTermDescription,
isConversionPossible,
getDescription,
getButtonLabel,
Expand Down
Loading

0 comments on commit 92d89b0

Please sign in to comment.