forked from crowetic/commerce
Added initial version of useAddItem
This commit is contained in:
parent
7f0c1eb66c
commit
89cc3d0608
@ -1,66 +1,60 @@
|
|||||||
import { useCallback } from 'react'
|
import type { MutationHandler } from '@commerce/utils/types'
|
||||||
|
import { CommerceError } from '@commerce/utils/errors'
|
||||||
|
import useAddItem, { UseAddItem } from '@commerce/cart/use-add-item'
|
||||||
import useCart from './use-cart'
|
import useCart from './use-cart'
|
||||||
|
import { ShopifyProvider } from '..'
|
||||||
import useCartAddItem, {
|
import { AddCartItemBody, CartItemBody } from '@commerce/types'
|
||||||
AddItemInput as UseAddItemInput,
|
import { Cart } from '@framework/types'
|
||||||
} from '@commerce/cart/use-add-item'
|
import {
|
||||||
|
checkoutLineItemAddMutation,
|
||||||
import type { HookFetcher } from '@commerce/utils/types'
|
getCheckoutId,
|
||||||
import type { Cart } from '@commerce/types'
|
getCheckoutQuery,
|
||||||
|
} from '@framework/utils'
|
||||||
import { checkoutLineItemAddMutation, getCheckoutId } from '@framework/utils'
|
|
||||||
import { checkoutToCart } from './utils'
|
import { checkoutToCart } from './utils'
|
||||||
|
|
||||||
import { AddCartItemBody, CartItemBody } from '@framework/types'
|
|
||||||
import { MutationCheckoutLineItemsAddArgs } from '@framework/schema'
|
|
||||||
|
|
||||||
const defaultOpts = {
|
const defaultOpts = {
|
||||||
query: checkoutLineItemAddMutation,
|
query: checkoutLineItemAddMutation,
|
||||||
}
|
}
|
||||||
|
|
||||||
export type AddItemInput = UseAddItemInput<CartItemBody>
|
export default useAddItem as UseAddItem<ShopifyProvider, CartItemBody>
|
||||||
|
|
||||||
export const fetcher: HookFetcher<
|
export const handler: MutationHandler<Cart, {}, AddCartItemBody> = {
|
||||||
Cart,
|
fetchOptions: {
|
||||||
MutationCheckoutLineItemsAddArgs
|
query: checkoutLineItemAddMutation,
|
||||||
> = async (options, { checkoutId, lineItems }, fetch) => {
|
},
|
||||||
const data = await fetch<any, AddCartItemBody>({
|
async fetcher({ input, options, fetch }) {
|
||||||
|
const item = input.item ?? input
|
||||||
|
if (
|
||||||
|
item.quantity &&
|
||||||
|
(!Number.isInteger(item.quantity) || item.quantity! < 1)
|
||||||
|
) {
|
||||||
|
throw new CommerceError({
|
||||||
|
message: 'The item quantity has to be a valid integer greater than 0',
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await fetch<any, any>({
|
||||||
|
...defaultOpts,
|
||||||
...options,
|
...options,
|
||||||
variables: {
|
variables: {
|
||||||
checkoutId,
|
|
||||||
lineItems,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
return checkoutToCart(data?.checkoutLineItemsAdd)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function extendHook(customFetcher: typeof fetcher) {
|
|
||||||
const useAddItem = () => {
|
|
||||||
const { mutate, data: cart } = useCart()
|
|
||||||
const fn = useCartAddItem(defaultOpts, customFetcher)
|
|
||||||
|
|
||||||
return useCallback(
|
|
||||||
async function addItem(input: AddItemInput) {
|
|
||||||
const data = await fn({
|
|
||||||
lineItems: [
|
lineItems: [
|
||||||
{
|
{
|
||||||
variantId: input.variantId,
|
variantId: item.variantId,
|
||||||
quantity: input.quantity ?? 1,
|
quantity: item.quantity ?? 1,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
checkoutId: getCheckoutId(cart?.id)!,
|
checkoutId: getCheckoutId(),
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return checkoutToCart(data.checkoutLineItemsAdd)
|
||||||
|
},
|
||||||
|
useHook() {
|
||||||
|
const { mutate } = useCart()
|
||||||
|
return async function addItem({ input, fetch }) {
|
||||||
|
const data = await fetch({ input })
|
||||||
await mutate(data, false)
|
await mutate(data, false)
|
||||||
return data
|
return data
|
||||||
|
}
|
||||||
},
|
},
|
||||||
[fn, mutate]
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
useAddItem.extend = extendHook
|
|
||||||
|
|
||||||
return useAddItem
|
|
||||||
}
|
|
||||||
|
|
||||||
export default extendHook(fetcher)
|
|
||||||
|
@ -34,7 +34,7 @@ export const fetcher: HookFetcher<Cart | null, any> = async (
|
|||||||
...options,
|
...options,
|
||||||
variables: { lineItemIds: [itemId], checkoutId },
|
variables: { lineItemIds: [itemId], checkoutId },
|
||||||
})
|
})
|
||||||
return checkoutToCart(data?.checkoutLineItemsRemove)
|
return checkoutToCart(data.checkoutLineItemsRemove)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function extendHook(customFetcher: typeof fetcher) {
|
export function extendHook(customFetcher: typeof fetcher) {
|
||||||
|
@ -44,7 +44,7 @@ export const fetcher: HookFetcher<Cart | null, any> = async (
|
|||||||
variables: { checkoutId, lineItems: [item] },
|
variables: { checkoutId, lineItems: [item] },
|
||||||
})
|
})
|
||||||
|
|
||||||
return checkoutToCart(data?.checkoutLineItemsUpdate)
|
return checkoutToCart(data.checkoutLineItemsUpdate)
|
||||||
}
|
}
|
||||||
|
|
||||||
function extendHook(customFetcher: typeof fetcher, cfg?: { wait?: number }) {
|
function extendHook(customFetcher: typeof fetcher, cfg?: { wait?: number }) {
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
import { Cart } from '@commerce/types'
|
import { Cart } from '@commerce/types'
|
||||||
import { CommerceError, ValidationError } from '@commerce/utils/errors'
|
import { ValidationError } from '@commerce/utils/errors'
|
||||||
import { normalizeCart } from '@framework/utils/normalize'
|
import { normalizeCart } from '@framework/utils/normalize'
|
||||||
import { Checkout, Maybe, UserError } from '@framework/schema'
|
import { Checkout, UserError } from '@framework/schema'
|
||||||
|
|
||||||
const checkoutToCart = (checkoutResponse?: {
|
const checkoutToCart = (checkoutResponse: {
|
||||||
checkout: Checkout
|
checkout: Checkout
|
||||||
userErrors?: UserError[]
|
userErrors?: UserError[]
|
||||||
}): Maybe<Cart> => {
|
}): Cart => {
|
||||||
const checkout = checkoutResponse?.checkout
|
const checkout = checkoutResponse?.checkout
|
||||||
const userErrors = checkoutResponse?.userErrors
|
const userErrors = checkoutResponse?.userErrors
|
||||||
|
|
||||||
@ -16,12 +16,6 @@ const checkoutToCart = (checkoutResponse?: {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!checkout) {
|
|
||||||
throw new CommerceError({
|
|
||||||
message: 'Missing checkout details from response cart Response',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return normalizeCart(checkout)
|
return normalizeCart(checkout)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,22 +5,22 @@ import { FetchCartInput } from '@commerce/cart/use-cart'
|
|||||||
|
|
||||||
const fetcher: HookFetcherFn<Cart | null, FetchCartInput> = async ({
|
const fetcher: HookFetcherFn<Cart | null, FetchCartInput> = async ({
|
||||||
options,
|
options,
|
||||||
input: { cartId },
|
input: { cartId: checkoutId },
|
||||||
fetch,
|
fetch,
|
||||||
}) => {
|
}) => {
|
||||||
let checkout
|
let checkout
|
||||||
|
|
||||||
if (cartId) {
|
if (checkoutId) {
|
||||||
const data = await fetch({
|
const data = await fetch({
|
||||||
...options,
|
...options,
|
||||||
variables: {
|
variables: {
|
||||||
cartId,
|
checkoutId,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
checkout = data?.node
|
checkout = data?.node
|
||||||
}
|
}
|
||||||
|
|
||||||
if (checkout?.completedAt || !cartId) {
|
if (checkout?.completedAt || !checkoutId) {
|
||||||
checkout = await checkoutCreate(fetch)
|
checkout = await checkoutCreate(fetch)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
import { SHOPIFY_CHECKOUT_ID_COOKIE, STORE_DOMAIN } from './const'
|
import { SHOPIFY_CHECKOUT_ID_COOKIE, STORE_DOMAIN } from './const'
|
||||||
|
|
||||||
import { handler as useCart } from '@framework/cart/use-cart'
|
import { handler as useCart } from './cart/use-cart'
|
||||||
import { handler as useSearch } from '@framework/product/use-search'
|
import { handler as useAddItem } from './cart/use-add-item'
|
||||||
import { handler as useCustomer } from '@framework/customer/use-customer'
|
import { handler as useSearch } from './product/use-search'
|
||||||
|
import { handler as useCustomer } from './customer/use-customer'
|
||||||
import fetcher from './fetcher'
|
import fetcher from './fetcher'
|
||||||
|
|
||||||
export const shopifyProvider = {
|
export const shopifyProvider = {
|
||||||
@ -10,7 +11,7 @@ export const shopifyProvider = {
|
|||||||
cartCookie: SHOPIFY_CHECKOUT_ID_COOKIE,
|
cartCookie: SHOPIFY_CHECKOUT_ID_COOKIE,
|
||||||
storeDomain: STORE_DOMAIN,
|
storeDomain: STORE_DOMAIN,
|
||||||
fetcher,
|
fetcher,
|
||||||
cart: { useCart },
|
cart: { useCart, useAddItem },
|
||||||
customer: { useCustomer },
|
customer: { useCustomer },
|
||||||
products: { useSearch },
|
products: { useSearch },
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ export type ShopifyCheckout = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface Cart extends Core.Cart {
|
export interface Cart extends Core.Cart {
|
||||||
|
id: string
|
||||||
lineItems: LineItem[]
|
lineItems: LineItem[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,7 +107,7 @@ function normalizeLineItem({
|
|||||||
variantId: String(variant?.id),
|
variantId: String(variant?.id),
|
||||||
productId: String(variant?.id),
|
productId: String(variant?.id),
|
||||||
name: `${title} - ${variant?.title}`,
|
name: `${title} - ${variant?.title}`,
|
||||||
quantity: quantity,
|
quantity,
|
||||||
variant: {
|
variant: {
|
||||||
id: String(variant?.id),
|
id: String(variant?.id),
|
||||||
sku: variant?.sku ?? '',
|
sku: variant?.sku ?? '',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user