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

Remove CustomerDataContext #7686

Merged
merged 4 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Expand Up @@ -11,7 +11,11 @@ import deprecated from '@wordpress/deprecated';
import LoadingMask from '@woocommerce/base-components/loading-mask';
import type { PaymentMethodInterface } from '@woocommerce/types';
import { useSelect, useDispatch } from '@wordpress/data';
import { CHECKOUT_STORE_KEY, PAYMENT_STORE_KEY } from '@woocommerce/block-data';
import {
CHECKOUT_STORE_KEY,
PAYMENT_STORE_KEY,
CART_STORE_KEY,
} from '@woocommerce/block-data';
import { ValidationInputError } from '@woocommerce/blocks-checkout';

/**
Expand All @@ -23,7 +27,6 @@ import { noticeContexts, responseTypes } from '../../event-emit';
import { useCheckoutEventsContext } from '../../providers/cart-checkout/checkout-events';
import { usePaymentEventsContext } from '../../providers/cart-checkout/payment-events';
import { useShippingDataContext } from '../../providers/cart-checkout/shipping';
import { useCustomerDataContext } from '../../providers/cart-checkout/customer';
import { prepareTotalItems } from './utils';
import { useShippingData } from '../shipping/use-shipping-data';

Expand Down Expand Up @@ -92,8 +95,11 @@ export const usePaymentMethodInterface = (): PaymentMethodInterface => {
selectShippingRate,
needsShipping,
} = useShippingData();
const { billingAddress, shippingAddress, setShippingAddress } =
useCustomerDataContext();

const { billingAddress, shippingAddress } = useSelect( ( select ) =>
select( CART_STORE_KEY ).getCustomerData()
);
const { setShippingAddress } = useDispatch( CART_STORE_KEY );
const { cartItems, cartFees, cartTotals, extensions } = useStoreCart();
const { appliedCoupons } = useStoreCartCoupons();
const currentCartTotals = useRef(
Expand Down
3 changes: 3 additions & 0 deletions assets/js/base/context/hooks/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"include": [
".",
"../../../../../packages/checkout/index.js",
"../providers/cart-checkout/checkout-events/index.tsx",
"../providers/cart-checkout/payment-events/index.tsx",
"../providers/cart-checkout/shipping/index.js"
],
"exclude": [ "**/test/**" ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
CHECKOUT_STORE_KEY,
PAYMENT_STORE_KEY,
VALIDATION_STORE_KEY,
CART_STORE_KEY,
} from '@woocommerce/block-data';
import {
getPaymentMethods,
Expand All @@ -31,7 +32,6 @@ import {
import { preparePaymentData, processCheckoutResponseHeaders } from './utils';
import { useCheckoutEventsContext } from './checkout-events';
import { useShippingDataContext } from './shipping';
import { useCustomerDataContext } from './customer';
import { useStoreCart } from '../../hooks/cart/use-store-cart';

/**
Expand Down Expand Up @@ -68,7 +68,11 @@ const CheckoutProcessor = () => {
( select ) => select( VALIDATION_STORE_KEY ).hasValidationErrors
);
const { shippingErrorStatus } = useShippingDataContext();
const { billingAddress, shippingAddress } = useCustomerDataContext();

const { billingAddress, shippingAddress } = useSelect( ( select ) =>
select( CART_STORE_KEY ).getCustomerData()
);

const { cartNeedsPayment, cartNeedsShipping, receiveCart } = useStoreCart();
const { createErrorNotice, removeNotice } = useDispatch( 'core/notices' );

Expand Down
31 changes: 14 additions & 17 deletions assets/js/base/context/providers/cart-checkout/checkout-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import BlockErrorBoundary from '@woocommerce/base-components/block-error-boundar
*/
import { PaymentEventsProvider } from './payment-events';
import { ShippingDataProvider } from './shipping';
import { CustomerDataProvider } from './customer';
import { CheckoutEventsProvider } from './checkout-events';
import CheckoutProcessor from './checkout-processor';

Expand All @@ -28,23 +27,21 @@ import CheckoutProcessor from './checkout-processor';
export const CheckoutProvider = ( { children, redirectUrl } ) => {
return (
<CheckoutEventsProvider redirectUrl={ redirectUrl }>
<CustomerDataProvider>
<ShippingDataProvider>
<PaymentEventsProvider>
{ children }
{ /* If the current user is an admin, we let BlockErrorBoundary render
<ShippingDataProvider>
<PaymentEventsProvider>
{ children }
{ /* If the current user is an admin, we let BlockErrorBoundary render
the error, or we simply die silently. */ }
<BlockErrorBoundary
renderError={
CURRENT_USER_IS_ADMIN ? null : () => null
}
>
<PluginArea scope="woocommerce-checkout" />
</BlockErrorBoundary>
<CheckoutProcessor />
</PaymentEventsProvider>
</ShippingDataProvider>
</CustomerDataProvider>
<BlockErrorBoundary
renderError={
CURRENT_USER_IS_ADMIN ? null : () => null
}
>
<PluginArea scope="woocommerce-checkout" />
</BlockErrorBoundary>
<CheckoutProcessor />
</PaymentEventsProvider>
</ShippingDataProvider>
</CheckoutEventsProvider>
);
};

This file was deleted.

42 changes: 0 additions & 42 deletions assets/js/base/context/providers/cart-checkout/customer/index.tsx

This file was deleted.

1 change: 0 additions & 1 deletion assets/js/base/context/providers/cart-checkout/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export * from './payment-events';
export * from './shipping';
export * from './customer';
export * from './checkout-events';
export * from './cart';
export * from './checkout-processor';
Expand Down
13 changes: 5 additions & 8 deletions assets/js/data/payment/utils/check-payment-methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ import {
EMPTY_CART_ERRORS,
EMPTY_CART_ITEM_ERRORS,
EMPTY_EXTENSIONS,
} from '../../constants';
import {
defaultBillingAddress,
defaultShippingAddress,
} from '../../../base/context/providers/cart-checkout/customer/constants';
} from '../../../data/constants';
import { defaultCartState } from '../../../data/cart/default-state';

export const checkPaymentMethodsCanPay = async ( express = false ) => {
const isEditor = !! select( 'core/editor' );
Expand Down Expand Up @@ -122,9 +119,9 @@ export const checkPaymentMethodsCanPay = async ( express = false ) => {
cartTotals: previewCart.totals,
cartIsLoading: false,
cartErrors: EMPTY_CART_ERRORS,
billingData: defaultBillingAddress,
billingAddress: defaultBillingAddress,
shippingAddress: defaultShippingAddress,
billingData: defaultCartState.cartData.billingAddress,
billingAddress: defaultCartState.cartData.billingAddress,
shippingAddress: defaultCartState.cartData.shippingAddress,
extensions: EMPTY_EXTENSIONS,
shippingRates: previewCart.shipping_rates,
isLoadingRates: false,
Expand Down
6 changes: 3 additions & 3 deletions checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1049,10 +1049,10 @@
<file name="assets/js/base/context/providers/cart-checkout/checkout-provider.js">
<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.
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" />
<error line="30" column="27" severity="error" message="Type &apos;string | undefined&apos; is not assignable to type &apos;string&apos;.
<error line="29" column="27" severity="error" message="Type &apos;string | undefined&apos; is not assignable to type &apos;string&apos;.
Type &apos;undefined&apos; is not assignable to type &apos;string&apos;." source="TS2322" />
<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" />
<error line="38" column="8" severity="error" message="No overload matches this call.
<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" />
<error line="36" column="7" severity="error" message="No overload matches this call.
Overload 1 of 2, &apos;(props: BlockErrorBoundaryProps | Readonly&lt;BlockErrorBoundaryProps&gt;): BlockErrorBoundary&apos;, gave the following error.
Type &apos;(() =&gt; null) | null&apos; is not assignable to type &apos;(props: RenderErrorProps) =&gt; ReactNode&apos;.
Type &apos;null&apos; is not assignable to type &apos;(props: RenderErrorProps) =&gt; ReactNode&apos;.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,6 @@ Much of the data and api interface for components in the Checkout Block are cons

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

### Customer Data Context

The customer data context exposes the api interfaces for the following things via the `useCustomerDataContext` hook:

- `billingData`: The currently set billing data.
- `setBillingData`: A state updated for updating the billing data state with new billing data.
- `shippingAddress`: The current set shipping address.
- `setShippingAddress`: A function for setting the shipping address. This will trigger shipping rates updates.

### Shipping Method Data context

The shipping method data context exposes the api interfaces for the following things (typedef `ShippingMethodDataContext`) via the `useShippingMethodData` hook:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,3 @@ Returns true if the checkout is processing, and false otherwise.
🐞 Found a mistake, or have a suggestion? [Leave feedback about this document here.](https://github.com/woocommerce/woocommerce-blocks/issues/new?assignees=&labels=type%3A+documentation&template=--doc-feedback.md&title=Feedback%20on%20./docs/third-party-developers/extensibility/data-store/checkout.md)

<!-- /FEEDBACK -->