2020-03-11 10:50:12 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2022-06-10 16:33:15 +00:00
|
|
|
import { CHECKOUT_STORE_KEY } from '@woocommerce/block-data';
|
|
|
|
import { useSelect } from '@wordpress/data';
|
2020-04-30 09:52:36 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2021-01-19 15:55:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2022-06-10 16:33:15 +00:00
|
|
|
import { useCheckoutContext } from '../providers';
|
2021-04-08 12:31:12 +00:00
|
|
|
import { usePaymentMethodDataContext } from '../providers/cart-checkout/payment-methods';
|
|
|
|
import { usePaymentMethods } from './payment-methods/use-payment-methods';
|
2020-03-11 10:50:12 +00:00
|
|
|
|
|
|
|
/**
|
2020-04-30 09:52:36 +00:00
|
|
|
* Returns the submitButtonText, onSubmit interface from the checkout context,
|
|
|
|
* and an indication of submission status.
|
2020-03-11 10:50:12 +00:00
|
|
|
*/
|
|
|
|
export const useCheckoutSubmit = () => {
|
2020-04-30 09:52:36 +00:00
|
|
|
const {
|
|
|
|
isCalculating,
|
|
|
|
isBeforeProcessing,
|
|
|
|
isProcessing,
|
|
|
|
isAfterProcessing,
|
|
|
|
isComplete,
|
|
|
|
hasError,
|
2022-06-10 16:33:15 +00:00
|
|
|
} = useSelect( ( select ) => {
|
|
|
|
const store = select( CHECKOUT_STORE_KEY );
|
|
|
|
return {
|
|
|
|
isCalculating: store.isCalculating(),
|
|
|
|
isBeforeProcessing: store.isBeforeProcessing(),
|
|
|
|
isProcessing: store.isProcessing(),
|
|
|
|
isAfterProcessing: store.isAfterProcessing(),
|
|
|
|
isComplete: store.isComplete(),
|
|
|
|
hasError: store.hasError(),
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
|
|
|
const { onSubmit } = useCheckoutContext();
|
|
|
|
|
2021-01-19 15:55:44 +00:00
|
|
|
const { paymentMethods = {} } = usePaymentMethods();
|
2022-06-15 09:56:52 +00:00
|
|
|
const { activePaymentMethod, currentStatus: paymentStatus } =
|
|
|
|
usePaymentMethodDataContext();
|
2020-04-30 09:52:36 +00:00
|
|
|
const paymentMethod = paymentMethods[ activePaymentMethod ] || {};
|
2021-06-16 12:44:40 +00:00
|
|
|
const waitingForProcessing =
|
|
|
|
isProcessing || isAfterProcessing || isBeforeProcessing;
|
|
|
|
const waitingForRedirect = isComplete && ! hasError;
|
2020-04-30 09:52:36 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
submitButtonText:
|
|
|
|
paymentMethod?.placeOrderButtonLabel ||
|
|
|
|
__( 'Place Order', 'woo-gutenberg-products-block' ),
|
|
|
|
onSubmit,
|
|
|
|
isCalculating,
|
2021-06-16 12:44:40 +00:00
|
|
|
isDisabled: isProcessing || paymentStatus.isDoingExpressPayment,
|
|
|
|
waitingForProcessing,
|
|
|
|
waitingForRedirect,
|
2020-04-30 09:52:36 +00:00
|
|
|
};
|
2020-03-11 10:50:12 +00:00
|
|
|
};
|