Moved auth & cart hooks + several fixes

This commit is contained in:
cond0r
2021-02-22 14:06:34 +02:00
parent 005fe9d6c9
commit 528d7556a8
53 changed files with 447 additions and 331 deletions

View File

@@ -7,14 +7,13 @@ import useCommerceCart, {
} 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'
import { SWRHook } from '@commerce/utils/types'
import { checkoutCreate, checkoutToCart } from './utils'
import getCheckoutQuery from '../utils/queries/get-checkout-query'
export default useCommerceCart as UseCart<ShopifyProvider>
export const handler: HookHandler<
export const handler: SWRHook<
Cart | null,
{},
FetchCartInput,
@@ -23,10 +22,27 @@ export const handler: HookHandler<
fetchOptions: {
query: getCheckoutQuery,
},
fetcher,
useHook({ input, useData }) {
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)
}
return checkoutToCart({ checkout })
},
useHook: ({ useData }) => (input) => {
const response = useData({
swrOptions: { revalidateOnFocus: false, ...input.swrOptions },
swrOptions: { revalidateOnFocus: false, ...input?.swrOptions },
})
return useMemo(
() =>