forked from crowetic/commerce
.vscode
assets
components
config
docs
framework
bigcommerce
api
auth
cart
index.ts
use-add-item.tsx
use-cart.tsx
use-remove-item.tsx
use-update-item.tsx
common
customer
lib
product
scripts
wishlist
.env.template
README.md
commerce.config.json
fetcher.ts
index.tsx
next.config.js
provider.ts
schema.d.ts
schema.graphql
types.ts
commerce
shopify
swell
lib
pages
public
.editorconfig
.env.template
.gitignore
.prettierignore
.prettierrc
README.md
codegen.json
commerce.config.json
global.d.ts
license.md
next-env.d.ts
next.config.js
package.json
postcss.config.js
tailwind.config.js
tsconfig.json
yarn.lock
51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
import { useCallback } from 'react'
|
|
import type { MutationHook } from '@commerce/utils/types'
|
|
import { CommerceError } from '@commerce/utils/errors'
|
|
import useAddItem, { UseAddItem } from '@commerce/cart/use-add-item'
|
|
import { normalizeCart } from '../lib/normalize'
|
|
import type {
|
|
Cart,
|
|
BigcommerceCart,
|
|
CartItemBody,
|
|
AddCartItemBody,
|
|
} from '../types'
|
|
import useCart from './use-cart'
|
|
|
|
export default useAddItem as UseAddItem<typeof handler>
|
|
|
|
export const handler: MutationHook<Cart, {}, CartItemBody> = {
|
|
fetchOptions: {
|
|
url: '/api/bigcommerce/cart',
|
|
method: 'POST',
|
|
},
|
|
async fetcher({ input: item, options, fetch }) {
|
|
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<BigcommerceCart, AddCartItemBody>({
|
|
...options,
|
|
body: { item },
|
|
})
|
|
|
|
return normalizeCart(data)
|
|
},
|
|
useHook: ({ fetch }) => () => {
|
|
const { mutate } = useCart()
|
|
|
|
return useCallback(
|
|
async function addItem(input) {
|
|
const data = await fetch({ input })
|
|
await mutate(data, false)
|
|
return data
|
|
},
|
|
[fetch, mutate]
|
|
)
|
|
},
|
|
}
|