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

Commit

Permalink
Product Collection - Fix undefined layout attribute issue in migration (
Browse files Browse the repository at this point in the history
#11196)

This commit addresses an issue where layout attributes could become undefined during the block migration process. Alongside this fix, several updates were made to align the migration logic with the new `ProductCollectionDisplayLayout` types:

- Added logic to handle `undefined` layout attributes, defaulting to `DEFAULT_ATTRIBUTES.displayLayout`.
- Removed `ProductGridLayout` and `ProductGridLayoutTypes` from the types file.
- Imported `LayoutOptions` and `ProductCollectionDisplayLayout` from the product-collection module.
- Updated the `mapLayoutType` and `mapLayoutPropertiesFrom...` functions to use the new layout types.
- Updated transformation functions like `transformProductTemplate` and `transformPostSummary` to use the new types.

These changes not only resolve the issue with undefined layout attributes but also align the codebase with the new layout options, enhancing code maintainability.
  • Loading branch information
imanish003 authored Oct 11, 2023
1 parent 6b2121c commit 6bb21dc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ import {
import type {
TransformBlock,
IsBlockType,
ProductGridLayout,
ProductGridLayoutTypes,
PostTemplateLayout,
PostTemplateLayoutTypes,
} from './types';
import {
LayoutOptions,
ProductCollectionDisplayLayout,
} from '../product-collection/types';

const VARIATION_NAME = 'woocommerce/product-query';

Expand Down Expand Up @@ -77,9 +79,7 @@ const isPostSummary: IsBlockType = ( { name, attributes } ) =>
attributes.__woocommerceNamespace ===
'woocommerce/product-collection/product-summary';

const mapLayoutType = (
type: ProductGridLayoutTypes
): PostTemplateLayoutTypes => {
const mapLayoutType = ( type: LayoutOptions ): PostTemplateLayoutTypes => {
if ( type === 'flex' ) {
return 'grid';
}
Expand All @@ -90,7 +90,7 @@ const mapLayoutType = (
};

const mapLayoutPropertiesFromProductCollectionToPostTemplate = (
layout: ProductGridLayout
layout: ProductCollectionDisplayLayout
): PostTemplateLayout => {
const { type, columns } = layout;

Expand All @@ -103,15 +103,15 @@ const mapLayoutPropertiesFromProductCollectionToPostTemplate = (
const transformProductTemplate: TransformBlock = (
block,
innerBlocks,
displayLayout?: ProductGridLayout
displayLayout?: ProductCollectionDisplayLayout
) => {
return createBlock(
'core/post-template',
{
className: 'products-block-post-template',
layout: postTemplateHasSupportForGridView
? mapLayoutPropertiesFromProductCollectionToPostTemplate(
displayLayout as ProductGridLayout
displayLayout as ProductCollectionDisplayLayout
)
: undefined,
__woocommerceNamespace:
Expand Down Expand Up @@ -150,7 +150,7 @@ const transformPostSummary: TransformBlock = ( block, innerBlocks ) => {

const mapInnerBlocks = (
innerBlocks: BlockInstance[],
displayLayout?: ProductGridLayout
displayLayout?: ProductCollectionDisplayLayout
): BlockInstance[] => {
const mappedInnerBlocks = innerBlocks.map( ( innerBlock ) => {
const { name, attributes } = innerBlock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ import {
import type {
TransformBlock,
IsBlockType,
ProductGridLayout,
ProductGridLayoutTypes,
PostTemplateLayout,
PostTemplateLayoutTypes,
} from './types';
import { DEFAULT_ATTRIBUTES } from '../product-collection/constants';
import {
LayoutOptions,
ProductCollectionDisplayLayout,
} from '../product-collection/types';

const mapAttributes = ( attributes: Record< string, unknown > ) => {
const { query, namespace, ...restAttributes } = attributes;
Expand Down Expand Up @@ -106,21 +109,23 @@ const transformPostSummary: TransformBlock = ( block, innerBlocks ) => {
);
};

const mapLayoutType = (
type: PostTemplateLayoutTypes
): ProductGridLayoutTypes => {
const mapLayoutType = ( type: PostTemplateLayoutTypes ): LayoutOptions => {
if ( type === 'grid' ) {
return 'flex';
return LayoutOptions.GRID;
}
if ( type === 'default' ) {
return 'list';
return LayoutOptions.STACK;
}
return 'flex';
return LayoutOptions.GRID;
};

const mapLayoutPropertiesFromPostTemplateToProductCollection = (
layout: PostTemplateLayout
): ProductGridLayout => {
): ProductCollectionDisplayLayout => {
if ( layout === undefined ) {
return DEFAULT_ATTRIBUTES.displayLayout as ProductCollectionDisplayLayout;
}

const { type, columnCount } = layout;

return {
Expand All @@ -132,7 +137,7 @@ const mapLayoutPropertiesFromPostTemplateToProductCollection = (
const getLayoutAttribute = (
attributes,
innerBlocks: BlockInstance[]
): ProductGridLayout => {
): ProductCollectionDisplayLayout => {
// Starting from GB 16, it's not Query Loop that keeps the layout, but the Post Template block.
// We need to account for that and in that case, move the layout properties
// from Post Template to Product Collection.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,8 @@ export type TransformBlock = (
block: BlockInstance,
innerBlock: BlockInstance[]
) => BlockInstance;
export type ProductGridLayoutTypes = 'flex' | 'list';
export type PostTemplateLayoutTypes = 'grid' | 'default';

export type ProductGridLayout = {
type: ProductGridLayoutTypes;
columns: number;
};

export type PostTemplateLayout = {
type: PostTemplateLayoutTypes;
columnCount: number;
Expand Down

0 comments on commit 6bb21dc

Please sign in to comment.