mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
update get product hook
This commit is contained in:
@@ -1,60 +1,28 @@
|
||||
import { useMemo } from 'react'
|
||||
import useCommerceCart, {
|
||||
FetchCartInput,
|
||||
UseCart,
|
||||
} from '@commerce/cart/use-cart'
|
||||
|
||||
import { Cart } from '../types'
|
||||
import useCart, { UseCart } from '@commerce/cart/use-cart'
|
||||
import { Customer } from '@commerce/types'
|
||||
import { SWRHook } from '@commerce/utils/types'
|
||||
import { checkoutCreate, checkoutToCart } from './utils'
|
||||
import getCheckoutQuery from '../utils/queries/get-checkout-query'
|
||||
import { normalizeCart } from '../utils/normalize'
|
||||
// import { getCustomerQuery, getCustomerToken } from '../utils'
|
||||
|
||||
export default useCommerceCart as UseCart<typeof handler>
|
||||
export default useCart as UseCart<typeof handler>
|
||||
|
||||
export const handler: SWRHook<
|
||||
Cart | null,
|
||||
{},
|
||||
FetchCartInput,
|
||||
{ isEmpty?: boolean }
|
||||
> = {
|
||||
export const handler: SWRHook<Customer | null> = {
|
||||
fetchOptions: {
|
||||
query: 'cart',
|
||||
method: 'get',
|
||||
},
|
||||
async fetcher({ input: { cartId: checkoutId }, options, fetch }) {
|
||||
let checkout
|
||||
if (checkoutId) {
|
||||
const data = await fetch({
|
||||
...options,
|
||||
variables: {
|
||||
checkoutId,
|
||||
},
|
||||
})
|
||||
checkout = data.node
|
||||
}
|
||||
|
||||
if (checkout?.completedAt || !checkoutId) {
|
||||
checkout = await checkoutCreate(fetch)
|
||||
}
|
||||
|
||||
// TODO: Fix this type
|
||||
return checkoutToCart({ checkout } as any)
|
||||
async fetcher({ options, fetch }) {
|
||||
const data = await fetch<any | null>({
|
||||
...options,
|
||||
})
|
||||
return data ? normalizeCart(data) : null
|
||||
},
|
||||
useHook: ({ useData }) => (input) => {
|
||||
const response = useData({
|
||||
swrOptions: { revalidateOnFocus: false, ...input?.swrOptions },
|
||||
return useData({
|
||||
swrOptions: {
|
||||
revalidateOnFocus: false,
|
||||
...input?.swrOptions,
|
||||
},
|
||||
})
|
||||
return useMemo(
|
||||
() =>
|
||||
Object.create(response, {
|
||||
isEmpty: {
|
||||
get() {
|
||||
return (response.data?.lineItems.length ?? 0) <= 0
|
||||
},
|
||||
enumerable: true,
|
||||
},
|
||||
}),
|
||||
[response]
|
||||
)
|
||||
},
|
||||
}
|
||||
|
@@ -4,26 +4,27 @@ import {
|
||||
SHOPIFY_COOKIE_EXPIRE,
|
||||
} from '../../const'
|
||||
|
||||
import checkoutCreateMutation from '../../utils/mutations/checkout-create'
|
||||
// import checkoutCreateMutation from '../../utils/mutations/checkout-create'
|
||||
import Cookies from 'js-cookie'
|
||||
|
||||
export const checkoutCreate = async (fetch: any) => {
|
||||
const data = await fetch({
|
||||
query: checkoutCreateMutation,
|
||||
const cart = await fetch({
|
||||
query: 'cart',
|
||||
method: 'get',
|
||||
})
|
||||
|
||||
const checkout = data.checkoutCreate?.checkout
|
||||
const checkoutId = checkout?.id
|
||||
// const checkout = data.checkoutCreate?.checkout
|
||||
const checkoutId = cart?.id
|
||||
|
||||
if (checkoutId) {
|
||||
const options = {
|
||||
expires: SHOPIFY_COOKIE_EXPIRE,
|
||||
}
|
||||
Cookies.set(SHOPIFY_CHECKOUT_ID_COOKIE, checkoutId, options)
|
||||
Cookies.set(SHOPIFY_CHECKOUT_URL_COOKIE, checkout?.webUrl, options)
|
||||
}
|
||||
// if (checkoutId) {
|
||||
// const options = {
|
||||
// expires: SHOPIFY_COOKIE_EXPIRE,
|
||||
// }
|
||||
// Cookies.set(SHOPIFY_CHECKOUT_ID_COOKIE, checkoutId, options)
|
||||
// Cookies.set(SHOPIFY_CHECKOUT_URL_COOKIE, checkout?.webUrl, options)
|
||||
// }
|
||||
|
||||
return checkout
|
||||
return cart
|
||||
}
|
||||
|
||||
export default checkoutCreate
|
||||
|
@@ -1,31 +1,33 @@
|
||||
import { HookFetcherFn } from '@commerce/utils/types'
|
||||
import { Cart } from '@commerce/types'
|
||||
import { checkoutCreate, checkoutToCart } from '.'
|
||||
// import { checkoutCreate, checkoutToCart } from '.'
|
||||
import { FetchCartInput } from '@commerce/cart/use-cart'
|
||||
import { data } from 'autoprefixer'
|
||||
import { normalizeCart } from '../../utils'
|
||||
|
||||
const fetcher: HookFetcherFn<Cart | null, FetchCartInput> = async ({
|
||||
options,
|
||||
input: { cartId: checkoutId },
|
||||
// input: { cartId: checkoutId },
|
||||
fetch,
|
||||
}) => {
|
||||
let checkout
|
||||
|
||||
if (checkoutId) {
|
||||
const data = await fetch({
|
||||
...options,
|
||||
variables: {
|
||||
checkoutId,
|
||||
},
|
||||
})
|
||||
checkout = data.node
|
||||
}
|
||||
// if (checkoutId) {
|
||||
const data = await fetch({
|
||||
query: 'cart',
|
||||
method: 'get',
|
||||
// variables: { category: categoryId },
|
||||
})
|
||||
// checkout = data.node
|
||||
// }
|
||||
|
||||
if (checkout?.completedAt || !checkoutId) {
|
||||
checkout = await checkoutCreate(fetch)
|
||||
}
|
||||
// if (checkout?.completedAt || !checkoutId) {
|
||||
// checkout = await checkoutCreate(fetch)
|
||||
// }
|
||||
|
||||
// TODO: Fix this type
|
||||
return checkoutToCart({ checkout } as any)
|
||||
// return checkoutToCart({ checkout } as any)
|
||||
return normalizeCart(data)
|
||||
}
|
||||
|
||||
export default fetcher
|
||||
|
Reference in New Issue
Block a user