diff --git a/components/cart/CartSidebarView/CartSidebarView.tsx b/components/cart/CartSidebarView/CartSidebarView.tsx index 1f54b96f9..5cd409559 100644 --- a/components/cart/CartSidebarView/CartSidebarView.tsx +++ b/components/cart/CartSidebarView/CartSidebarView.tsx @@ -11,7 +11,7 @@ import s from './CartSidebarView.module.css' const CartSidebarView: FC = () => { const { closeSidebar } = useUI() - const { data, isEmpty } = useCart() + const { data, isLoading, isEmpty } = useCart() const { price: subTotal } = usePrice( data && { @@ -19,7 +19,7 @@ const CartSidebarView: FC = () => { currencyCode: data.currency?.code || 'USD', } ) - + const { price: total } = usePrice( data && { amount: Number(data.total), @@ -34,9 +34,7 @@ const CartSidebarView: FC = () => { return (
@@ -56,7 +54,7 @@ const CartSidebarView: FC = () => {
- {isEmpty ? ( + {isLoading || isEmpty ? (
diff --git a/framework/bigcommerce/cart/use-cart.tsx b/framework/bigcommerce/cart/use-cart.tsx index aab4c2e6f..708db68c5 100644 --- a/framework/bigcommerce/cart/use-cart.tsx +++ b/framework/bigcommerce/cart/use-cart.tsx @@ -1,6 +1,7 @@ import { normalizeCart } from '../lib/normalize' import type { HookFetcher } from '@commerce/utils/types' import type { SwrOptions } from '@commerce/utils/use-data' +import defineProperty from '@commerce/utils/define-property' import useCommerceCart, { CartInput } from '@commerce/cart/use-cart' import type { Cart as BigCommerceCart } from '../api/cart' import update from '@framework/lib/immutability' @@ -32,14 +33,16 @@ export function extendHook( // Uses a getter to only calculate the prop when required // response.data is also a getter and it's better to not trigger it early - Object.defineProperty(response, 'isEmpty', { - get() { - return Object.values(response.data?.line_items ?? {}).every( - (items) => !items.length - ) - }, - set: (x) => x, - }) + if (!('isEmpty' in response)) { + defineProperty(response, 'isEmpty', { + get() { + return Object.values(response.data?.line_items ?? {}).every( + (items) => !items.length + ) + }, + set: (x) => x, + }) + } return response.data ? update(response, { diff --git a/framework/bigcommerce/wishlist/use-wishlist.tsx b/framework/bigcommerce/wishlist/use-wishlist.tsx index 0e1671039..870b90019 100644 --- a/framework/bigcommerce/wishlist/use-wishlist.tsx +++ b/framework/bigcommerce/wishlist/use-wishlist.tsx @@ -1,5 +1,6 @@ import { HookFetcher } from '@commerce/utils/types' import { SwrOptions } from '@commerce/utils/use-data' +import defineProperty from '@commerce/utils/define-property' import useCommerceWishlist from '@commerce/wishlist/use-wishlist' import type { Wishlist } from '../api/wishlist' import useCustomer from '../customer/use-customer' @@ -58,12 +59,14 @@ export function extendHook( // Uses a getter to only calculate the prop when required // response.data is also a getter and it's better to not trigger it early - Object.defineProperty(response, 'isEmpty', { - get() { - return (response.data?.items?.length || 0) <= 0 - }, - set: (x) => x, - }) + if (!('isEmpty' in response)) { + defineProperty(response, 'isEmpty', { + get() { + return (response.data?.items?.length || 0) <= 0 + }, + set: (x) => x, + }) + } return response } diff --git a/framework/commerce/cart/use-cart.tsx b/framework/commerce/cart/use-cart.tsx index af472244d..513c041ea 100644 --- a/framework/commerce/cart/use-cart.tsx +++ b/framework/commerce/cart/use-cart.tsx @@ -4,7 +4,7 @@ import type { HookInput, HookFetcher, HookFetcherOptions } from '../utils/types' import useData, { ResponseState, SwrOptions } from '../utils/use-data' import { useCommerce } from '..' -export type CartResponse = ResponseState & { isEmpty: boolean } +export type CartResponse = ResponseState & { isEmpty?: boolean } export type CartInput = { cartId: Cart['id'] @@ -25,5 +25,5 @@ export default function useCart( const response = useData(options, input, fetcher, swrOptions) - return Object.assign(response, { isEmpty: true }) + return response } diff --git a/framework/commerce/wishlist/use-wishlist.tsx b/framework/commerce/wishlist/use-wishlist.tsx index 66199e380..62b1c8702 100644 --- a/framework/commerce/wishlist/use-wishlist.tsx +++ b/framework/commerce/wishlist/use-wishlist.tsx @@ -3,7 +3,7 @@ import type { HookInput, HookFetcher, HookFetcherOptions } from '../utils/types' import useData, { ResponseState, SwrOptions } from '../utils/use-data' export type WishlistResponse = ResponseState & { - isEmpty: boolean + isEmpty?: boolean } export default function useWishlist( @@ -13,5 +13,5 @@ export default function useWishlist( swrOptions?: SwrOptions ): WishlistResponse { const response = useData(options, input, fetcherFn, swrOptions) - return Object.assign(response, { isEmpty: true }) + return response } diff --git a/pages/cart.tsx b/pages/cart.tsx index 2021291a1..b7f2027d3 100644 --- a/pages/cart.tsx +++ b/pages/cart.tsx @@ -24,7 +24,7 @@ export async function getStaticProps({ export default function Cart() { const error = null const success = null - const { data, isEmpty } = useCart() + const { data, isLoading, isEmpty } = useCart() const { price: subTotal } = usePrice( data && { @@ -42,7 +42,7 @@ export default function Cart() { return (
- {isEmpty ? ( + {isLoading || isEmpty ? (
diff --git a/pages/wishlist.tsx b/pages/wishlist.tsx index 9df111dc6..a3f25d0e7 100644 --- a/pages/wishlist.tsx +++ b/pages/wishlist.tsx @@ -22,14 +22,14 @@ export async function getStaticProps({ export default function Wishlist() { const { data: customer } = useCustomer() - const { data, isEmpty } = useWishlist() + const { data, isLoading, isEmpty } = useWishlist() return (
My Wishlist
- {isEmpty ? ( + {isLoading || isEmpty ? (