2020-03-11 10:50:12 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-04-30 09:52:36 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2021-01-19 15:55:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-04-08 12:31:12 +00:00
|
|
|
import { useCheckoutContext } from '../providers/cart-checkout/checkout-state';
|
|
|
|
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 {
|
|
|
|
onSubmit,
|
|
|
|
isCalculating,
|
|
|
|
isBeforeProcessing,
|
|
|
|
isProcessing,
|
|
|
|
isAfterProcessing,
|
|
|
|
isComplete,
|
|
|
|
hasError,
|
|
|
|
} = useCheckoutContext();
|
2021-01-19 15:55:44 +00:00
|
|
|
const { paymentMethods = {} } = usePaymentMethods();
|
2021-06-16 12:44:40 +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
|
|
|
};
|