mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
Changed the way isEmpty works
This commit is contained in:
@@ -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, {
|
||||
|
@@ -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
|
||||
}
|
||||
|
@@ -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<Result> = ResponseState<Result> & { isEmpty: boolean }
|
||||
export type CartResponse<Result> = ResponseState<Result> & { isEmpty?: boolean }
|
||||
|
||||
export type CartInput = {
|
||||
cartId: Cart['id']
|
||||
@@ -25,5 +25,5 @@ export default function useCart<Result>(
|
||||
|
||||
const response = useData(options, input, fetcher, swrOptions)
|
||||
|
||||
return Object.assign(response, { isEmpty: true })
|
||||
return response
|
||||
}
|
||||
|
@@ -3,7 +3,7 @@ import type { HookInput, HookFetcher, HookFetcherOptions } from '../utils/types'
|
||||
import useData, { ResponseState, SwrOptions } from '../utils/use-data'
|
||||
|
||||
export type WishlistResponse<Result> = ResponseState<Result> & {
|
||||
isEmpty: boolean
|
||||
isEmpty?: boolean
|
||||
}
|
||||
|
||||
export default function useWishlist<Result, Input = null>(
|
||||
@@ -13,5 +13,5 @@ export default function useWishlist<Result, Input = null>(
|
||||
swrOptions?: SwrOptions<Result, Input>
|
||||
): WishlistResponse<Result> {
|
||||
const response = useData(options, input, fetcherFn, swrOptions)
|
||||
return Object.assign(response, { isEmpty: true })
|
||||
return response
|
||||
}
|
||||
|
Reference in New Issue
Block a user