mirror of
https://github.com/vercel/commerce.git
synced 2025-07-26 19:51:23 +00:00
Moved handler to each hook
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
import { useCallback } from 'react'
|
||||
import useCart from './use-cart'
|
||||
|
||||
import useCartAddItem, {
|
||||
AddItemInput as UseAddItemInput,
|
||||
} from '@commerce/cart/use-add-item'
|
||||
|
||||
import type { HookFetcher } from '@commerce/utils/types'
|
||||
import type { Cart } from '@commerce/types'
|
||||
import checkoutLineItemAddMutation from '../utils/mutations/checkout-line-item-add'
|
||||
import getCheckoutId from '@framework/utils/get-checkout-id'
|
||||
|
||||
import { checkoutLineItemAddMutation, getCheckoutId } from '@framework/utils'
|
||||
import { checkoutToCart } from './utils'
|
||||
|
||||
import { AddCartItemBody, CartItemBody } from '@framework/types'
|
||||
import { MutationCheckoutLineItemsAddArgs } from '@framework/schema'
|
||||
|
||||
|
@@ -1,4 +1,44 @@
|
||||
import useCommerceCart, { UseCart } from '@commerce/cart/use-cart'
|
||||
import { useMemo } from 'react'
|
||||
import type { ShopifyProvider } from '..'
|
||||
|
||||
import useCommerceCart, {
|
||||
FetchCartInput,
|
||||
UseCart,
|
||||
} from '@commerce/cart/use-cart'
|
||||
|
||||
import { Cart } from '@commerce/types'
|
||||
import { HookHandler } from '@commerce/utils/types'
|
||||
|
||||
import fetcher from './utils/fetcher'
|
||||
import getCheckoutQuery from '@framework/utils/queries/get-checkout-query'
|
||||
|
||||
export default useCommerceCart as UseCart<ShopifyProvider>
|
||||
|
||||
export const handler: HookHandler<
|
||||
Cart | null,
|
||||
{},
|
||||
FetchCartInput,
|
||||
{ isEmpty?: boolean }
|
||||
> = {
|
||||
fetchOptions: {
|
||||
query: getCheckoutQuery,
|
||||
},
|
||||
fetcher,
|
||||
useHook({ input, useData }) {
|
||||
const response = useData({
|
||||
swrOptions: { revalidateOnFocus: false, ...input.swrOptions },
|
||||
})
|
||||
return useMemo(
|
||||
() =>
|
||||
Object.create(response, {
|
||||
isEmpty: {
|
||||
get() {
|
||||
return (response.data?.lineItems.length ?? 0) <= 0
|
||||
},
|
||||
enumerable: true,
|
||||
},
|
||||
}),
|
||||
[response]
|
||||
)
|
||||
},
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
import { Cart } from '@commerce/types'
|
||||
import { CommerceError, ValidationError } from '@commerce/utils/errors'
|
||||
import { normalizeCart } from '@framework/lib/normalize'
|
||||
import { normalizeCart } from '@framework/utils/normalize'
|
||||
import { Checkout, Maybe, UserError } from '@framework/schema'
|
||||
|
||||
const checkoutToCart = (checkoutResponse?: {
|
||||
|
30
framework/shopify/cart/utils/fetcher.ts
Normal file
30
framework/shopify/cart/utils/fetcher.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { HookFetcherFn } from '@commerce/utils/types'
|
||||
import { Cart } from '@commerce/types'
|
||||
import { checkoutCreate, checkoutToCart } from '.'
|
||||
import { FetchCartInput } from '@commerce/cart/use-cart'
|
||||
|
||||
const fetcher: HookFetcherFn<Cart | null, FetchCartInput> = async ({
|
||||
options,
|
||||
input: { cartId },
|
||||
fetch,
|
||||
}) => {
|
||||
let checkout
|
||||
|
||||
if (cartId) {
|
||||
const data = await fetch({
|
||||
...options,
|
||||
variables: {
|
||||
cartId,
|
||||
},
|
||||
})
|
||||
checkout = data?.node
|
||||
}
|
||||
|
||||
if (checkout?.completedAt || !cartId) {
|
||||
checkout = await checkoutCreate(fetch)
|
||||
}
|
||||
|
||||
return checkoutToCart({ checkout })
|
||||
}
|
||||
|
||||
export default fetcher
|
@@ -1,2 +1,3 @@
|
||||
export { default as checkoutToCart } from './checkout-to-cart'
|
||||
export { default as checkoutCreate } from './checkout-create'
|
||||
export { default as fetcher } from './fetcher'
|
||||
|
Reference in New Issue
Block a user