Changed the way isEmpty works

This commit is contained in:
Luis Alvarez
2021-01-20 15:29:02 -05:00
parent a7baff45fc
commit 1db974dabd
7 changed files with 32 additions and 28 deletions

View File

@@ -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, {

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}