diff --git a/components/wishlist/WishlistCard/WishlistCard.tsx b/components/wishlist/WishlistCard/WishlistCard.tsx index f54e3e71e..acd814d59 100644 --- a/components/wishlist/WishlistCard/WishlistCard.tsx +++ b/components/wishlist/WishlistCard/WishlistCard.tsx @@ -24,6 +24,7 @@ const WishlistCard: FC = ({ product }) => { baseAmount: product.price?.retailPrice, currencyCode: product.price?.currencyCode!, }) + // @ts-ignore Wishlist is not always enabled const removeItem = useRemoveItem({ wishlist: { includeProducts: true } }) const [loading, setLoading] = useState(false) @@ -36,6 +37,7 @@ const WishlistCard: FC = ({ product }) => { const handleRemove = async () => { setRemoving(true) + try { // If this action succeeds then there's no need to do `setRemoving(true)` // because the component will be removed from the view diff --git a/framework/shopify/cart/use-cart.tsx b/framework/shopify/cart/use-cart.tsx index 22db9fb80..06596644e 100644 --- a/framework/shopify/cart/use-cart.tsx +++ b/framework/shopify/cart/use-cart.tsx @@ -5,7 +5,7 @@ import { GetCartHook } from '../types/cart' import { GetCartQueryVariables, QueryRoot } from '../schema' import { normalizeCart, getCartQuery, setCheckoutUrlCookie } from '../utils' import Cookies from 'js-cookie' -import { SHOPIFY_CART_ID_COOKIE } from '@framework/const' +import { SHOPIFY_CART_ID_COOKIE } from '../const' export default useCommerceCart as UseCart diff --git a/framework/shopify/utils/normalize.ts b/framework/shopify/utils/normalize.ts index 2e333ba01..5540c5b7e 100644 --- a/framework/shopify/utils/normalize.ts +++ b/framework/shopify/utils/normalize.ts @@ -21,11 +21,14 @@ import { colorMap } from '@lib/colors' import { CommerceError } from '@commerce/utils/errors' import type { Wishlist } from '@commerce/types/wishlist' -const money = ({ amount, currencyCode }: MoneyV2) => { - return { - value: +amount, - currencyCode, +const money = (money?: MoneyV2) => { + if (money) { + return { + value: +money.amount, + currencyCode: money.currencyCode, + } } + return null } const normalizeProductOption = ({ @@ -206,9 +209,13 @@ export function normalizeWishlist( ], id, variants: variant?.id ? [{ id: variant?.id }] : [], - amount: +variant?.priceV2?.amount, - baseAmount: +variant?.compareAtPriceV2?.amount, - currencyCode: variant?.priceV2?.currencyCode, + price: { + value: +variant?.priceV2?.amount, + currencyCode: variant?.priceV2?.currencyCode, + retailPrice: variant?.compareAtPriceV2 + ? +variant?.compareAtPriceV2.amount + : null, + }, }, })) ?? [], } diff --git a/framework/shopify/wishlist/use-add-item.tsx b/framework/shopify/wishlist/use-add-item.tsx index ebb03b685..03a67f99f 100644 --- a/framework/shopify/wishlist/use-add-item.tsx +++ b/framework/shopify/wishlist/use-add-item.tsx @@ -6,7 +6,6 @@ import type { AddItemHook } from '../types/wishlist' import { getWishlistId, - normalizeCart, normalizeWishlist, throwUserErrors, wishlistCreate, @@ -34,7 +33,7 @@ export const handler: MutationHook = { if (!wishlistId) { const cart = await wishlistCreate(fetch, lines) - return normalizeCart(cart) + return normalizeWishlist(cart) } else { const { cartLinesAdd } = await fetch< CartLinesAddMutation, @@ -46,9 +45,7 @@ export const handler: MutationHook = { lines, }, }) - throwUserErrors(cartLinesAdd?.userErrors) - return normalizeWishlist(cartLinesAdd?.cart) } }, diff --git a/framework/shopify/wishlist/use-wishlist.tsx b/framework/shopify/wishlist/use-wishlist.tsx index 71c7c5b91..bc99f2c63 100644 --- a/framework/shopify/wishlist/use-wishlist.tsx +++ b/framework/shopify/wishlist/use-wishlist.tsx @@ -4,6 +4,8 @@ import useWishlist, { UseWishlist } from '@commerce/wishlist/use-wishlist' import type { GetWishlistHook } from '../types/wishlist' import { getWishlistId, normalizeWishlist, getWishlistQuery } from '../utils' import { GetCartQueryVariables, QueryRoot } from '../schema' +import Cookies from 'js-cookie' +import { SHOPIFY_WHISLIST_ID_COOKIE } from '../const' export default useWishlist as UseWishlist @@ -13,16 +15,17 @@ export const handler: SWRHook = { }, async fetcher({ input: _input, options, fetch }) { const wishListId = getWishlistId() - if (wishListId) { - const { cart } = await fetch({ + const { cart: wishlist } = await fetch({ ...options, variables: { cartId: wishListId }, }) - - return normalizeWishlist(cart) + if (wishlist) { + return normalizeWishlist(wishlist) + } else { + Cookies.remove(SHOPIFY_WHISLIST_ID_COOKIE) + } } - return null }, useHook: