2020-03-05 19:54:05 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-03-31 15:40:27 +00:00
|
|
|
import { useDispatch, useSelect } from '@wordpress/data';
|
2020-03-26 12:40:56 +00:00
|
|
|
import { useState, useEffect, useMemo } from '@wordpress/element';
|
2020-03-05 19:54:05 +00:00
|
|
|
import { CART_STORE_KEY as storeKey } from '@woocommerce/block-data';
|
2020-03-13 20:05:45 +00:00
|
|
|
import { useThrowError } from '@woocommerce/base-hooks';
|
2020-03-05 19:54:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a custom hook for loading the selected shipping rate from the cart store and actions for selecting a rate.
|
2020-09-26 17:28:16 +00:00
|
|
|
See also: https://github.com/woocommerce/woocommerce-gutenberg-products-block/tree/trunk/src/RestApi/StoreApi
|
2020-03-05 19:54:05 +00:00
|
|
|
*
|
|
|
|
* @param {Array} shippingRates an array of packages with shipping rates.
|
|
|
|
* @return {Object} This hook will return an object with two properties:
|
|
|
|
* - selectShippingRate A function that immediately returns the selected
|
|
|
|
* rate and dispatches an action generator.
|
2020-03-12 09:59:43 +00:00
|
|
|
* - selectedShippingRates An object of selected shipping rates and package id as key, maintained
|
2020-08-31 16:15:56 +00:00
|
|
|
* locally by a state and updated optimistically, this will only return packages that has selected
|
|
|
|
* shipping rates.
|
|
|
|
*
|
2020-03-05 19:54:05 +00:00
|
|
|
*/
|
|
|
|
export const useSelectShippingRate = ( shippingRates ) => {
|
2020-03-13 20:05:45 +00:00
|
|
|
const throwError = useThrowError();
|
2020-03-26 12:40:56 +00:00
|
|
|
const derivedSelectedRates = useMemo(
|
|
|
|
() =>
|
|
|
|
shippingRates
|
|
|
|
.map(
|
|
|
|
// the API responds with those keys.
|
|
|
|
/* eslint-disable camelcase */
|
|
|
|
( p ) => [
|
|
|
|
p.package_id,
|
|
|
|
p.shipping_rates.find( ( rate ) => rate.selected )
|
|
|
|
?.rate_id,
|
|
|
|
]
|
|
|
|
/* eslint-enable */
|
|
|
|
)
|
|
|
|
// A fromEntries ponyfill, creates an object from an array of arrays.
|
|
|
|
.reduce( ( obj, [ key, val ] ) => {
|
2020-08-31 16:15:56 +00:00
|
|
|
if ( val ) {
|
|
|
|
obj[ key ] = val;
|
|
|
|
}
|
2020-03-26 12:40:56 +00:00
|
|
|
return obj;
|
|
|
|
}, {} ),
|
|
|
|
[ shippingRates ]
|
|
|
|
);
|
2020-03-12 09:59:43 +00:00
|
|
|
|
2020-03-05 19:54:05 +00:00
|
|
|
const [ selectedShippingRates, setSelectedShipping ] = useState(
|
2020-03-26 12:40:56 +00:00
|
|
|
derivedSelectedRates
|
2020-03-05 19:54:05 +00:00
|
|
|
);
|
2020-03-26 12:40:56 +00:00
|
|
|
|
|
|
|
// useState initial state is always remembered, so even if that value changes,
|
|
|
|
// useState won't update, we're forcing it to update if the initial value changes,
|
|
|
|
// useMemo helps us not running this function when we don't need to.
|
|
|
|
useEffect( () => {
|
|
|
|
setSelectedShipping( derivedSelectedRates );
|
|
|
|
}, [ derivedSelectedRates ] );
|
2020-03-05 19:54:05 +00:00
|
|
|
const { selectShippingRate } = useDispatch( storeKey );
|
2020-03-31 15:40:27 +00:00
|
|
|
const isSelectingRate = useSelect( ( select ) => {
|
|
|
|
return select( storeKey ).isShippingRateBeingSelected();
|
|
|
|
}, [] );
|
2020-03-05 19:54:05 +00:00
|
|
|
const setRate = ( newShippingRate, packageId ) => {
|
2020-03-12 09:59:43 +00:00
|
|
|
setSelectedShipping( {
|
|
|
|
...selectedShippingRates,
|
|
|
|
[ packageId ]: newShippingRate,
|
|
|
|
} );
|
2020-03-13 20:05:45 +00:00
|
|
|
selectShippingRate( newShippingRate, packageId ).catch( ( error ) => {
|
|
|
|
// we throw this error because an error on selecting a rate
|
|
|
|
// is problematic.
|
|
|
|
throwError( error );
|
|
|
|
} );
|
2020-03-05 19:54:05 +00:00
|
|
|
};
|
|
|
|
return {
|
|
|
|
selectShippingRate: setRate,
|
|
|
|
selectedShippingRates,
|
2020-03-31 15:40:27 +00:00
|
|
|
isSelectingRate,
|
2020-03-05 19:54:05 +00:00
|
|
|
};
|
|
|
|
};
|