2020-03-05 19:54:05 +00:00
/ * *
* External dependencies
* /
2021-01-28 14:24:01 +00:00
import { useState , useEffect , useRef , useCallback } from '@wordpress/element' ;
import isShallowEqual from '@wordpress/is-shallow-equal' ;
2021-01-19 15:55:44 +00:00
/ * *
* Internal dependencies
* /
2021-01-28 14:24:01 +00:00
import { useSelectShippingRates } from './use-select-shipping-rates' ;
2020-03-05 19:54:05 +00:00
/ * *
2021-01-28 14:24:01 +00:00
* Selected rates are derived by looping over the shipping rates .
2020-03-05 19:54:05 +00:00
*
2021-01-28 14:24:01 +00:00
* @ param { Array } shippingRates Array of shipping rates .
* @ return { string } Selected rate id .
* /
// This will find the selected rate ID in an array of shipping rates.
const deriveSelectedRateId = ( shippingRates ) =>
shippingRates . find ( ( rate ) => rate . selected ) ? . rate _id ;
/ * *
* This is a custom hook for tracking selected shipping rates for a package and selecting a rate . State is used so
* changes are reflected in the UI instantly .
2020-08-31 16:15:56 +00:00
*
2021-01-28 14:24:01 +00:00
* @ param { string } packageId Package ID to select rates for .
* @ param { Array } shippingRates an array of packages with 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 .
* - selectedShippingRate : The selected rate id .
* - isSelectingRate : True when rates are being resolved to the API .
2020-03-05 19:54:05 +00:00
* /
2021-01-28 14:24:01 +00:00
export const useSelectShippingRate = ( packageId , shippingRates ) => {
// Rates are selected via the shipping data context provider.
const { selectShippingRate , isSelectingRate } = useSelectShippingRates ( ) ;
2020-03-12 09:59:43 +00:00
2021-01-28 14:24:01 +00:00
// Selected rates are stored in state. This allows shipping rates changes to be shown in the UI instantly.
// Defaults to the currently selected rate_id.
const [ selectedShippingRate , setSelectedShippingRate ] = useState ( ( ) =>
deriveSelectedRateId ( shippingRates )
2020-03-05 19:54:05 +00:00
) ;
2020-03-26 12:40:56 +00:00
2021-01-28 14:24:01 +00:00
// This ref is used to track when changes come in via the props. When the incoming shipping rates change, update our local state if there are changes to selected methods.
const currentShippingRates = useRef ( shippingRates ) ;
2020-03-26 12:40:56 +00:00
useEffect ( ( ) => {
2021-01-28 14:24:01 +00:00
if ( ! isShallowEqual ( currentShippingRates . current , shippingRates ) ) {
currentShippingRates . current = shippingRates ;
setSelectedShippingRate ( deriveSelectedRateId ( shippingRates ) ) ;
}
} , [ shippingRates ] ) ;
// Sets a rate for a package in state (so changes are shown right away to consumers of the hook) and in the stores.
const setPackageRateId = useCallback (
( newShippingRate ) => {
setSelectedShippingRate ( newShippingRate ) ;
selectShippingRate ( newShippingRate , packageId ) ;
} ,
[ packageId , selectShippingRate ]
) ;
2020-03-05 19:54:05 +00:00
return {
2021-01-28 14:24:01 +00:00
selectShippingRate : setPackageRateId ,
selectedShippingRate ,
2020-03-31 15:40:27 +00:00
isSelectingRate ,
2020-03-05 19:54:05 +00:00
} ;
} ;