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

Commit 1785fa9

Browse files
Remove CustomerDataContext (#7686)
* Remove CustomerDataContext * bot: update checkstyle.xml Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 3e55755 commit 1785fa9

File tree

10 files changed

+41
-117
lines changed

10 files changed

+41
-117
lines changed

assets/js/base/context/hooks/payment-methods/use-payment-method-interface.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import deprecated from '@wordpress/deprecated';
1111
import LoadingMask from '@woocommerce/base-components/loading-mask';
1212
import type { PaymentMethodInterface } from '@woocommerce/types';
1313
import { useSelect, useDispatch } from '@wordpress/data';
14-
import { CHECKOUT_STORE_KEY, PAYMENT_STORE_KEY } from '@woocommerce/block-data';
14+
import {
15+
CHECKOUT_STORE_KEY,
16+
PAYMENT_STORE_KEY,
17+
CART_STORE_KEY,
18+
} from '@woocommerce/block-data';
1519
import { ValidationInputError } from '@woocommerce/blocks-checkout';
1620

1721
/**
@@ -23,7 +27,6 @@ import { noticeContexts, responseTypes } from '../../event-emit';
2327
import { useCheckoutEventsContext } from '../../providers/cart-checkout/checkout-events';
2428
import { usePaymentEventsContext } from '../../providers/cart-checkout/payment-events';
2529
import { useShippingDataContext } from '../../providers/cart-checkout/shipping';
26-
import { useCustomerDataContext } from '../../providers/cart-checkout/customer';
2730
import { prepareTotalItems } from './utils';
2831
import { useShippingData } from '../shipping/use-shipping-data';
2932

@@ -92,8 +95,11 @@ export const usePaymentMethodInterface = (): PaymentMethodInterface => {
9295
selectShippingRate,
9396
needsShipping,
9497
} = useShippingData();
95-
const { billingAddress, shippingAddress, setShippingAddress } =
96-
useCustomerDataContext();
98+
99+
const { billingAddress, shippingAddress } = useSelect( ( select ) =>
100+
select( CART_STORE_KEY ).getCustomerData()
101+
);
102+
const { setShippingAddress } = useDispatch( CART_STORE_KEY );
97103
const { cartItems, cartFees, cartTotals, extensions } = useStoreCart();
98104
const { appliedCoupons } = useStoreCartCoupons();
99105
const currentCartTotals = useRef(

assets/js/base/context/hooks/tsconfig.json

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"include": [
55
".",
66
"../../../../../packages/checkout/index.js",
7+
"../providers/cart-checkout/checkout-events/index.tsx",
8+
"../providers/cart-checkout/payment-events/index.tsx",
9+
"../providers/cart-checkout/shipping/index.js"
710
],
811
"exclude": [ "**/test/**" ]
912
}

assets/js/base/context/providers/cart-checkout/checkout-processor.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
CHECKOUT_STORE_KEY,
2020
PAYMENT_STORE_KEY,
2121
VALIDATION_STORE_KEY,
22+
CART_STORE_KEY,
2223
} from '@woocommerce/block-data';
2324
import {
2425
getPaymentMethods,
@@ -31,7 +32,6 @@ import {
3132
import { preparePaymentData, processCheckoutResponseHeaders } from './utils';
3233
import { useCheckoutEventsContext } from './checkout-events';
3334
import { useShippingDataContext } from './shipping';
34-
import { useCustomerDataContext } from './customer';
3535
import { useStoreCart } from '../../hooks/cart/use-store-cart';
3636

3737
/**
@@ -72,7 +72,11 @@ const CheckoutProcessor = () => {
7272
( select ) => select( VALIDATION_STORE_KEY ).hasValidationErrors
7373
);
7474
const { shippingErrorStatus } = useShippingDataContext();
75-
const { billingAddress, shippingAddress } = useCustomerDataContext();
75+
76+
const { billingAddress, shippingAddress } = useSelect( ( select ) =>
77+
select( CART_STORE_KEY ).getCustomerData()
78+
);
79+
7680
const { cartNeedsPayment, cartNeedsShipping, receiveCart } = useStoreCart();
7781
const { createErrorNotice, removeNotice } = useDispatch( 'core/notices' );
7882

assets/js/base/context/providers/cart-checkout/checkout-provider.js

+14-17
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import BlockErrorBoundary from '@woocommerce/base-components/block-error-boundar
99
*/
1010
import { PaymentEventsProvider } from './payment-events';
1111
import { ShippingDataProvider } from './shipping';
12-
import { CustomerDataProvider } from './customer';
1312
import { CheckoutEventsProvider } from './checkout-events';
1413
import CheckoutProcessor from './checkout-processor';
1514

@@ -28,23 +27,21 @@ import CheckoutProcessor from './checkout-processor';
2827
export const CheckoutProvider = ( { children, redirectUrl } ) => {
2928
return (
3029
<CheckoutEventsProvider redirectUrl={ redirectUrl }>
31-
<CustomerDataProvider>
32-
<ShippingDataProvider>
33-
<PaymentEventsProvider>
34-
{ children }
35-
{ /* If the current user is an admin, we let BlockErrorBoundary render
30+
<ShippingDataProvider>
31+
<PaymentEventsProvider>
32+
{ children }
33+
{ /* If the current user is an admin, we let BlockErrorBoundary render
3634
the error, or we simply die silently. */ }
37-
<BlockErrorBoundary
38-
renderError={
39-
CURRENT_USER_IS_ADMIN ? null : () => null
40-
}
41-
>
42-
<PluginArea scope="woocommerce-checkout" />
43-
</BlockErrorBoundary>
44-
<CheckoutProcessor />
45-
</PaymentEventsProvider>
46-
</ShippingDataProvider>
47-
</CustomerDataProvider>
35+
<BlockErrorBoundary
36+
renderError={
37+
CURRENT_USER_IS_ADMIN ? null : () => null
38+
}
39+
>
40+
<PluginArea scope="woocommerce-checkout" />
41+
</BlockErrorBoundary>
42+
<CheckoutProcessor />
43+
</PaymentEventsProvider>
44+
</ShippingDataProvider>
4845
</CheckoutEventsProvider>
4946
);
5047
};

assets/js/base/context/providers/cart-checkout/customer/constants.ts

-31
This file was deleted.

assets/js/base/context/providers/cart-checkout/customer/index.tsx

-42
This file was deleted.

assets/js/base/context/providers/cart-checkout/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export * from './payment-events';
22
export * from './shipping';
3-
export * from './customer';
43
export * from './checkout-events';
54
export * from './cart';
65
export * from './checkout-processor';

assets/js/data/payment/utils/check-payment-methods.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,8 @@ import {
3030
EMPTY_CART_ERRORS,
3131
EMPTY_CART_ITEM_ERRORS,
3232
EMPTY_EXTENSIONS,
33-
} from '../../constants';
34-
import {
35-
defaultBillingAddress,
36-
defaultShippingAddress,
37-
} from '../../../base/context/providers/cart-checkout/customer/constants';
33+
} from '../../../data/constants';
34+
import { defaultCartState } from '../../../data/cart/default-state';
3835

3936
export const checkPaymentMethodsCanPay = async ( express = false ) => {
4037
const isEditor = !! select( 'core/editor' );
@@ -122,9 +119,9 @@ export const checkPaymentMethodsCanPay = async ( express = false ) => {
122119
cartTotals: previewCart.totals,
123120
cartIsLoading: false,
124121
cartErrors: EMPTY_CART_ERRORS,
125-
billingData: defaultBillingAddress,
126-
billingAddress: defaultBillingAddress,
127-
shippingAddress: defaultShippingAddress,
122+
billingData: defaultCartState.cartData.billingAddress,
123+
billingAddress: defaultCartState.cartData.billingAddress,
124+
shippingAddress: defaultCartState.cartData.shippingAddress,
128125
extensions: EMPTY_EXTENSIONS,
129126
shippingRates: previewCart.shipping_rates,
130127
isLoadingRates: false,

checkstyle.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1049,10 +1049,10 @@
10491049
<file name="assets/js/base/context/providers/cart-checkout/checkout-provider.js">
10501050
<error line="4" column="28" severity="error" message="Could not find a declaration file for module &apos;@wordpress/plugins&apos;. &apos;/home/runner/work/woocommerce-blocks/woocommerce-blocks/node_modules/@wordpress/plugins/build/index.js&apos; implicitly has an &apos;any&apos; type.
10511051
Try `npm i --save-dev @types/wordpress__plugins` if it exists or add a new declaration (.d.ts) file containing `declare module &apos;@wordpress/plugins&apos;;`" source="TS7016" />
1052-
<error line="30" column="27" severity="error" message="Type &apos;string | undefined&apos; is not assignable to type &apos;string&apos;.
1052+
<error line="29" column="27" severity="error" message="Type &apos;string | undefined&apos; is not assignable to type &apos;string&apos;.
10531053
Type &apos;undefined&apos; is not assignable to type &apos;string&apos;." source="TS2322" />
1054-
<error line="31" column="4" severity="error" message="Type &apos;Element&apos; is missing the following properties from type &apos;ReactChildren&apos;: map, forEach, count, only, toArray" source="TS2739" />
1055-
<error line="38" column="8" severity="error" message="No overload matches this call.
1054+
<error line="30" column="4" severity="error" message="Type &apos;Element&apos; is missing the following properties from type &apos;ReactChildren&apos;: map, forEach, count, only, toArray" source="TS2739" />
1055+
<error line="36" column="7" severity="error" message="No overload matches this call.
10561056
Overload 1 of 2, &apos;(props: BlockErrorBoundaryProps | Readonly&lt;BlockErrorBoundaryProps&gt;): BlockErrorBoundary&apos;, gave the following error.
10571057
Type &apos;(() =&gt; null) | null&apos; is not assignable to type &apos;(props: RenderErrorProps) =&gt; ReactNode&apos;.
10581058
Type &apos;null&apos; is not assignable to type &apos;(props: RenderErrorProps) =&gt; ReactNode&apos;.

docs/internal-developers/block-client-apis/checkout/checkout-api.md

-9
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,6 @@ Much of the data and api interface for components in the Checkout Block are cons
7979

8080
You can find type definitions (`typedef`) for contexts in [this file](../../../../assets/js/types/type-defs/contexts.js).
8181

82-
### Customer Data Context
83-
84-
The customer data context exposes the api interfaces for the following things via the `useCustomerDataContext` hook:
85-
86-
- `billingData`: The currently set billing data.
87-
- `setBillingData`: A state updated for updating the billing data state with new billing data.
88-
- `shippingAddress`: The current set shipping address.
89-
- `setShippingAddress`: A function for setting the shipping address. This will trigger shipping rates updates.
90-
9182
### Shipping Method Data context
9283

9384
The shipping method data context exposes the api interfaces for the following things (typedef `ShippingMethodDataContext`) via the `useShippingMethodData` hook:

0 commit comments

Comments
 (0)