2022-03-04 17:43:45 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { CART_STORE_KEY as storeKey } from '@woocommerce/block-data';
|
|
|
|
import { useSelect } from '@wordpress/data';
|
2022-12-23 11:59:02 +00:00
|
|
|
import { Cart, SelectShippingRateType, isObject } from '@woocommerce/types';
|
2022-03-04 17:43:45 +00:00
|
|
|
import { useEffect, useRef } from '@wordpress/element';
|
|
|
|
import { deriveSelectedShippingRates } from '@woocommerce/base-utils';
|
|
|
|
import isShallowEqual from '@wordpress/is-shallow-equal';
|
2022-07-27 06:00:17 +00:00
|
|
|
import { previewCart } from '@woocommerce/resource-previews';
|
2022-03-04 17:43:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { useSelectShippingRate } from './use-select-shipping-rate';
|
|
|
|
|
|
|
|
interface ShippingData extends SelectShippingRateType {
|
|
|
|
needsShipping: Cart[ 'needsShipping' ];
|
|
|
|
hasCalculatedShipping: Cart[ 'hasCalculatedShipping' ];
|
|
|
|
shippingRates: Cart[ 'shippingRates' ];
|
2022-03-14 11:29:25 +00:00
|
|
|
isLoadingRates: boolean;
|
2022-03-04 17:43:45 +00:00
|
|
|
selectedRates: Record< string, string | unknown >;
|
2022-10-18 16:31:06 +00:00
|
|
|
/**
|
|
|
|
* The following values are used to determine if pickup methods are shown separately from shipping methods, or if
|
|
|
|
* those options should be hidden.
|
|
|
|
*/
|
|
|
|
isCollectable: boolean; // Only true when ALL packages support local pickup. If true, we can show the collection/delivery toggle
|
2022-03-04 17:43:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const useShippingData = (): ShippingData => {
|
|
|
|
const {
|
|
|
|
shippingRates,
|
|
|
|
needsShipping,
|
|
|
|
hasCalculatedShipping,
|
2022-03-14 11:29:25 +00:00
|
|
|
isLoadingRates,
|
2022-10-18 16:31:06 +00:00
|
|
|
isCollectable,
|
2022-03-04 17:43:45 +00:00
|
|
|
} = useSelect( ( select ) => {
|
2022-07-27 06:00:17 +00:00
|
|
|
const isEditor = !! select( 'core/editor' );
|
2022-03-04 17:43:45 +00:00
|
|
|
const store = select( storeKey );
|
2022-10-18 16:31:06 +00:00
|
|
|
const rates = isEditor
|
|
|
|
? previewCart.shipping_rates
|
|
|
|
: store.getShippingRates();
|
2022-03-04 17:43:45 +00:00
|
|
|
return {
|
2022-10-18 16:31:06 +00:00
|
|
|
shippingRates: rates,
|
2022-07-27 06:00:17 +00:00
|
|
|
needsShipping: isEditor
|
|
|
|
? previewCart.needs_shipping
|
|
|
|
: store.getNeedsShipping(),
|
|
|
|
hasCalculatedShipping: isEditor
|
2022-07-28 09:22:42 +00:00
|
|
|
? previewCart.has_calculated_shipping
|
2022-07-27 06:00:17 +00:00
|
|
|
: store.getHasCalculatedShipping(),
|
|
|
|
isLoadingRates: isEditor ? false : store.isCustomerDataUpdating(),
|
2022-10-18 16:31:06 +00:00
|
|
|
isCollectable: rates.every(
|
|
|
|
( { shipping_rates: packageShippingRates } ) =>
|
|
|
|
packageShippingRates.find(
|
|
|
|
( { method_id: methodId } ) =>
|
|
|
|
methodId === 'pickup_location'
|
|
|
|
)
|
|
|
|
),
|
2022-03-04 17:43:45 +00:00
|
|
|
};
|
|
|
|
} );
|
|
|
|
const { isSelectingRate, selectShippingRate } = useSelectShippingRate();
|
|
|
|
|
|
|
|
// set selected rates on ref so it's always current.
|
|
|
|
const selectedRates = useRef< Record< string, unknown > >( {} );
|
|
|
|
useEffect( () => {
|
2022-06-15 09:56:52 +00:00
|
|
|
const derivedSelectedRates =
|
|
|
|
deriveSelectedShippingRates( shippingRates );
|
2022-03-04 17:43:45 +00:00
|
|
|
if (
|
|
|
|
isObject( derivedSelectedRates ) &&
|
|
|
|
! isShallowEqual( selectedRates.current, derivedSelectedRates )
|
|
|
|
) {
|
|
|
|
selectedRates.current = derivedSelectedRates;
|
|
|
|
}
|
|
|
|
}, [ shippingRates ] );
|
|
|
|
|
|
|
|
return {
|
|
|
|
isSelectingRate,
|
|
|
|
selectedRates: selectedRates.current,
|
|
|
|
selectShippingRate,
|
|
|
|
shippingRates,
|
|
|
|
needsShipping,
|
|
|
|
hasCalculatedShipping,
|
2022-03-14 11:29:25 +00:00
|
|
|
isLoadingRates,
|
2022-10-18 16:31:06 +00:00
|
|
|
isCollectable,
|
2022-03-04 17:43:45 +00:00
|
|
|
};
|
|
|
|
};
|