diff --git a/packages/storefront-react/src/hooks/use-shop-wishlist.hook.ts b/packages/storefront-react/src/hooks/use-shop-wishlist.hook.ts index 99d24ee..395714a 100644 --- a/packages/storefront-react/src/hooks/use-shop-wishlist.hook.ts +++ b/packages/storefront-react/src/hooks/use-shop-wishlist.hook.ts @@ -1,4 +1,5 @@ -import { useContext } from 'react' +import { MakairaResponse } from '@makaira/storefront-types' +import { useCallback, useContext } from 'react' import { ShopContext, ShopContextData } from '../context' export type UseShopWishlistData = { @@ -6,10 +7,28 @@ export type UseShopWishlistData = { * The current wishlist with the products */ wishlist: ShopContextData['wishlist'] + /** + * + */ + isProductInWishlist: (id: string) => MakairaResponse } export function useShopWishlist(): UseShopWishlistData { const { wishlist } = useContext(ShopContext) - return { wishlist } + const isProductInWishlist = useCallback( + (id: string): MakairaResponse => { + 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 } }