Change `shippingRatesLoading` to `isLoadingRates` (https://github.com/woocommerce/woocommerce-blocks/pull/5999)

* Change `shippingRatesLoading` to `isLoadingRates`

* Revert isLoadingRates back to old name in payment method interface

* Update docs with correct names returned by usePaymentMethodInterface

* Update TS to name the selectShippingRate method correctly

* Change method name on usePaymentMethodInterface back
This commit is contained in:
Thomas Roberts 2022-03-14 11:29:25 +00:00 committed by GitHub
parent 5a8e94d86e
commit a6bff7e6e8
13 changed files with 42 additions and 50 deletions

View File

@ -78,7 +78,7 @@ interface ShippingRatesControlProps {
collapsible?: boolean; collapsible?: boolean;
shippingRates: CartResponseShippingRate[]; shippingRates: CartResponseShippingRate[];
className?: string; className?: string;
shippingRatesLoading: boolean; isLoadingRates: boolean;
noResultsMessage: ReactElement; noResultsMessage: ReactElement;
renderOption: PackageRateRenderOption; renderOption: PackageRateRenderOption;
context: 'woocommerce/cart' | 'woocommerce/checkout'; context: 'woocommerce/cart' | 'woocommerce/checkout';
@ -88,7 +88,7 @@ interface ShippingRatesControlProps {
* *
* @param {Object} props Incoming props. * @param {Object} props Incoming props.
* @param {Array} props.shippingRates Array of packages containing shipping rates. * @param {Array} props.shippingRates Array of packages containing shipping rates.
* @param {boolean} props.shippingRatesLoading True when rates are being loaded. * @param {boolean} props.isLoadingRates True when rates are being loaded.
* @param {string} props.className Class name for package rates. * @param {string} props.className Class name for package rates.
* @param {boolean} [props.collapsible] If true, when multiple packages are rendered they can be toggled open and closed. * @param {boolean} [props.collapsible] If true, when multiple packages are rendered they can be toggled open and closed.
* @param {ReactElement} props.noResultsMessage Rendered when there are no packages. * @param {ReactElement} props.noResultsMessage Rendered when there are no packages.
@ -97,7 +97,7 @@ interface ShippingRatesControlProps {
*/ */
const ShippingRatesControl = ( { const ShippingRatesControl = ( {
shippingRates, shippingRates,
shippingRatesLoading, isLoadingRates,
className, className,
collapsible = false, collapsible = false,
noResultsMessage, noResultsMessage,
@ -105,7 +105,7 @@ const ShippingRatesControl = ( {
context, context,
}: ShippingRatesControlProps ): JSX.Element => { }: ShippingRatesControlProps ): JSX.Element => {
useEffect( () => { useEffect( () => {
if ( shippingRatesLoading ) { if ( isLoadingRates ) {
return; return;
} }
const packageCount = getShippingRatesPackageCount( shippingRates ); const packageCount = getShippingRatesPackageCount( shippingRates );
@ -148,7 +148,7 @@ const ShippingRatesControl = ( {
) )
); );
} }
}, [ shippingRatesLoading, shippingRates ] ); }, [ isLoadingRates, shippingRates ] );
// Prepare props to pass to the ExperimentalOrderShippingPackages slot fill. // Prepare props to pass to the ExperimentalOrderShippingPackages slot fill.
// We need to pluck out receiveCart. // We need to pluck out receiveCart.
@ -170,7 +170,7 @@ const ShippingRatesControl = ( {
return ( return (
<LoadingMask <LoadingMask
isLoading={ shippingRatesLoading } isLoading={ isLoadingRates }
screenReaderLabel={ __( screenReaderLabel={ __(
'Loading shipping rates…', 'Loading shipping rates…',
'woo-gutenberg-products-block' 'woo-gutenberg-products-block'

View File

@ -138,7 +138,7 @@ export const TotalsShipping = ( {
shippingAddress, shippingAddress,
cartHasCalculatedShipping, cartHasCalculatedShipping,
shippingRates, shippingRates,
shippingRatesLoading, isLoadingRates,
} = useStoreCart(); } = useStoreCart();
const totalShippingValue = getSetting( const totalShippingValue = getSetting(
@ -209,7 +209,7 @@ export const TotalsShipping = ( {
<ShippingRateSelector <ShippingRateSelector
hasRates={ hasRates } hasRates={ hasRates }
shippingRates={ shippingRates } shippingRates={ shippingRates }
shippingRatesLoading={ shippingRatesLoading } isLoadingRates={ isLoadingRates }
/> />
) } ) }
</div> </div>

View File

@ -13,7 +13,7 @@ import ShippingRatesControl from '../../shipping-rates-control';
const ShippingRateSelector = ( { const ShippingRateSelector = ( {
hasRates, hasRates,
shippingRates, shippingRates,
shippingRatesLoading, isLoadingRates,
} ) => { } ) => {
const legend = hasRates const legend = hasRates
? __( 'Shipping options', 'woo-gutenberg-products-block' ) ? __( 'Shipping options', 'woo-gutenberg-products-block' )
@ -39,7 +39,7 @@ const ShippingRateSelector = ( {
</Notice> </Notice>
} }
shippingRates={ shippingRates } shippingRates={ shippingRates }
shippingRatesLoading={ shippingRatesLoading } isLoadingRates={ isLoadingRates }
context="woocommerce/cart" context="woocommerce/cart"
/> />
</fieldset> </fieldset>

View File

@ -66,7 +66,7 @@ describe( 'useStoreCart', () => {
}, },
shippingRates: previewCart.shipping_rates, shippingRates: previewCart.shipping_rates,
extensions: {}, extensions: {},
shippingRatesLoading: false, isLoadingRates: false,
cartHasCalculatedShipping: true, cartHasCalculatedShipping: true,
}; };
@ -112,7 +112,7 @@ describe( 'useStoreCart', () => {
shippingAddress: mockShippingAddress, shippingAddress: mockShippingAddress,
shippingRates: [], shippingRates: [],
extensions: {}, extensions: {},
shippingRatesLoading: false, isLoadingRates: false,
cartHasCalculatedShipping: true, cartHasCalculatedShipping: true,
receiveCart: undefined, receiveCart: undefined,
paymentRequirements: [], paymentRequirements: [],

View File

@ -108,7 +108,7 @@ export const defaultCartData: StoreCart = {
billingAddress: defaultBillingAddress, billingAddress: defaultBillingAddress,
shippingAddress: defaultShippingAddress, shippingAddress: defaultShippingAddress,
shippingRates: EMPTY_SHIPPING_RATES, shippingRates: EMPTY_SHIPPING_RATES,
shippingRatesLoading: false, isLoadingRates: false,
cartHasCalculatedShipping: false, cartHasCalculatedShipping: false,
paymentRequirements: EMPTY_PAYMENT_REQUIREMENTS, paymentRequirements: EMPTY_PAYMENT_REQUIREMENTS,
receiveCart: () => undefined, receiveCart: () => undefined,
@ -163,7 +163,7 @@ export const useStoreCart = (
shippingAddress: defaultShippingAddress, shippingAddress: defaultShippingAddress,
extensions: EMPTY_EXTENSIONS, extensions: EMPTY_EXTENSIONS,
shippingRates: previewCart.shipping_rates, shippingRates: previewCart.shipping_rates,
shippingRatesLoading: false, isLoadingRates: false,
cartHasCalculatedShipping: cartHasCalculatedShipping:
previewCart.has_calculated_shipping, previewCart.has_calculated_shipping,
paymentRequirements: previewCart.paymentRequirements, paymentRequirements: previewCart.paymentRequirements,
@ -182,7 +182,7 @@ export const useStoreCart = (
'getCartData' 'getCartData'
); );
const shippingRatesLoading = store.isCustomerDataUpdating(); const isLoadingRates = store.isCustomerDataUpdating();
const { receiveCart } = dispatch( storeKey ); const { receiveCart } = dispatch( storeKey );
const billingAddress = decodeValues( cartData.billingAddress ); const billingAddress = decodeValues( cartData.billingAddress );
const shippingAddress = cartData.needsShipping const shippingAddress = cartData.needsShipping
@ -224,7 +224,7 @@ export const useStoreCart = (
shippingAddress: emptyHiddenAddressFields( shippingAddress ), shippingAddress: emptyHiddenAddressFields( shippingAddress ),
extensions: cartData.extensions, extensions: cartData.extensions,
shippingRates: cartData.shippingRates, shippingRates: cartData.shippingRates,
shippingRatesLoading, isLoadingRates,
cartHasCalculatedShipping: cartData.hasCalculatedShipping, cartHasCalculatedShipping: cartData.hasCalculatedShipping,
paymentRequirements: cartData.paymentRequirements, paymentRequirements: cartData.paymentRequirements,
receiveCart, receiveCart,

View File

@ -58,7 +58,7 @@ export const usePaymentMethodInterface = (): PaymentMethodInterface => {
} = useShippingDataContext(); } = useShippingDataContext();
const { const {
shippingRates, shippingRates,
shippingRatesLoading, isLoadingRates,
selectedRates, selectedRates,
isSelectingRate, isSelectingRate,
selectShippingRate, selectShippingRate,
@ -160,11 +160,11 @@ export const usePaymentMethodInterface = (): PaymentMethodInterface => {
isSelectingRate, isSelectingRate,
needsShipping, needsShipping,
selectedRates, selectedRates,
selectShippingRate, setSelectedRates: selectShippingRate,
setShippingAddress, setShippingAddress,
shippingAddress, shippingAddress,
shippingRates, shippingRates,
shippingRatesLoading, shippingRatesLoading: isLoadingRates,
}, },
shippingStatus: { shippingStatus: {
shippingErrorStatus, shippingErrorStatus,

View File

@ -19,7 +19,7 @@ interface ShippingData extends SelectShippingRateType {
needsShipping: Cart[ 'needsShipping' ]; needsShipping: Cart[ 'needsShipping' ];
hasCalculatedShipping: Cart[ 'hasCalculatedShipping' ]; hasCalculatedShipping: Cart[ 'hasCalculatedShipping' ];
shippingRates: Cart[ 'shippingRates' ]; shippingRates: Cart[ 'shippingRates' ];
shippingRatesLoading: boolean; isLoadingRates: boolean;
selectedRates: Record< string, string | unknown >; selectedRates: Record< string, string | unknown >;
} }
@ -28,14 +28,14 @@ export const useShippingData = (): ShippingData => {
shippingRates, shippingRates,
needsShipping, needsShipping,
hasCalculatedShipping, hasCalculatedShipping,
shippingRatesLoading, isLoadingRates,
} = useSelect( ( select ) => { } = useSelect( ( select ) => {
const store = select( storeKey ); const store = select( storeKey );
return { return {
shippingRates: store.getShippingRates(), shippingRates: store.getShippingRates(),
needsShipping: store.getNeedsShipping(), needsShipping: store.getNeedsShipping(),
hasCalculatedShipping: store.getHasCalculatedShipping(), hasCalculatedShipping: store.getHasCalculatedShipping(),
shippingRatesLoading: store.isCustomerDataUpdating(), isLoadingRates: store.isCustomerDataUpdating(),
}; };
} ); } );
const { isSelectingRate, selectShippingRate } = useSelectShippingRate(); const { isSelectingRate, selectShippingRate } = useSelectShippingRate();
@ -61,6 +61,6 @@ export const useShippingData = (): ShippingData => {
shippingRates, shippingRates,
needsShipping, needsShipping,
hasCalculatedShipping, hasCalculatedShipping,
shippingRatesLoading, isLoadingRates,
}; };
}; };

View File

@ -47,7 +47,7 @@ export const DEFAULT_SHIPPING_CONTEXT_DATA = {
dispatchErrorStatus: () => null, dispatchErrorStatus: () => null,
shippingErrorTypes: ERROR_TYPES, shippingErrorTypes: ERROR_TYPES,
shippingRates: [], shippingRates: [],
shippingRatesLoading: false, isLoadingRates: false,
selectedRates: [], selectedRates: [],
setSelectedRates: () => null, setSelectedRates: () => null,
shippingAddress: DEFAULT_SHIPPING_ADDRESS, shippingAddress: DEFAULT_SHIPPING_ADDRESS,

View File

@ -50,7 +50,7 @@ export const useShippingDataContext = () => {
*/ */
export const ShippingDataProvider = ( { children } ) => { export const ShippingDataProvider = ( { children } ) => {
const { dispatchActions } = useCheckoutContext(); const { dispatchActions } = useCheckoutContext();
const { shippingRates, shippingRatesLoading, cartErrors } = useStoreCart(); const { shippingRates, isLoadingRates, cartErrors } = useStoreCart();
const { isSelectingRate } = useSelectShippingRate(); const { isSelectingRate } = useSelectShippingRate();
const { selectedRates } = useShippingData(); const { selectedRates } = useShippingData();
const [ shippingErrorStatus, dispatchErrorStatus ] = useReducer( const [ shippingErrorStatus, dispatchErrorStatus ] = useReducer(
@ -79,12 +79,12 @@ export const ShippingDataProvider = ( { children } ) => {
// increment/decrement checkout calculating counts when shipping is loading. // increment/decrement checkout calculating counts when shipping is loading.
useEffect( () => { useEffect( () => {
if ( shippingRatesLoading ) { if ( isLoadingRates ) {
dispatchActions.incrementCalculating(); dispatchActions.incrementCalculating();
} else { } else {
dispatchActions.decrementCalculating(); dispatchActions.decrementCalculating();
} }
}, [ shippingRatesLoading, dispatchActions ] ); }, [ isLoadingRates, dispatchActions ] );
// increment/decrement checkout calculating counts when shipping rates are being selected. // increment/decrement checkout calculating counts when shipping rates are being selected.
useEffect( () => { useEffect( () => {
@ -122,7 +122,7 @@ export const ShippingDataProvider = ( { children } ) => {
// emit events. // emit events.
useEffect( () => { useEffect( () => {
if ( if (
! shippingRatesLoading && ! isLoadingRates &&
( shippingRates.length === 0 || currentErrorStatus.hasError ) ( shippingRates.length === 0 || currentErrorStatus.hasError )
) { ) {
emitEvent( emitEvent(
@ -136,14 +136,14 @@ export const ShippingDataProvider = ( { children } ) => {
} }
}, [ }, [
shippingRates, shippingRates,
shippingRatesLoading, isLoadingRates,
currentErrorStatus.hasError, currentErrorStatus.hasError,
currentErrorStatus.hasInvalidAddress, currentErrorStatus.hasInvalidAddress,
] ); ] );
useEffect( () => { useEffect( () => {
if ( if (
! shippingRatesLoading && ! isLoadingRates &&
shippingRates.length > 0 && shippingRates.length > 0 &&
! currentErrorStatus.hasError ! currentErrorStatus.hasError
) { ) {
@ -153,7 +153,7 @@ export const ShippingDataProvider = ( { children } ) => {
shippingRates shippingRates
); );
} }
}, [ shippingRates, shippingRatesLoading, currentErrorStatus.hasError ] ); }, [ shippingRates, isLoadingRates, currentErrorStatus.hasError ] );
// emit shipping rate selection events. // emit shipping rate selection events.
useEffect( () => { useEffect( () => {

View File

@ -52,7 +52,7 @@ const Block = (): JSX.Element | null => {
const { const {
shippingRates, shippingRates,
needsShipping, needsShipping,
shippingRatesLoading, isLoadingRates,
hasCalculatedShipping, hasCalculatedShipping,
} = useShippingData(); } = useShippingData();
@ -101,7 +101,7 @@ const Block = (): JSX.Element | null => {
} }
renderOption={ renderShippingRatesControlOption } renderOption={ renderShippingRatesControlOption }
shippingRates={ shippingRates } shippingRates={ shippingRates }
shippingRatesLoading={ shippingRatesLoading } isLoadingRates={ isLoadingRates }
context="woocommerce/checkout" context="woocommerce/checkout"
/> />
) } ) }

View File

@ -47,7 +47,7 @@ export interface StoreCart {
shippingAddress: CartResponseShippingAddress; shippingAddress: CartResponseShippingAddress;
shippingRates: Array< CartResponseShippingRate >; shippingRates: Array< CartResponseShippingRate >;
extensions: Record< string, unknown >; extensions: Record< string, unknown >;
shippingRatesLoading: boolean; isLoadingRates: boolean;
cartHasCalculatedShipping: boolean; cartHasCalculatedShipping: boolean;
paymentRequirements: Array< string >; paymentRequirements: Array< string >;
receiveCart: ( cart: CartResponse ) => void; receiveCart: ( cart: CartResponse ) => void;

View File

@ -47,21 +47,13 @@ The customer data context exposes the api interfaces for the following things vi
The shipping method data context exposes the api interfaces for the following things (typedef `ShippingMethodDataContext`) via the `useShippingMethodData` hook: The shipping method data context exposes the api interfaces for the following things (typedef `ShippingMethodDataContext`) via the `useShippingMethodData` hook:
- `shippingErrorStatus`: The current error status for the context. - `shippingErrorStatus`: The current error status for the context.
- `dispatchErrorStatus`: A function for dispatching a shipping error status. Used in combination with... - `dispatchErrorStatus`: A function for dispatching a shipping error status. Used in combination with...
- `shippingErrorTypes`: An object with the various error statuses that can be dispatched (`NONE`, `INVALID_ADDRESS`, `UNKNOWN`) - `shippingErrorTypes`: An object with the various error statuses that can be dispatched (`NONE`, `INVALID_ADDRESS`, `UNKNOWN`)
- `shippingRates`: This is retrieved using the `useShippingRates` hook and exposed on the context. - `onShippingRateSuccess`: This is a function for registering a callback to be invoked when shipping rates are retrieved successfully. Callbacks will receive the new rates as an argument.
- `shippingRatesLoading`: True when shipping rates are loading. - `onShippingRateFail`: This is a function for registering a callback to be invoked when shipping rates fail to be retrieved. Callbacks will receive the error status as an argument.
- `selectedRates`: Will expose the selected rates. - `onShippingRateSelectSuccess`: This is a function for registering a callback to be invoked when shipping rate selection is successful.
- `setSelectedRates`: Function for setting new selected rates. - `onShippingRateSelectFail`: This is a function for registering a callback to be invoked when shipping rates selection is unsuccessful.
- `isSelectingRate`: True when shipping rate selection is persisting to the server.
- `shippingAddress`: The current set shipping address.
- `setShippingAddress`: A function for setting the shipping address. This will trigger shipping rates updates.
- `onShippingRateSuccess`: This is a function for registering a callback to be invoked when shipping rates are retrieved successfully. Callbacks will receive the new rates as an argument.
- `onShippingRateFail`: This is a function for registering a callback to be invoked when shipping rates fail to be retrieved. Callbacks will receive the error status as an argument.
- `onShippingRateSelectSuccess`: This is a function for registering a callback to be invoked when shipping rate selection is successful.
- `onShippingRateSelectFail`: This is a function for registering a callback to be invoked when shipping rates selection is unsuccessful.
- `needsShipping`: Boolean indicating whether the contents of the order needs shipping (`true`) or not (`false`).
#### Payment Method Data Context #### Payment Method Data Context

View File

@ -22,7 +22,7 @@ export const useStoreCart = () => ( {
billingAddress: {}, billingAddress: {},
shippingAddress: {}, shippingAddress: {},
shippingRates: previewShippingRates, shippingRates: previewShippingRates,
shippingRatesLoading: false, isLoadingRates: false,
cartHasCalculatedShipping: previewCart.has_calculated_shipping, cartHasCalculatedShipping: previewCart.has_calculated_shipping,
receiveCart: () => void null, receiveCart: () => void null,
} ); } );