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

Add new hooks related to payment methods and checkout and remove obsolete. #1929

Merged
merged 5 commits into from
Mar 11, 2020
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 @@ -2,34 +2,30 @@
* External dependencies
*/
import {
useCheckoutData,
usePaymentEvents,
useExpressPaymentMethods,
usePaymentMethodInterface,
} from '@woocommerce/base-hooks';
import { cloneElement, isValidElement } from '@wordpress/element';
import { useCheckoutContext } from '@woocommerce/base-context';

const ExpressPaymentMethods = () => {
const [ checkoutData ] = useCheckoutData();
const { isEditor } = useCheckoutContext();
const { dispatch, select } = usePaymentEvents();
const paymentMethodInterface = usePaymentMethodInterface();
// not implementing isInitialized here because it's utilized further
// up in the tree for express payment methods. We won't even get here if
// there's no payment methods after initialization.
const { paymentMethods } = useExpressPaymentMethods();
const paymentMethodSlugs = Object.keys( paymentMethods );
const paymentMethodIds = Object.keys( paymentMethods );
const content =
paymentMethodSlugs.length > 0 ? (
paymentMethodSlugs.map( ( slug ) => {
paymentMethodIds.length > 0 ? (
paymentMethodIds.map( ( id ) => {
const expressPaymentMethod = isEditor
? paymentMethods[ slug ].edit
: paymentMethods[ slug ].activeContent;
const paymentEvents = { dispatch, select };
? paymentMethods[ id ].edit
: paymentMethods[ id ].activeContent;
return isValidElement( expressPaymentMethod ) ? (
<li key={ slug } id={ `express-payment-method-${ slug }` }>
<li key={ id } id={ `express-payment-method-${ id }` }>
{ cloneElement( expressPaymentMethod, {
checkoutData,
paymentEvents,
...paymentMethodInterface,
} ) }
</li>
) : null;
Expand Down
38 changes: 17 additions & 21 deletions assets/js/base/components/payment-methods/payment-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
* External dependencies
*/
import {
useCheckoutData,
usePaymentEvents,
useActivePaymentMethod,
usePaymentMethods,
usePaymentMethodInterface,
} from '@woocommerce/base-hooks';
import {
useCallback,
Expand All @@ -31,12 +29,12 @@ const noPaymentMethodTab = () => {
};

const createTabs = ( paymentMethods ) => {
const paymentMethodsKeys = Object.keys( paymentMethods );
return paymentMethodsKeys.length > 0
? paymentMethodsKeys.map( ( key ) => {
const { label, ariaLabel } = paymentMethods[ key ];
const paymentMethodIds = Object.keys( paymentMethods );
return paymentMethodIds.length > 0
? paymentMethodIds.map( ( id ) => {
const { label, ariaLabel } = paymentMethods[ id ];
return {
name: key,
name: id,
title: () => label,
ariaLabel,
};
Expand Down Expand Up @@ -64,38 +62,36 @@ const getPaymentMethod = ( id, paymentMethods, isEditor ) => {
};

const PaymentMethods = () => {
const [ checkoutData ] = useCheckoutData();
const { isEditor } = useCheckoutContext();
const { dispatch, select } = usePaymentEvents();
const { isInitialized, paymentMethods } = usePaymentMethods();
const currentPaymentMethods = useRef( paymentMethods );
const {
activePaymentMethod,
setActivePaymentMethod,
...paymentMethodInterface
} = usePaymentMethodInterface();
const currentPaymentMethodInterface = useRef( paymentMethodInterface );

// update ref on changes
useEffect( () => {
currentPaymentMethods.current = paymentMethods;
}, [ paymentMethods ] );

const {
activePaymentMethod,
setActivePaymentMethod,
} = useActivePaymentMethod();
currentPaymentMethodInterface.current = paymentMethodInterface;
}, [ paymentMethods, paymentMethodInterface, activePaymentMethod ] );
const getRenderedTab = useCallback(
() => ( selectedTab ) => {
const paymentMethod = getPaymentMethod(
selectedTab,
currentPaymentMethods.current,
isEditor
);
const paymentEvents = { dispatch, select };
return paymentMethod
? cloneElement( paymentMethod, {
isActive: true,
checkoutData,
paymentEvents,
activePaymentMethod: paymentMethod.id,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen that payment methods sometimes have a slug, other times an id (and these is used as key and name in components). If we can standardise on one name for this id / slug (slug?) that might help simplify this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call on this. I looked for places where this was inconsistent and addressed (see ff5de1)

...currentPaymentMethodInterface.current,
} )
: null;
},
[ checkoutData, dispatch, select, isEditor ]
[ isEditor, activePaymentMethod ]
);
if (
! isInitialized ||
Expand Down
3 changes: 3 additions & 0 deletions assets/js/base/hooks/cart/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './use-store-cart';
export * from './use-store-cart-coupons';
export * from './use-store-cart-item-quantity';
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const defaultCartData = {
cartTotals: {},
cartIsLoading: true,
cartErrors: [],
shippingRates: [],
};

/**
Expand Down
6 changes: 2 additions & 4 deletions assets/js/base/hooks/checkout/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
export { default as useCheckoutEvents } from './use-checkout-events';
export { default as useCheckoutNotices } from './use-checkout-notices';
export { default as useCheckoutRedirectUrls } from './use-checkout-redirect-urls';
export { default as useCheckoutData } from './use-checkout-data';
export * from './use-checkout-redirect-url';
export * from './use-checkout-submit';
29 changes: 0 additions & 29 deletions assets/js/base/hooks/checkout/use-checkout-data.js

This file was deleted.

41 changes: 0 additions & 41 deletions assets/js/base/hooks/checkout/use-checkout-events.js

This file was deleted.

24 changes: 0 additions & 24 deletions assets/js/base/hooks/checkout/use-checkout-notices.js

This file was deleted.

This file was deleted.

16 changes: 16 additions & 0 deletions assets/js/base/hooks/checkout/use-checkout-redirect-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* External dependencies
*/
import { useCheckoutContext } from '@woocommerce/base-context';

/**
* Returns redirect url interface from checkout context.
*/
export const useCheckoutRedirectUrl = () => {
const { redirectUrl, dispatchActions } = useCheckoutContext();

return {
redirectUrl,
setRedirectUrl: dispatchActions.setRedirectUrl,
};
};
22 changes: 0 additions & 22 deletions assets/js/base/hooks/checkout/use-checkout-redirect-urls.js

This file was deleted.

12 changes: 12 additions & 0 deletions assets/js/base/hooks/checkout/use-checkout-submit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* External dependencies
*/
import { useCheckoutContext } from '@woocommerce/base-context';

/**
* Returns the submitLabel and onSubmit interface from the checkout context
*/
export const useCheckoutSubmit = () => {
const { submitLabel, onSubmit } = useCheckoutContext();
return { submitLabel, onSubmit };
};
20 changes: 9 additions & 11 deletions assets/js/base/hooks/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
export * from './use-query-state';
export * from './use-shallow-equal';
export * from './use-store-cart';
export * from './use-store-cart-coupons';
export * from './use-store-cart-item-quantity';
export * from './use-store-products';
export * from './use-store-notices';
export * from './cart';
export * from './checkout';
export * from './order';
export * from './payment-methods';
export * from './shipping';
export * from './use-collection';
export * from './use-collection-header';
export * from './use-collection-data';
export * from './use-previous';
export * from './checkout';
export * from './payment-methods';
export * from './use-shipping-rates';
export * from './use-select-shipping-rate';
export * from './use-shallow-equal';
export * from './use-store-products';
export * from './use-store-notices';
export * from './use-query-state';
2 changes: 2 additions & 0 deletions assets/js/base/hooks/order/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './use-store-order';
export * from './use-billing-data';
15 changes: 15 additions & 0 deletions assets/js/base/hooks/order/use-billing-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* External dependencies
*/
import { usePaymentMethodDataContext } from '@woocommerce/base-context';

/**
* Exposes billing data api interface from the payment method data context.
*/
export const useBillingData = () => {
const { billingData, setBillingData } = usePaymentMethodDataContext();
return {
billingData,
setBillingData,
};
};
10 changes: 10 additions & 0 deletions assets/js/base/hooks/order/use-store-order.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @todo finish out this hook which will return a store draft order id and
// order loading (likely from payment data context).

export const useStoreOrder = () => {
const orderId = 0;
return {
orderId,
isLoading: false,
};
};
7 changes: 0 additions & 7 deletions assets/js/base/hooks/payment-methods/constants.js

This file was deleted.

3 changes: 1 addition & 2 deletions assets/js/base/hooks/payment-methods/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { default as useActivePaymentMethod } from './use-active-payment-method';
export { default as usePaymentEvents } from './use-payment-events';
export * from './use-payment-method-interface';
export * from './use-payment-methods';
Loading