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

Restructure Product Collection editor files #11981

Merged
merged 5 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
91 changes: 91 additions & 0 deletions assets/js/blocks/product-collection/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
*/
import { getSetting } from '@woocommerce/settings';
import { objectOmit } from '@woocommerce/utils';
import {
type InnerBlockTemplate,
createBlock,
// @ts-expect-error Missing types in Gutenberg
createBlocksFromInnerBlocksTemplate,
} from '@wordpress/blocks';

/**
* Internal dependencies
Expand All @@ -15,7 +21,10 @@ import {
ProductCollectionDisplayLayout,
LayoutOptions,
} from './types';
import { ImageSizing } from '../../atomic/blocks/product-elements/image/types';
import { VARIATION_NAME as PRODUCT_TITLE_ID } from './variations/elements/product-title';
import { getDefaultValueOfInheritQueryFromTemplate } from './utils';
import blockJson from './block.json';

export const STOCK_STATUS_OPTIONS = getSetting< Record< string, string > >(
'stockStatusOptions',
Expand Down Expand Up @@ -91,3 +100,85 @@ export const DEFAULT_FILTERS: Partial< ProductCollectionQuery > = {
featured: DEFAULT_QUERY.featured,
timeFrame: undefined,
};

/**
* Default inner block templates for the product collection block.
* Exported for use in different collections, e.g., 'New Arrivals' collection.
*/
export const INNER_BLOCKS_PRODUCT_TEMPLATE: InnerBlockTemplate = [
'woocommerce/product-template',
{},
[
[
'woocommerce/product-image',
{
imageSizing: ImageSizing.THUMBNAIL,
},
],
[
'core/post-title',
{
textAlign: 'center',
level: 3,
fontSize: 'medium',
style: {
spacing: {
margin: {
bottom: '0.75rem',
top: '0',
},
},
},
isLink: true,
__woocommerceNamespace: PRODUCT_TITLE_ID,
},
],
[
'woocommerce/product-price',
{
textAlign: 'center',
fontSize: 'small',
},
],
[
'woocommerce/product-button',
{
textAlign: 'center',
fontSize: 'small',
},
],
],
];

export const INNER_BLOCKS_PAGINATION_TEMPLATE: InnerBlockTemplate = [
'core/query-pagination',
{
layout: {
type: 'flex',
justifyContent: 'center',
},
},
];

export const INNER_BLOCKS_NO_RESULTS_TEMPLATE: InnerBlockTemplate = [
'woocommerce/product-collection-no-results',
];

export const INNER_BLOCKS_TEMPLATE: InnerBlockTemplate[] = [
INNER_BLOCKS_PRODUCT_TEMPLATE,
INNER_BLOCKS_PAGINATION_TEMPLATE,
INNER_BLOCKS_NO_RESULTS_TEMPLATE,
];

export const getDefaultProductCollection = () =>
createBlock(
blockJson.name,
{
...DEFAULT_ATTRIBUTES,
query: {
...DEFAULT_ATTRIBUTES.query,
inherit: getDefaultValueOfInheritQueryFromTemplate(),
},
},
createBlocksFromInnerBlocksTemplate( INNER_BLOCKS_TEMPLATE )
);
Original file line number Diff line number Diff line change
Expand Up @@ -2,82 +2,23 @@
* External dependencies
*/
import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor';
import { BlockEditProps, InnerBlockTemplate } from '@wordpress/blocks';
import { BlockEditProps } from '@wordpress/blocks';
import { useInstanceId } from '@wordpress/compose';
import { useEffect } from '@wordpress/element';

/**
* Internal dependencies
*/
import { ImageSizing } from '../../atomic/blocks/product-elements/image/types';
import type {
ProductCollectionAttributes,
ProductCollectionQuery,
} from './types';
import { VARIATION_NAME as PRODUCT_TITLE_ID } from './variations/elements/product-title';
} from '../types';
import InspectorControls from './inspector-controls';
import { DEFAULT_ATTRIBUTES } from './constants';
import { DEFAULT_ATTRIBUTES, INNER_BLOCKS_TEMPLATE } from '../constants';
import './editor.scss';
import { getDefaultValueOfInheritQueryFromTemplate } from './utils';
import { getDefaultValueOfInheritQueryFromTemplate } from '../utils';
import ToolbarControls from './toolbar-controls';

export const INNER_BLOCKS_TEMPLATE: InnerBlockTemplate[] = [
[
'woocommerce/product-template',
{},
[
[
'woocommerce/product-image',
{
imageSizing: ImageSizing.THUMBNAIL,
},
],
[
'core/post-title',
{
textAlign: 'center',
level: 3,
fontSize: 'medium',
style: {
spacing: {
margin: {
bottom: '0.75rem',
top: '0',
},
},
},
isLink: true,
__woocommerceNamespace: PRODUCT_TITLE_ID,
},
],
[
'woocommerce/product-price',
{
textAlign: 'center',
fontSize: 'small',
},
],
[
'woocommerce/product-button',
{
textAlign: 'center',
fontSize: 'small',
},
],
],
],
[
'core/query-pagination',
{
layout: {
type: 'flex',
justifyContent: 'center',
},
},
],
[ 'woocommerce/product-collection-no-results' ],
];

const Edit = ( props: BlockEditProps< ProductCollectionAttributes > ) => {
const { attributes, setAttributes } = props;
const { queryId } = attributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
/**
* Internal dependencies
*/
import { QueryControlProps } from '../types';
import { QueryControlProps } from '../../types';

const EDIT_ATTRIBUTES_URL = `${ ADMIN_URL }edit.php?post_type=product&page=product_attributes`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {
/**
* Internal dependencies
*/
import { DisplayLayoutToolbarProps } from '../types';
import { getDefaultDisplayLayout } from '../constants';
import { DisplayLayoutToolbarProps } from '../../types';
import { getDefaultDisplayLayout } from '../../constants';

const columnsLabel = __( 'Columns', 'woo-gutenberg-products-block' );
const toggleLabel = __( 'Responsive', 'woo-gutenberg-products-block' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
/**
* Internal dependencies
*/
import { ETimeFrameOperator, QueryControlProps } from '../types';
import { ETimeFrameOperator, QueryControlProps } from '../../types';

const CreatedControl = ( props: QueryControlProps ) => {
const { query, setQueryAttribute } = props;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
/**
* Internal dependencies
*/
import { QueryControlProps } from '../types';
import { QueryControlProps } from '../../types';

const FeaturedProductsControl = ( props: QueryControlProps ) => {
const { query, setQueryAttribute } = props;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
/**
* Internal dependencies
*/
import { QueryControlProps } from '../types';
import { QueryControlProps } from '../../types';

/**
* Returns:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import {
/**
* Internal dependencies
*/
import metadata from '../block.json';
import { ProductCollectionAttributes } from '../types';
import { setQueryAttribute } from '../utils';
import { DEFAULT_FILTERS, getDefaultSettings } from '../constants';
import metadata from '../../block.json';
import { ProductCollectionAttributes } from '../../types';
import { setQueryAttribute } from '../../utils';
import { DEFAULT_FILTERS, getDefaultSettings } from '../../constants';
import UpgradeNotice from './upgrade-notice';
import ColumnsControl from './columns-control';
import InheritQueryControl from './inherit-query-control';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import {
/**
* Internal dependencies
*/
import { ProductCollectionQuery } from '../types';
import { DEFAULT_QUERY } from '../constants';
import { getDefaultValueOfInheritQueryFromTemplate } from '../utils';
import { ProductCollectionQuery } from '../../types';
import { DEFAULT_QUERY } from '../../constants';
import { getDefaultValueOfInheritQueryFromTemplate } from '../../utils';

const label = __(
'Inherit query from template',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
/**
* Internal dependencies
*/
import { QueryControlProps } from '../types';
import { QueryControlProps } from '../../types';

const KeywordControl = ( props: QueryControlProps ) => {
const { query, setQueryAttribute } = props;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
/**
* Internal dependencies
*/
import { DisplayLayoutToolbarProps, LayoutOptions } from '../types';
import { DisplayLayoutToolbarProps, LayoutOptions } from '../../types';

const getHelpText = ( layoutOptions: LayoutOptions ) => {
switch ( layoutOptions ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
/**
* Internal dependencies
*/
import { QueryControlProps } from '../types';
import { QueryControlProps } from '../../types';

const OnSaleControl = ( props: QueryControlProps ) => {
const { query, setQueryAttribute } = props;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import {
TProductCollectionOrder,
TProductCollectionOrderBy,
QueryControlProps,
} from '../types';
import { getDefaultQuery } from '../constants';
} from '../../types';
import { getDefaultQuery } from '../../constants';

const orderOptions = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {
/**
* Internal dependencies
*/
import { QueryControlProps } from '../types';
import { STOCK_STATUS_OPTIONS, getDefaultStockStatuses } from '../constants';
import { QueryControlProps } from '../../types';
import { STOCK_STATUS_OPTIONS, getDefaultStockStatuses } from '../../constants';

/**
* Gets the id of a specific stock status from its text label
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
* Internal dependencies
*/
import TaxonomyItem from './taxonomy-item';
import { ProductCollectionQuery } from '../../types';
import { ProductCollectionQuery } from '../../../types';

interface TaxonomyControlProps {
query: ProductCollectionQuery;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
DisplayLayoutToolbarProps,
ProductCollectionDisplayLayout,
LayoutOptions,
} from '../types';
} from '../../types';

const DisplayLayoutToolbar = ( props: DisplayLayoutToolbarProps ) => {
const { type, columns, shrinkColumns } = props.displayLayout;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
/**
* Internal dependencies
*/
import { ProductCollectionQuery } from '../types';
import { ProductCollectionQuery } from '../../types';

interface DisplaySettingsToolbarProps {
query: ProductCollectionQuery;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { BlockControls } from '@wordpress/block-editor';
/**
* Internal dependencies
*/
import { setQueryAttribute } from '../utils';
import { ProductCollectionAttributes } from '../types';
import { setQueryAttribute } from '../../utils';
import { ProductCollectionAttributes } from '../../types';
import DisplaySettingsToolbar from './display-settings-toolbar';
import DisplayLayoutToolbar from './display-layout-toolbar';
import PatternChooserToolbar from './pattern-chooser-toolbar';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
import { Modal } from '@wordpress/components';
import {
store as blockEditorStore,
__experimentalBlockPatternsList as BlockPatternsList,

Check warning on line 9 in assets/js/blocks/product-collection/edit/toolbar-controls/pattern-selection-modal.tsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

assets/js/blocks/product-collection/edit/toolbar-controls/pattern-selection-modal.tsx#L9

[@wordpress/no-unsafe-wp-apis] Usage of `__experimentalBlockPatternsList` from `@wordpress/block-editor` is not allowed. See https://developer.wordpress.org/block-editor/contributors/develop/coding-guidelines/#experimental-and-unstable-apis for details.
} from '@wordpress/block-editor';
import { type BlockInstance, cloneBlock } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import { ProductCollectionQuery } from '../types';
import { ProductCollectionQuery } from '../../types';

const blockName = 'woocommerce/product-collection';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import {
BLOCK_DESCRIPTION,
BLOCK_TITLE,
} from '../../../../atomic/blocks/product-elements/summary/constants';
import blockJson from '../../block.json';

export const CORE_NAME = 'core/post-excerpt';
export const VARIATION_NAME = 'woocommerce/product-collection/product-summary';
export const VARIATION_NAME = `${ blockJson.name }/product-summary`;

const registerProductSummary = () => {
registerElementVariation( CORE_NAME, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import {
BLOCK_DESCRIPTION,
BLOCK_TITLE,
} from '../../../../atomic/blocks/product-elements/title/constants';
import blockJson from '../../block.json';

export const CORE_NAME = 'core/post-title';
export const VARIATION_NAME = 'woocommerce/product-collection/product-title';
export const VARIATION_NAME = `${ blockJson.name }/product-title`;

const registerProductTitle = () => {
registerElementVariation( CORE_NAME, {
Expand Down
Loading