commerce/framework/commercelayer/cart/use-remove-item.tsx
2022-01-21 11:09:33 +01:00

28 lines
793 B
TypeScript

import { MutationHook } from '@commerce/utils/types'
import useRemoveItem, { UseRemoveItem } from '@commerce/cart/use-remove-item'
import getCredentials from '@framework/api/utils/getCredentials'
import { LineItem } from '@commercelayer/js-sdk'
export default useRemoveItem as UseRemoveItem<typeof handler>
export const handler: MutationHook<any> = {
fetchOptions: {
query: '',
},
async fetcher({ input: { id } }) {
const credentials = getCredentials()
const orderId = localStorage.getItem('CL_ORDER')
if (orderId && id) {
await LineItem.build({ id }).withCredentials(credentials).destroy()
return {}
}
},
useHook:
({ fetch }) =>
() => {
return async function removeItem(input) {
return await fetch({ input })
}
},
}