2020-03-11 10:50:12 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2022-10-06 12:46:46 +00:00
|
|
|
import { CHECKOUT_STORE_KEY, PAYMENT_STORE_KEY } from '@woocommerce/block-data';
|
2022-06-10 16:33:15 +00:00
|
|
|
import { useSelect } from '@wordpress/data';
|
2020-04-30 09:52:36 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2022-09-29 08:46:10 +00:00
|
|
|
import { __experimentalApplyCheckoutFilter } from '@woocommerce/blocks-checkout';
|
2021-01-19 15:55:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2022-06-21 14:09:22 +00:00
|
|
|
import { useCheckoutEventsContext } from '../providers';
|
2021-04-08 12:31:12 +00:00
|
|
|
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(),
|
|
|
|
};
|
|
|
|
} );
|
2022-07-08 05:53:24 +00:00
|
|
|
const { currentStatus: paymentStatus, activePaymentMethod } = useSelect(
|
|
|
|
( select ) => {
|
2022-10-06 12:46:46 +00:00
|
|
|
const store = select( PAYMENT_STORE_KEY );
|
2022-07-08 05:53:24 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
currentStatus: store.getCurrentStatus(),
|
|
|
|
activePaymentMethod: store.getActivePaymentMethod(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
2022-06-10 16:33:15 +00:00
|
|
|
|
2022-06-21 14:09:22 +00:00
|
|
|
const { onSubmit } = useCheckoutEventsContext();
|
2022-06-10 16:33:15 +00:00
|
|
|
|
2021-01-19 15:55:44 +00:00
|
|
|
const { paymentMethods = {} } = usePaymentMethods();
|
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;
|
2022-09-29 08:46:10 +00:00
|
|
|
const defaultLabel =
|
|
|
|
paymentMethod.placeOrderButtonLabel ||
|
|
|
|
__( 'Place Order', 'woo-gutenberg-products-block' );
|
|
|
|
const label = __experimentalApplyCheckoutFilter( {
|
|
|
|
filterName: 'placeOrderButtonLabel',
|
|
|
|
defaultValue: defaultLabel,
|
|
|
|
} );
|
2020-04-30 09:52:36 +00:00
|
|
|
|
|
|
|
return {
|
2022-09-29 08:46:10 +00:00
|
|
|
submitButtonText: label,
|
2020-04-30 09:52:36 +00:00
|
|
|
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
|
|
|
};
|