Skip to content

Commit

Permalink
Remove hydration hocs in favour of apiFetch Middlewares (woocommerce#…
Browse files Browse the repository at this point in the history
…5022)

* Remove withRestApiHydration

* Preload checkout data via setting - server data is required for this block

* Handle cart hydration using createPreloadingMiddleware which removes the need for HOCs

* Rename variable

* Remove withStoreCartApiHydration and timestamp checking

* Empty test file
  • Loading branch information
mikejolley authored and jonny-bull committed Dec 16, 2021
1 parent dbc6d86 commit a58b569
Show file tree
Hide file tree
Showing 23 changed files with 283 additions and 755 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ export enum STATUS {
AFTER_PROCESSING = 'after_processing',
}

const preloadedApiRequests = getSetting( 'preloadedApiRequests', {} ) as Record<
const preloadedCheckoutData = getSetting( 'checkoutData', {} ) as Record<
string,
{ body: Record< string, unknown > }
unknown
>;

const checkoutData = {
order_id: 0,
customer_id: 0,
...( preloadedApiRequests[ '/wc/store/checkout' ]?.body || {} ),
...( preloadedCheckoutData || {} ),
};

export const DEFAULT_CHECKOUT_STATE_DATA: CheckoutStateContextType = {
Expand Down
3 changes: 1 addition & 2 deletions assets/js/blocks/active-filters/frontend.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import { withRestApiHydration } from '@woocommerce/block-hocs';
import { renderFrontend } from '@woocommerce/base-utils';

/**
Expand All @@ -21,6 +20,6 @@ const getProps = ( el ) => {

renderFrontend( {
selector: '.wp-block-woocommerce-active-filters',
Block: withRestApiHydration( Block ),
Block,
getProps,
} );
3 changes: 1 addition & 2 deletions assets/js/blocks/attribute-filter/frontend.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import { withRestApiHydration } from '@woocommerce/block-hocs';
import { renderFrontend } from '@woocommerce/base-utils';

/**
Expand All @@ -25,6 +24,6 @@ const getProps = ( el ) => {

renderFrontend( {
selector: '.wp-block-woocommerce-attribute-filter',
Block: withRestApiHydration( Block ),
Block,
getProps,
} );
6 changes: 1 addition & 5 deletions assets/js/blocks/cart-checkout/cart/frontend.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/**
* External dependencies
*/
import {
withStoreCartApiHydration,
withRestApiHydration,
} from '@woocommerce/block-hocs';
import { getValidBlockAttributes } from '@woocommerce/base-utils';
import { Children, cloneElement, isValidElement } from '@wordpress/element';
import { useStoreCart } from '@woocommerce/base-context';
Expand Down Expand Up @@ -45,7 +41,7 @@ const Wrapper = ( { children } ) => {
};

renderParentBlock( {
Block: withStoreCartApiHydration( withRestApiHydration( Block ) ),
Block,
blockName,
selector: '.wp-block-woocommerce-cart',
getProps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const cartItemErrorCodes = [
GENERIC_CART_ITEM_ERROR,
];

const preloadedCheckoutData = getSetting( 'checkoutData', {} );

/**
* When an order was not created for the checkout, for example, when an item
* was out of stock, this component will be shown instead of the checkout form.
Expand All @@ -35,11 +37,10 @@ const cartItemErrorCodes = [
* checkout block.
*/
const CheckoutOrderError = () => {
const preloadedApiRequests = getSetting( 'preloadedApiRequests', {} );
const checkoutData = {
code: '',
message: '',
...( preloadedApiRequests[ '/wc/store/checkout' ]?.body || {} ),
...( preloadedCheckoutData || {} ),
};

const errorData = {
Expand Down
6 changes: 1 addition & 5 deletions assets/js/blocks/cart-checkout/checkout/frontend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import {
useValidation,
} from '@woocommerce/base-context/hooks';
import { getRegisteredBlockComponents } from '@woocommerce/blocks-registry';
import {
withStoreCartApiHydration,
withRestApiHydration,
} from '@woocommerce/block-hocs';
import { renderParentBlock } from '@woocommerce/atomic-utils';

/**
Expand Down Expand Up @@ -57,7 +53,7 @@ const Wrapper = ( {
};

renderParentBlock( {
Block: withStoreCartApiHydration( withRestApiHydration( Block ) ),
Block,
blockName,
selector: '.wp-block-woocommerce-checkout',
getProps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { renderFrontend } from '@woocommerce/base-utils';
/**
* Internal dependencies
*/
import withMiniCartConditionalHydration from './with-mini-cart-conditional-hydration';
import MiniCartBlock from './block';
import './style.scss';

Expand All @@ -28,7 +27,7 @@ const renderMiniCartFrontend = () => {

renderFrontend( {
selector: '.wc-block-mini-cart',
Block: withMiniCartConditionalHydration( MiniCartBlock ),
Block: MiniCartBlock,
getProps: ( el: HTMLElement ) => ( {
isDataOutdated: el.dataset.isDataOutdated,
isInitiallyOpen: el.dataset.isInitiallyOpen === 'true',
Expand Down

This file was deleted.

3 changes: 1 addition & 2 deletions assets/js/blocks/price-filter/frontend.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import { withRestApiHydration } from '@woocommerce/block-hocs';
import { renderFrontend } from '@woocommerce/base-utils';

/**
Expand All @@ -20,6 +19,6 @@ const getProps = ( el ) => {

renderFrontend( {
selector: '.wp-block-woocommerce-price-filter',
Block: withRestApiHydration( Block ),
Block,
getProps,
} );
3 changes: 1 addition & 2 deletions assets/js/blocks/products/all-products/frontend.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import { withRestApiHydration } from '@woocommerce/block-hocs';
import { StoreNoticesProvider } from '@woocommerce/base-context';
import { renderFrontend } from '@woocommerce/base-utils';

Expand Down Expand Up @@ -29,6 +28,6 @@ const getProps = ( el ) => ( {

renderFrontend( {
selector: '.wp-block-woocommerce-all-products',
Block: withRestApiHydration( AllProductsFrontend ),
Block: AllProductsFrontend,
getProps,
} );
3 changes: 1 addition & 2 deletions assets/js/blocks/stock-filter/frontend.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import { withRestApiHydration } from '@woocommerce/block-hocs';
import { renderFrontend } from '@woocommerce/base-utils';

/**
Expand All @@ -22,6 +21,6 @@ const getProps = ( el ) => {

renderFrontend( {
selector: '.wp-block-woocommerce-stock-filter',
Block: withRestApiHydration( Block ),
Block,
getProps,
} );
1 change: 0 additions & 1 deletion assets/js/data/cart/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ export const CART_API_ERROR = {
status: 500,
},
};
export const LAST_CART_UPDATE_TIMESTAMP_KEY = 'wc-blocks_cart_update_timestamp';
2 changes: 0 additions & 2 deletions assets/js/hocs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ export { default as withProduct } from './with-product';
export { default as withProductVariations } from './with-product-variations';
export { default as withSearchedProducts } from './with-searched-products';
export { default as withTransformSingleSelectToMultipleSelect } from './with-transform-single-select-to-multiple-select';
export { default as withStoreCartApiHydration } from './with-store-cart-api-hydration';
export { default as withRestApiHydration } from './with-rest-api-hydration';
55 changes: 0 additions & 55 deletions assets/js/hocs/with-rest-api-hydration.js

This file was deleted.

102 changes: 0 additions & 102 deletions assets/js/hocs/with-store-cart-api-hydration.js

This file was deleted.

Loading

0 comments on commit a58b569

Please sign in to comment.