Create New Item
Item Type
File
Folder
Item Name
Search file in folder and subfolders...
Are you sure want to rename?
optometrists
/
cache
/
cache
/
cache
/
cache
/
cache
/
cache
/
cache
/
.wp-cli
/
wp-content
/
plugins
/
woocommerce
/
packages
/
woocommerce-blocks
/
assets
/
js
/
base
/
context
/
hooks
/
shipping
:
use-select-shipping-rates.ts
Advanced Search
Upload
New Item
Settings
Back
Back Up
Advanced Editor
Save
/** * External dependencies */ 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'; /** * This is a custom hook for selecting shipping rates * * @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. */ export const useSelectShippingRates = (): { selectShippingRate: ( newShippingRateId: string, packageId: string | number ) => unknown; isSelectingRate: boolean; } => { const throwError = useThrowError(); const { selectShippingRate } = ( useDispatch( storeKey ) as { selectShippingRate: unknown; } ) as { selectShippingRate: ( newShippingRateId: string, packageId: string | number ) => Promise< unknown >; }; // Sets a rate for a package in state (so changes are shown right away to consumers of the hook) and in the stores. const setRate = useCallback( ( newShippingRateId, packageId ) => { selectShippingRate( newShippingRateId, packageId ).catch( ( error ) => { // we throw this error because an error on selecting a rate is problematic. throwError( error ); } ); }, [ throwError, selectShippingRate ] ); // See if rates are being selected. const isSelectingRate = useSelect< boolean >( ( select ) => { return select( storeKey ).isShippingRateBeingSelected(); }, [] ); return { selectShippingRate: setRate, isSelectingRate, }; };