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

Commit 4c86da4

Browse files
authored
Switch to rest_preload_api_request for API hydration in cart and checkout blocks. (#4090)
* Introduce hydrate_api_request * Consume preloadedApiRequests * no need to set false here
1 parent 282dafa commit 4c86da4

File tree

6 files changed

+41
-20
lines changed

6 files changed

+41
-20
lines changed

assets/js/base/context/providers/cart-checkout/checkout-state/constants.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ export const STATUS = {
1515
AFTER_PROCESSING: 'after_processing',
1616
};
1717

18-
const checkoutData = getSetting( 'checkoutData', {
18+
const preloadedApiRequests = getSetting( 'preloadedApiRequests', {} );
19+
const checkoutData = {
1920
order_id: 0,
2021
customer_id: 0,
21-
} );
22+
...( preloadedApiRequests[ '/wc/store/checkout' ]?.body || {} ),
23+
};
2224

2325
export const DEFAULT_STATE = {
2426
redirectUrl: '',

assets/js/blocks/cart-checkout/checkout/checkout-order-error/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ const cartItemErrorCodes = [
3434
* checkout block.
3535
*/
3636
const CheckoutOrderError = () => {
37-
const checkoutData = getSetting( 'checkoutData', {} );
37+
const preloadedApiRequests = getSetting( 'preloadedApiRequests', {} );
38+
const checkoutData = {
39+
code: '',
40+
message: '',
41+
...( preloadedApiRequests[ '/wc/store/checkout' ]?.body || {} ),
42+
};
43+
3844
const errorData = {
3945
code: checkoutData.code || 'unknown',
4046
message:

assets/js/hocs/with-store-cart-api-hydration.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@ import { LAST_CART_UPDATE_TIMESTAMP_KEY } from '../data/cart/constants';
1717
* Makes cart data available without an API request to wc/store/cart/.
1818
*/
1919
const useStoreCartApiHydration = () => {
20-
const cartData = useRef( getSetting( 'cartData' ) );
20+
const preloadedApiRequests = useRef(
21+
getSetting( 'preloadedApiRequests', {} )
22+
);
2123
const { setIsCartDataStale } = useDispatch( CART_STORE_KEY );
2224

2325
useSelect( ( select, registry ) => {
24-
if ( ! cartData.current ) {
26+
const cartData = preloadedApiRequests.current[ '/wc/store/cart' ]?.body;
27+
28+
if ( ! cartData ) {
2529
return;
2630
}
31+
2732
const { isResolving, hasFinishedResolution, isCartDataStale } = select(
2833
CART_STORE_KEY
2934
);
@@ -46,7 +51,7 @@ const useStoreCartApiHydration = () => {
4651
if ( lastCartUpdateRaw ) {
4752
const lastCartUpdate = parseFloat( lastCartUpdateRaw );
4853
const cartGeneratedTimestamp = parseFloat(
49-
cartData.current.generated_timestamp
54+
cartData.generated_timestamp
5055
);
5156

5257
const needsUpdateFromAPI =
@@ -72,10 +77,10 @@ const useStoreCartApiHydration = () => {
7277
! hasFinishedResolution( 'getCartData', [] )
7378
) {
7479
startResolution( 'getCartData', [] );
75-
if ( cartData.current?.code?.includes( 'error' ) ) {
76-
receiveError( cartData.current );
80+
if ( cartData?.code?.includes( 'error' ) ) {
81+
receiveError( cartData );
7782
} else {
78-
receiveCart( cartData.current );
83+
receiveCart( cartData );
7984
}
8085
finishResolution( 'getCartData', [] );
8186
}

src/Assets/AssetDataRegistry.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,20 @@ public function add( $key, $data, $check_key_exists = false ) {
272272
}
273273
}
274274

275+
/**
276+
* Hydrate from API.
277+
*
278+
* @param string $path REST API path to preload.
279+
*/
280+
public function hydrate_api_request( $path ) {
281+
if ( ! isset( $this->data['preloadedApiRequests'] ) ) {
282+
$this->data['preloadedApiRequests'] = [];
283+
}
284+
if ( ! isset( $this->data['preloadedApiRequests'][ $path ] ) ) {
285+
$this->data['preloadedApiRequests'] = rest_preload_api_request( $this->data['preloadedApiRequests'], $path );
286+
}
287+
}
288+
275289
/**
276290
* Adds a page permalink to the data registry.
277291
*

src/BlockTypes/Cart.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,7 @@ protected function deep_sort_with_accents( $array ) {
159159
* Hydrate the cart block with data from the API.
160160
*/
161161
protected function hydrate_from_api() {
162-
if ( ! $this->asset_data_registry->exists( 'cartData' ) ) {
163-
$this->asset_data_registry->add( 'cartData', WC()->api->get_endpoint_data( '/wc/store/cart' ) );
164-
}
162+
$this->asset_data_registry->hydrate_api_request( '/wc/store/cart' );
165163
}
166164

167165
/**

src/BlockTypes/Checkout.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,10 @@ protected function hydrate_from_api() {
216216
// Controller and converted to exceptions.
217217
wc_print_notices();
218218

219-
if ( ! $this->asset_data_registry->exists( 'cartData' ) ) {
220-
$this->asset_data_registry->add( 'cartData', WC()->api->get_endpoint_data( '/wc/store/cart' ) );
221-
}
222-
if ( ! $this->asset_data_registry->exists( 'checkoutData' ) ) {
223-
add_filter( 'woocommerce_store_api_disable_nonce_check', '__return_true' );
224-
$this->asset_data_registry->add( 'checkoutData', WC()->api->get_endpoint_data( '/wc/store/checkout' ) );
225-
remove_filter( 'woocommerce_store_api_disable_nonce_check', '__return_true' );
226-
}
219+
add_filter( 'woocommerce_store_api_disable_nonce_check', '__return_true' );
220+
$this->asset_data_registry->hydrate_api_request( '/wc/store/cart' );
221+
$this->asset_data_registry->hydrate_api_request( '/wc/store/checkout' );
222+
remove_filter( 'woocommerce_store_api_disable_nonce_check', '__return_true' );
227223
}
228224

229225
/**

0 commit comments

Comments
 (0)