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

Add logic of Upgrade Notice after upgrading Products to Product Collection #10267

Merged
merged 27 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
b97c0d0
WIP of Upgrade Notice state
kmanijak Jul 18, 2023
d66a275
Extend the state options with seeing option
kmanijak Jul 18, 2023
62663c0
Move the logic to the dedicated folder
kmanijak Jul 19, 2023
354e035
Subscribe only if not reverted
kmanijak Jul 19, 2023
fe7c896
Merge branch 'trunk' into add/products-migration-upgrade-notice-logic
kmanijak Jul 20, 2023
c569c04
Refactor the way UpgradeNotice is rendered
kmanijak Jul 20, 2023
52e3364
Simplify the logic of keeping the Upgrade Notice state in local storage
kmanijak Jul 20, 2023
32f7d6e
Improve types organisation
kmanijak Jul 20, 2023
51bb3b5
Lift the functions interacting with local storage to the Inspector Co…
kmanijak Jul 20, 2023
8ff448f
Simplify logic of showing Upgrade Notice
kmanijak Jul 20, 2023
845526f
Disable auto migration
kmanijak Jul 20, 2023
b985da5
Refactoring
kmanijak Jul 21, 2023
a44da88
Use useLocalStorageState hook
kmanijak Jul 21, 2023
487c1a4
Merge branch 'trunk' into add/products-migration-upgrade-notice-logic
kmanijak Jul 21, 2023
07ea260
Fix incorrect merge
kmanijak Jul 21, 2023
01c0de1
Final improvements
kmanijak Jul 21, 2023
9e33b8c
Allow to display Upgrade Notice after revert and manual upgrade
kmanijak Jul 21, 2023
f7931a2
Merge branch 'trunk' into add/products-migration-upgrade-notice-logic
kmanijak Jul 26, 2023
a86499c
Fix incorrect merge
kmanijak Jul 26, 2023
6ef4658
Improve the unsubscribe process
kmanijak Jul 27, 2023
f233045
Trigger auto-update from Product Collection only
kmanijak Jul 27, 2023
6164dd4
Merge branch 'trunk' into add/products-migration-upgrade-notice-logic
kmanijak Jul 27, 2023
c04fb52
Remove weird console.log
kmanijak Jul 27, 2023
85037f5
Abstract manual update from Product Query
kmanijak Jul 27, 2023
548053c
Fix the taxQuery migration from Product Collection to Products
kmanijak Aug 8, 2023
95b5195
Merge branch 'trunk' into add/products-migration-upgrade-notice-logic
kmanijak Aug 9, 2023
46ca3da
Product Collection - logic to hide upgrade notice (#10494)
kmanijak Aug 10, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Internal dependencies
*/
import type { UpgradeNoticeStatus, UpgradeNoticeStatuses } from './types';

export const AUTO_REPLACE_PRODUCTS_WITH_PRODUCT_COLLECTION = false;
export const MANUAL_REPLACE_PRODUCTS_WITH_PRODUCT_COLLECTION = false;
export const HOURS_TO_DISPLAY_UPGRADE_NOTICE = 72;
export const UPGRADE_NOTICE_DISPLAY_COUNT_THRESHOLD = 4;
export const MIGRATION_STATUS_LS_KEY =
'wc-blocks_upgraded-products-to-product-collection';
// Initial status used in the localStorage
export const INITIAL_STATUS_LS_VALUE: UpgradeNoticeStatuses = 'notseen';

export const getInitialStatusLSValue: () => UpgradeNoticeStatus = () => ( {
status: INITIAL_STATUS_LS_VALUE,
time: Date.now(),
displayCount: 0,
} );
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export * from './migration-from-products-to-product-collection';
export * from './migration-from-product-collection-to-products';
export * from './migration-utils';
export * from './constants';
export * from './types';
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ import { select, dispatch } from '@wordpress/data';
/**
* Internal dependencies
*/
import { disableAutoUpdate } from './migration-from-products-to-product-collection';
import {
getProductCollectionBlockClientIds,
checkIfBlockCanBeInserted,
postTemplateHasSupportForGridView,
type TransformBlock,
type IsBlockType,
type ProductGridLayout,
type ProductGridLayoutTypes,
type PostTemplateLayout,
type PostTemplateLayoutTypes,
setUpgradeStatus,
} from './migration-utils';
import type {
TransformBlock,
IsBlockType,
ProductGridLayout,
ProductGridLayoutTypes,
PostTemplateLayout,
PostTemplateLayoutTypes,
} from './types';

const VARIATION_NAME = 'woocommerce/product-query';

Expand Down Expand Up @@ -45,6 +49,10 @@ const mapAttributes = ( attributes ) => {
mappedQuery.__woocommerceOnSale = woocommerceOnSale;
}

if ( taxQuery ) {
mappedQuery.taxQuery = taxQuery;
}

return {
...restAttributes,
namespace: VARIATION_NAME,
Expand Down Expand Up @@ -207,3 +215,12 @@ export const replaceProductCollectionWithProducts = () => {

productCollectionBlockClientIds.map( replaceProductCollectionBlock );
};

export const revertMigration = () => {
disableAutoUpdate();
setUpgradeStatus( {
status: 'reverted',
time: Date.now(),
} );
replaceProductCollectionWithProducts();
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,31 @@
* External dependencies
*/
import { createBlock, BlockInstance } from '@wordpress/blocks';
import { select, dispatch } from '@wordpress/data';
import { select, dispatch, subscribe } from '@wordpress/data';
import { isWpVersion } from '@woocommerce/settings';

/**
* Internal dependencies
*/
import {
AUTO_REPLACE_PRODUCTS_WITH_PRODUCT_COLLECTION,
getInitialStatusLSValue,
} from './constants';
import {
getProductsBlockClientIds,
checkIfBlockCanBeInserted,
postTemplateHasSupportForGridView,
type TransformBlock,
type IsBlockType,
type ProductGridLayout,
type ProductGridLayoutTypes,
type PostTemplateLayout,
type PostTemplateLayoutTypes,
getUpgradeStatus,
setUpgradeStatus,
} from './migration-utils';
import type {
TransformBlock,
IsBlockType,
ProductGridLayout,
ProductGridLayoutTypes,
PostTemplateLayout,
PostTemplateLayoutTypes,
} from './types';

const mapAttributes = ( attributes: Record< string, unknown > ) => {
const { query, namespace, ...restAttributes } = attributes;
Expand All @@ -41,7 +50,7 @@ const mapAttributes = ( attributes: Record< string, unknown > ) => {
isProductCollectionBlock: true,
...restQuery,
},
displayUpgradeNotice: true,
convertedFromProducts: true,
};
};

Expand Down Expand Up @@ -194,9 +203,7 @@ const replaceProductsBlocks = ( productsBlockClientIds: string[] ) => {
return !! results.length && results.every( ( result ) => !! result );
};

export const replaceProductsWithProductCollection = (
unsubscribe?: () => void
) => {
export const replaceProductsWithProductCollection = () => {
const queryBlocksCount =
select( 'core/block-editor' ).getGlobalBlockCount( 'core/query' );
if ( queryBlocksCount === 0 ) {
Expand All @@ -211,10 +218,32 @@ export const replaceProductsWithProductCollection = (
return;
}

const replaced = replaceProductsBlocks( productsBlockClientIds );
replaceProductsBlocks( productsBlockClientIds );
};

export const manualUpdate = () => {
setUpgradeStatus( getInitialStatusLSValue() );
replaceProductsWithProductCollection();
};

if ( unsubscribe && replaced ) {
// @todo: unsubscribe on user reverting migration
let unsubscribe: ( () => void ) | undefined;
export const disableAutoUpdate = () => {
if ( unsubscribe ) {
unsubscribe();
}
};
export const enableAutoUpdate = () => {
if ( isWpVersion( '6.1', '>=' ) ) {
const { status } = getUpgradeStatus();

if (
AUTO_REPLACE_PRODUCTS_WITH_PRODUCT_COLLECTION &&
status !== 'reverted' &&
! unsubscribe
) {
unsubscribe = subscribe( () => {
replaceProductsWithProductCollection();
}, 'core/block-editor' );
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,25 @@
import { getSettingWithCoercion } from '@woocommerce/settings';
import { type BlockInstance } from '@wordpress/blocks';
import { select } from '@wordpress/data';
import { isBoolean } from '@woocommerce/types';
import { isBoolean, isNumber } from '@woocommerce/types';

type GetBlocksClientIds = ( blocks: BlockInstance[] ) => string[];
export type IsBlockType = ( block: BlockInstance ) => boolean;
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;
};
/**
* Internal dependencies
*/
import { MIGRATION_STATUS_LS_KEY, getInitialStatusLSValue } from './constants';
import type {
IsBlockType,
GetBlocksClientIds,
UpgradeNoticeStatus,
} from './types';

const isProductsBlock: IsBlockType = ( block ) =>
block.name === 'core/query' &&
block.attributes.namespace === 'woocommerce/product-query';

const isProductCollectionBlock: IsBlockType = ( block ) =>
block.name === 'woocommerce/product-collection';
const isConvertedProductCollectionBlock: IsBlockType = ( block ) =>
block.name === 'woocommerce/product-collection' &&
block.attributes.convertedFromProducts;

const getBlockClientIdsByPredicate = (
blocks: BlockInstance[],
Expand All @@ -53,7 +45,7 @@ const getProductsBlockClientIds: GetBlocksClientIds = ( blocks ) =>
getBlockClientIdsByPredicate( blocks, isProductsBlock );

const getProductCollectionBlockClientIds: GetBlocksClientIds = ( blocks ) =>
getBlockClientIdsByPredicate( blocks, isProductCollectionBlock );
getBlockClientIdsByPredicate( blocks, isConvertedProductCollectionBlock );

const checkIfBlockCanBeInserted = (
clientId: string,
Expand All @@ -78,9 +70,35 @@ const postTemplateHasSupportForGridView = getSettingWithCoercion(
isBoolean
);

const getUpgradeStatus = (): UpgradeNoticeStatus => {
const status = window.localStorage.getItem( MIGRATION_STATUS_LS_KEY );
return status ? JSON.parse( status ) : getInitialStatusLSValue();
};

const setUpgradeStatus = ( newStatus: UpgradeNoticeStatus ) => {
window.localStorage.setItem(
MIGRATION_STATUS_LS_KEY,
JSON.stringify( newStatus )
);
};

const incrementUpgradeStatusDisplayCount = () => {
const status = getUpgradeStatus();
const displayCount = isNumber( status.displayCount )
? status.displayCount + 1
: 0;
setUpgradeStatus( {
...status,
displayCount,
} );
};

export {
getProductsBlockClientIds,
getProductCollectionBlockClientIds,
checkIfBlockCanBeInserted,
postTemplateHasSupportForGridView,
getUpgradeStatus,
setUpgradeStatus,
incrementUpgradeStatusDisplayCount,
};
29 changes: 29 additions & 0 deletions assets/js/blocks/migration-products-to-product-collection/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* External dependencies
*/
import { type BlockInstance } from '@wordpress/blocks';

export type GetBlocksClientIds = ( blocks: BlockInstance[] ) => string[];
export type IsBlockType = ( block: BlockInstance ) => boolean;
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;
};
export type UpgradeNoticeStatuses = 'notseen' | 'seen' | 'reverted';
export type UpgradeNoticeStatus = {
status: UpgradeNoticeStatuses;
time: number;
displayCount?: number;
};
2 changes: 1 addition & 1 deletion assets/js/blocks/product-collection/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"displayLayout": {
"type": "object"
},
"displayUpgradeNotice": {
"convertedFromProducts": {
"type": "boolean",
"default": false
}
Expand Down
Loading
Loading