Skip to content

Commit

Permalink
Merge pull request #28 from MakairaIO/feat/add-is-product-part-of-wis…
Browse files Browse the repository at this point in the history
…hlist

feat(useShopWishlist): add isProductInWishlist to check if product is…
  • Loading branch information
jembach authored Jul 12, 2022
2 parents ea10d72 + 528f5f8 commit 34bcb23
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions packages/storefront-react/src/hooks/use-shop-wishlist.hook.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
import { useContext } from 'react'
import { MakairaResponse } from '@makaira/storefront-types'
import { useCallback, useContext } from 'react'
import { ShopContext, ShopContextData } from '../context'

export type UseShopWishlistData = {
/**
* The current wishlist with the products
*/
wishlist: ShopContextData['wishlist']
/**
*
*/
isProductInWishlist: (id: string) => MakairaResponse<boolean, Error>
}

export function useShopWishlist(): UseShopWishlistData {
const { wishlist } = useContext(ShopContext)

return { wishlist }
const isProductInWishlist = useCallback(
(id: string): MakairaResponse<boolean, Error> => {
if (wishlist?.items) {
return {
data: wishlist.items.some(({ product }) => product.id === id),
error: undefined,
}
}

return { data: undefined, error: new Error('Wishlist is not loaded') }
},
[wishlist]
)

return { wishlist, isProductInWishlist }
}

0 comments on commit 34bcb23

Please sign in to comment.