Skip to content

Commit 3b133f2

Browse files
committed
Add types to getConfig()
1 parent bc753da commit 3b133f2

File tree

5 files changed

+42
-8
lines changed

5 files changed

+42
-8
lines changed

plugins/woocommerce/client/blocks/assets/js/atomic/blocks/product-elements/frontend.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import {
99
} from '@wordpress/interactivity';
1010
import '@woocommerce/stores/woocommerce/product-data';
1111
import type { ProductDataStore } from '@woocommerce/stores/woocommerce/product-data';
12-
import type { ProductData } from '@woocommerce/stores/woocommerce/cart';
12+
import type {
13+
ProductData,
14+
WooCommerceConfig,
15+
} from '@woocommerce/stores/woocommerce/cart';
1316
import { sanitizeHTML } from '@woocommerce/sanitize';
1417

1518
// Stores are locked to prevent 3PD usage until the API is stable.
@@ -63,7 +66,9 @@ const productElementStore = store(
6366
return undefined;
6467
}
6568

66-
const { products } = getConfig( 'woocommerce' );
69+
const { products } = getConfig(
70+
'woocommerce'
71+
) as WooCommerceConfig;
6772

6873
if ( ! products ) {
6974
return undefined;

plugins/woocommerce/client/blocks/assets/js/base/stores/woocommerce/cart.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
ApiErrorResponse,
1010
ApiResponse,
1111
CartResponseTotals,
12+
Currency,
1213
} from '@woocommerce/types';
1314
import type {
1415
Store as StoreNotices,
@@ -20,6 +21,17 @@ import type {
2021
*/
2122
import { triggerAddedToCartEvent } from './legacy-events';
2223

24+
export type WooCommerceConfig = {
25+
products?: {
26+
[ productId: number ]: ProductData;
27+
};
28+
messages?: {
29+
addedToCartText?: string;
30+
};
31+
placeholderImgSrc?: string;
32+
currency?: Currency;
33+
};
34+
2335
export type SelectedAttributes = Omit< CartVariationItem, 'raw_attribute' >;
2436

2537
export type OptimisticCartItem = {
@@ -386,7 +398,9 @@ const { state, actions } = store< Store >(
386398
preserveCartData: true,
387399
} );
388400

389-
const { messages } = getConfig( 'woocommerce' );
401+
const { messages } = getConfig(
402+
'woocommerce'
403+
) as WooCommerceConfig;
390404
if ( messages?.addedToCartText ) {
391405
wp?.a11y?.speak( messages.addedToCartText, 'polite' );
392406
}
@@ -537,7 +551,9 @@ const { state, actions } = store< Store >(
537551
preserveCartData: true,
538552
} );
539553

540-
const { messages } = getConfig( 'woocommerce' );
554+
const { messages } = getConfig(
555+
'woocommerce'
556+
) as WooCommerceConfig;
541557
if ( messages?.addedToCartText ) {
542558
wp?.a11y?.speak(
543559
messages.addedToCartText,

plugins/woocommerce/client/blocks/assets/js/blocks/add-to-cart-with-options/frontend.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
Store as WooCommerce,
88
SelectedAttributes,
99
ProductData,
10+
WooCommerceConfig,
1011
} from '@woocommerce/stores/woocommerce/cart';
1112
import '@woocommerce/stores/woocommerce/product-data';
1213
import type { Store as StoreNotices } from '@woocommerce/stores/store-notices';
@@ -74,7 +75,7 @@ export const getProductData = (
7475
let productId = id;
7576
let productData: ProductData | undefined;
7677

77-
const { products } = getConfig( 'woocommerce' );
78+
const { products } = getConfig( 'woocommerce' ) as WooCommerceConfig;
7879

7980
if ( selectedAttributes && selectedAttributes.length > 0 ) {
8081
if ( ! products || ! products[ id ] ) {

plugins/woocommerce/client/blocks/assets/js/blocks/mini-cart/iapi-frontend.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import {
1010
useRef,
1111
} from '@wordpress/interactivity';
1212
import '@woocommerce/stores/woocommerce/cart';
13-
import type { Store as WooCommerce } from '@woocommerce/stores/woocommerce/cart';
13+
import type {
14+
Store as WooCommerce,
15+
WooCommerceConfig,
16+
} from '@woocommerce/stores/woocommerce/cart';
1417
import Dinero from 'dinero.js';
1518

1619
/**
@@ -26,7 +29,9 @@ import { CartItem, Currency } from '../../types';
2629
const universalLock =
2730
'I acknowledge that using a private store means my plugin will inevitably break on the next store release.';
2831

29-
const { currency, placeholderImgSrc } = getConfig( 'woocommerce' );
32+
const { currency, placeholderImgSrc } = getConfig(
33+
'woocommerce'
34+
) as WooCommerceConfig;
3035
const {
3136
addToCartBehaviour,
3237
onCartClickBehaviour,
@@ -127,6 +132,10 @@ store< MiniCart >(
127132
},
128133

129134
get formattedSubtotal(): string {
135+
if ( ! currency ) {
136+
return '';
137+
}
138+
130139
const subtotal = displayCartPriceIncludingTax
131140
? parseInt( woocommerceState.cart.totals.total_items, 10 ) +
132141
parseInt(

plugins/woocommerce/client/blocks/assets/js/blocks/product-gallery/frontend.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
getConfig,
1010
} from '@wordpress/interactivity';
1111
import type { ProductDataStore } from '@woocommerce/stores/woocommerce/product-data';
12+
import type { WooCommerceConfig } from '@woocommerce/stores/woocommerce/cart';
1213

1314
/**
1415
* Internal dependencies
@@ -434,7 +435,9 @@ const productGallery = {
434435
return;
435436
}
436437

437-
const { products } = getConfig( 'woocommerce' );
438+
const { products } = getConfig(
439+
'woocommerce'
440+
) as WooCommerceConfig;
438441

439442
const productData =
440443
products?.[ productId ]?.variations?.[

0 commit comments

Comments
 (0)