2020-03-05 19:54:05 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2022-02-10 11:59:43 +00:00
|
|
|
import { useDispatch, useSelect } from '@wordpress/data';
|
|
|
|
import { useCallback } from '@wordpress/element';
|
|
|
|
import { CART_STORE_KEY as storeKey } from '@woocommerce/block-data';
|
|
|
|
import { useThrowError } from '@woocommerce/base-hooks';
|
2022-03-04 17:43:45 +00:00
|
|
|
import { SelectShippingRateType } from '@woocommerce/type-defs/shipping';
|
2021-01-19 15:55:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-03-31 14:22:27 +00:00
|
|
|
import { useStoreEvents } from '../use-store-events';
|
2020-03-05 19:54:05 +00:00
|
|
|
|
|
|
|
/**
|
2022-02-10 11:59:43 +00:00
|
|
|
* This is a custom hook for selecting shipping rates for a shipping package.
|
2020-03-05 19:54:05 +00:00
|
|
|
*
|
2021-01-28 14:24:01 +00:00
|
|
|
* @return {Object} This hook will return an object with these properties:
|
|
|
|
* - selectShippingRate: A function that immediately returns the selected rate and dispatches an action generator.
|
|
|
|
* - isSelectingRate: True when rates are being resolved to the API.
|
2020-03-05 19:54:05 +00:00
|
|
|
*/
|
2022-03-04 17:43:45 +00:00
|
|
|
export const useSelectShippingRate = (): SelectShippingRateType => {
|
2022-02-10 11:59:43 +00:00
|
|
|
const throwError = useThrowError();
|
2021-03-31 14:22:27 +00:00
|
|
|
const { dispatchCheckoutEvent } = useStoreEvents();
|
|
|
|
|
2022-02-10 11:59:43 +00:00
|
|
|
const { selectShippingRate: dispatchSelectShippingRate } = ( useDispatch(
|
|
|
|
storeKey
|
|
|
|
) as {
|
|
|
|
selectShippingRate: unknown;
|
|
|
|
} ) as {
|
|
|
|
selectShippingRate: (
|
|
|
|
newShippingRateId: string,
|
|
|
|
packageId: string | number
|
|
|
|
) => Promise< unknown >;
|
|
|
|
};
|
2021-01-28 14:24:01 +00:00
|
|
|
|
2022-02-10 11:59:43 +00:00
|
|
|
// Selects a shipping rate, fires an event, and catch any errors.
|
|
|
|
const selectShippingRate = useCallback(
|
|
|
|
( newShippingRateId, packageId ) => {
|
|
|
|
dispatchSelectShippingRate( newShippingRateId, packageId )
|
|
|
|
.then( () => {
|
|
|
|
dispatchCheckoutEvent( 'set-selected-shipping-rate', {
|
|
|
|
shippingRateId: newShippingRateId,
|
|
|
|
} );
|
|
|
|
} )
|
|
|
|
.catch( ( error ) => {
|
|
|
|
// Throw an error because an error when selecting a rate is problematic.
|
|
|
|
throwError( error );
|
|
|
|
} );
|
2021-01-28 14:24:01 +00:00
|
|
|
},
|
2022-02-10 11:59:43 +00:00
|
|
|
[ dispatchSelectShippingRate, dispatchCheckoutEvent, throwError ]
|
2021-01-28 14:24:01 +00:00
|
|
|
);
|
|
|
|
|
2022-02-10 11:59:43 +00:00
|
|
|
// See if rates are being selected.
|
|
|
|
const isSelectingRate = useSelect< boolean >( ( select ) => {
|
|
|
|
return select( storeKey ).isShippingRateBeingSelected();
|
|
|
|
}, [] );
|
|
|
|
|
2020-03-05 19:54:05 +00:00
|
|
|
return {
|
2022-02-10 11:59:43 +00:00
|
|
|
selectShippingRate,
|
2020-03-31 15:40:27 +00:00
|
|
|
isSelectingRate,
|
2020-03-05 19:54:05 +00:00
|
|
|
};
|
|
|
|
};
|