Handle cart api errors (https://github.com/woocommerce/woocommerce-blocks/pull/1907)
This commit is contained in:
parent
4b4dff1297
commit
d0cb38d37d
|
@ -6,6 +6,7 @@ import { useReducer, useEffect } from '@wordpress/element';
|
|||
import isShallowEqual from '@wordpress/is-shallow-equal';
|
||||
import { useDebounce } from 'use-debounce';
|
||||
import { CART_STORE_KEY as storeKey } from '@woocommerce/block-data';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
|
@ -26,7 +27,7 @@ import { pluckAddress } from '../utils';
|
|||
* - {Object} shippingAddress An object containing shipping address.
|
||||
*/
|
||||
export const useShippingRates = ( addressFieldsKeys ) => {
|
||||
const { shippingRates } = useStoreCart();
|
||||
const { cartErrors, shippingRates } = useStoreCart();
|
||||
const addressFields = Object.fromEntries(
|
||||
addressFieldsKeys.map( ( key ) => [ key, '' ] )
|
||||
);
|
||||
|
@ -63,5 +64,6 @@ export const useShippingRates = ( addressFieldsKeys ) => {
|
|||
shippingAddress,
|
||||
setShippingAddress,
|
||||
shippingRatesLoading,
|
||||
shippingRatesErrors: cartErrors,
|
||||
};
|
||||
};
|
||||
|
|
|
@ -22,7 +22,7 @@ import { useStoreCart } from './use-store-cart';
|
|||
* store api /cart/coupons endpoint.
|
||||
*/
|
||||
export const useStoreCartCoupons = () => {
|
||||
const { cartCoupons, cartIsLoading } = useStoreCart();
|
||||
const { cartCoupons, cartErrors, cartIsLoading } = useStoreCart();
|
||||
const {
|
||||
addErrorNotice,
|
||||
addSuccessNotice,
|
||||
|
@ -100,6 +100,7 @@ export const useStoreCartCoupons = () => {
|
|||
|
||||
return {
|
||||
appliedCoupons: cartCoupons,
|
||||
cartCouponsErrors: cartErrors,
|
||||
isLoading: cartIsLoading,
|
||||
...results,
|
||||
};
|
||||
|
|
|
@ -6,6 +6,11 @@ import { useState, useEffect } from '@wordpress/element';
|
|||
import { CART_STORE_KEY as storeKey } from '@woocommerce/block-data';
|
||||
import { useDebounce } from 'use-debounce';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import { useStoreCart } from './use-store-cart';
|
||||
|
||||
/**
|
||||
* @typedef {import('@woocommerce/type-defs/hooks').StoreCartItemQuantity} StoreCartItemQuantity
|
||||
* @typedef {import('@woocommerce/type-defs/cart').CartItem} CartItem
|
||||
|
@ -23,6 +28,7 @@ import { useDebounce } from 'use-debounce';
|
|||
* to cart items.
|
||||
*/
|
||||
export const useStoreCartItemQuantity = ( cartItem ) => {
|
||||
const { cartErrors } = useStoreCart();
|
||||
// Store quantity in hook state. This is used to keep the UI
|
||||
// updated while server request is updated.
|
||||
const [ quantity, changeQuantity ] = useState( cartItem.quantity );
|
||||
|
@ -53,5 +59,6 @@ export const useStoreCartItemQuantity = ( cartItem ) => {
|
|||
quantity,
|
||||
changeQuantity,
|
||||
removeItem,
|
||||
cartItemQuantityErrors: cartErrors,
|
||||
};
|
||||
};
|
||||
|
|
|
@ -28,6 +28,7 @@ const CartFrontend = ( {
|
|||
isShippingCostHidden,
|
||||
} ) => {
|
||||
const {
|
||||
cartErrors,
|
||||
cartItems,
|
||||
cartTotals,
|
||||
cartIsLoading,
|
||||
|
@ -35,6 +36,10 @@ const CartFrontend = ( {
|
|||
shippingRates,
|
||||
} = useStoreCart();
|
||||
|
||||
if ( cartErrors && cartErrors.length > 0 ) {
|
||||
throw new Error( cartErrors[ 0 ].message );
|
||||
}
|
||||
|
||||
return (
|
||||
<StoreNoticesProvider context="wc/cart">
|
||||
{ ! cartIsLoading && ! cartItems.length ? (
|
||||
|
|
|
@ -57,12 +57,17 @@ const CartLineItemRow = ( { lineItem } ) => {
|
|||
} = lineItem;
|
||||
|
||||
const {
|
||||
cartItemQuantityErrors,
|
||||
quantity,
|
||||
changeQuantity,
|
||||
removeItem,
|
||||
isPending: itemQuantityDisabled,
|
||||
} = useStoreCartItemQuantity( lineItem );
|
||||
|
||||
if ( cartItemQuantityErrors && cartItemQuantityErrors.length > 0 ) {
|
||||
throw new Error( cartItemQuantityErrors[ 0 ].message );
|
||||
}
|
||||
|
||||
const currency = getCurrency( prices );
|
||||
const regularPrice = parseInt( prices.regular_price, 10 ) * quantity;
|
||||
const purchasePrice = parseInt( prices.price, 10 ) * quantity;
|
||||
|
|
|
@ -95,6 +95,7 @@ const Cart = ( {
|
|||
const defaultAddressFields = [ 'country', 'state', 'city', 'postcode' ];
|
||||
const {
|
||||
shippingAddress,
|
||||
shippingRatesErrors,
|
||||
setShippingAddress,
|
||||
shippingRatesLoading,
|
||||
} = useShippingRates( defaultAddressFields );
|
||||
|
@ -103,8 +104,14 @@ const Cart = ( {
|
|||
removeCoupon,
|
||||
isApplyingCoupon,
|
||||
isRemovingCoupon,
|
||||
cartCouponsErrors,
|
||||
} = useStoreCartCoupons();
|
||||
|
||||
const errors = [ ...shippingRatesErrors, ...cartCouponsErrors ];
|
||||
if ( errors.length > 0 ) {
|
||||
throw new Error( errors[ 0 ].message );
|
||||
}
|
||||
|
||||
const showShippingCosts = Boolean(
|
||||
SHIPPING_ENABLED &&
|
||||
isShippingCalculatorEnabled &&
|
||||
|
|
|
@ -21,7 +21,12 @@ import renderFrontend from '../../../utils/render-frontend.js';
|
|||
* @param {Object} props Props for the block.
|
||||
*/
|
||||
const CheckoutFrontend = ( props ) => {
|
||||
const { shippingRates } = useStoreCart();
|
||||
const { cartErrors, shippingRates } = useStoreCart();
|
||||
|
||||
if ( cartErrors && cartErrors.length > 0 ) {
|
||||
throw new Error( cartErrors[ 0 ].message );
|
||||
}
|
||||
|
||||
return (
|
||||
<BlockErrorBoundary
|
||||
header={ __(
|
||||
|
|
|
@ -22,6 +22,7 @@ const useStoreCartApiHydration = () => {
|
|||
const { isResolving, hasFinishedResolution } = select( CART_STORE_KEY );
|
||||
const {
|
||||
receiveCart,
|
||||
receiveError,
|
||||
startResolution,
|
||||
finishResolution,
|
||||
} = registry.dispatch( CART_STORE_KEY );
|
||||
|
@ -31,7 +32,11 @@ const useStoreCartApiHydration = () => {
|
|||
! hasFinishedResolution( 'getCartData', [] )
|
||||
) {
|
||||
startResolution( 'getCartData', [] );
|
||||
receiveCart( cartData.current );
|
||||
if ( cartData.current?.code?.includes( 'error' ) ) {
|
||||
receiveError( cartData.current );
|
||||
} else {
|
||||
receiveCart( cartData.current );
|
||||
}
|
||||
finishResolution( 'getCartData', [] );
|
||||
}
|
||||
}, [] );
|
||||
|
|
|
@ -19,24 +19,28 @@
|
|||
/**
|
||||
* @typedef {Object} StoreCartCoupon
|
||||
*
|
||||
* @property {Array} appliedCoupons Collection of applied coupons from the
|
||||
* API.
|
||||
* @property {boolean} isLoading True when coupon data is being loaded.
|
||||
* @property {Function} applyCoupon Callback for applying a coupon by code.
|
||||
* @property {Function} removeCoupon Callback for removing a coupon by code.
|
||||
* @property {boolean} isApplyingCoupon True when a coupon is being applied.
|
||||
* @property {boolean} isRemovingCoupon True when a coupon is being removed.
|
||||
* @property {Array} appliedCoupons Collection of applied coupons from the
|
||||
* API.
|
||||
* @property {boolean} isLoading True when coupon data is being loaded.
|
||||
* @property {Function} applyCoupon Callback for applying a coupon by code.
|
||||
* @property {Function} removeCoupon Callback for removing a coupon by code.
|
||||
* @property {boolean} isApplyingCoupon True when a coupon is being applied.
|
||||
* @property {boolean} isRemovingCoupon True when a coupon is being removed.
|
||||
* @property {boolean} cartCouponsErrors An array of errors thrown by the cart.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} StoreCartItemQuantity
|
||||
*
|
||||
* @property {number} quantity The quantity of the item in the cart.
|
||||
* @property {boolean} isPending Whether the cart item is updating or
|
||||
* not.
|
||||
* @property {Function} changeQuantity Callback for changing quantity of
|
||||
* item in cart.
|
||||
* @property {Function} removeItem Callback for removing a cart item.
|
||||
* @property {number} quantity The quantity of the item in the
|
||||
* cart.
|
||||
* @property {boolean} isPending Whether the cart item is updating
|
||||
* or not.
|
||||
* @property {Function} changeQuantity Callback for changing quantity
|
||||
* of item in cart.
|
||||
* @property {Function} removeItem Callback for removing a cart item.
|
||||
* @property {Object} cartItemQuantityErrors An array of errors thrown by
|
||||
* the cart.
|
||||
*/
|
||||
|
||||
export {};
|
||||
|
|
Loading…
Reference in New Issue