4
0
forked from crowetic/commerce
Peter Mekhaeil 300d04c1ac
Shopify Provider ()
* Start of Shopify provider

* add missing comment to documentation

* add missing env vars to documentation

* update reference to types file
2021-02-12 11:14:16 -03:00

31 lines
675 B
TypeScript

import { useCallback } from 'react'
import { LineItemToAdd } from 'shopify-buy'
import { useCommerce } from '../index'
type Options = {
productId: number
variantId: string | number
}
const useAddItem = () => {
const { checkout, client, updateCheckout } = useCommerce()
return useCallback(
async function addItem(options: Options) {
const lineItems: LineItemToAdd[] = [
{
variantId: `${options.variantId}`,
quantity: 1,
},
]
const cart = await client?.checkout.addLineItems(checkout.id, lineItems)
updateCheckout(cart)
return cart
},
[checkout, client]
)
}
export default useAddItem