commerce/packages/spree/src/wishlist/use-remove-item.tsx
Catalin Pinte 6c2610584d
Update types (#831)
* Update product types

* Cart types progress, add zod & initial schema validator

* Update normalize.ts

* Update with-schema-parser.ts

* Updated types, schemas & providers

* Fix providers after schema parse errors

* Fix paths

* More provider fixes

* Fix kibocommerce & commercejs

* Add customer updated types & fixes

* Add checkout & customer types

* Import core types only from commerce

* Update tsconfig.json

* Convert hooks interfaces to types

* Requested changes

* Change to relative paths

* Move Zod dependency
2022-10-05 09:02:29 +03:00

76 lines
2.0 KiB
TypeScript

import { useCallback } from 'react'
import type { MutationHook } from '@vercel/commerce/utils/types'
import useRemoveItem from '@vercel/commerce/wishlist/use-remove-item'
import type { UseRemoveItem } from '@vercel/commerce/wishlist/use-remove-item'
import type { RemoveItemHook } from '@vercel/commerce/types/wishlist'
import useWishlist from './use-wishlist'
import isLoggedIn from '../utils/tokens/is-logged-in'
import ensureIToken from '../utils/tokens/ensure-itoken'
import type { IToken } from '@spree/storefront-api-v2-sdk/types/interfaces/Token'
import type { GraphQLFetcherResult } from '@vercel/commerce/api'
import type { WishedItem } from '@spree/storefront-api-v2-sdk/types/interfaces/WishedItem'
export default useRemoveItem as UseRemoveItem<typeof handler>
export const handler: MutationHook<RemoveItemHook> = {
fetchOptions: {
url: 'wishlists',
query: 'removeWishedItem',
},
async fetcher({ input, options, fetch }) {
console.info(
'useRemoveItem (wishlist) fetcher called. Configuration: ',
'input: ',
input,
'options: ',
options
)
const { itemId, wishlistToken } = input
if (!isLoggedIn() || !wishlistToken) {
return null
}
let token: IToken | undefined = ensureIToken()
await fetch<GraphQLFetcherResult<WishedItem>>({
variables: {
methodPath: 'wishlists.removeWishedItem',
arguments: [token, wishlistToken, itemId],
},
})
return null
},
useHook: ({ fetch }) => {
const useWrappedHook: ReturnType<
MutationHook<RemoveItemHook>['useHook']
> = () => {
const wishlist = useWishlist()
return useCallback(
async (input) => {
if (!wishlist.data) {
return null
}
const data = await fetch({
input: {
itemId: `${input.id}`,
wishlistToken: wishlist.data.token,
},
})
await wishlist.mutate()
return data
},
[wishlist]
)
}
return useWrappedHook
},
}