Complete wishlist

This commit is contained in:
cond0r 2021-08-10 17:09:45 +03:00
parent 3364d13451
commit d4e1effc7c
5 changed files with 26 additions and 17 deletions

View File

@ -24,6 +24,7 @@ const WishlistCard: FC<Props> = ({ product }) => {
baseAmount: product.price?.retailPrice, baseAmount: product.price?.retailPrice,
currencyCode: product.price?.currencyCode!, currencyCode: product.price?.currencyCode!,
}) })
// @ts-ignore Wishlist is not always enabled // @ts-ignore Wishlist is not always enabled
const removeItem = useRemoveItem({ wishlist: { includeProducts: true } }) const removeItem = useRemoveItem({ wishlist: { includeProducts: true } })
const [loading, setLoading] = useState(false) const [loading, setLoading] = useState(false)
@ -36,6 +37,7 @@ const WishlistCard: FC<Props> = ({ product }) => {
const handleRemove = async () => { const handleRemove = async () => {
setRemoving(true) setRemoving(true)
try { try {
// If this action succeeds then there's no need to do `setRemoving(true)` // If this action succeeds then there's no need to do `setRemoving(true)`
// because the component will be removed from the view // because the component will be removed from the view

View File

@ -5,7 +5,7 @@ import { GetCartHook } from '../types/cart'
import { GetCartQueryVariables, QueryRoot } from '../schema' import { GetCartQueryVariables, QueryRoot } from '../schema'
import { normalizeCart, getCartQuery, setCheckoutUrlCookie } from '../utils' import { normalizeCart, getCartQuery, setCheckoutUrlCookie } from '../utils'
import Cookies from 'js-cookie' 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<typeof handler> export default useCommerceCart as UseCart<typeof handler>

View File

@ -21,11 +21,14 @@ import { colorMap } from '@lib/colors'
import { CommerceError } from '@commerce/utils/errors' import { CommerceError } from '@commerce/utils/errors'
import type { Wishlist } from '@commerce/types/wishlist' import type { Wishlist } from '@commerce/types/wishlist'
const money = ({ amount, currencyCode }: MoneyV2) => { const money = (money?: MoneyV2) => {
if (money) {
return { return {
value: +amount, value: +money.amount,
currencyCode, currencyCode: money.currencyCode,
} }
}
return null
} }
const normalizeProductOption = ({ const normalizeProductOption = ({
@ -206,9 +209,13 @@ export function normalizeWishlist(
], ],
id, id,
variants: variant?.id ? [{ id: variant?.id }] : [], variants: variant?.id ? [{ id: variant?.id }] : [],
amount: +variant?.priceV2?.amount, price: {
baseAmount: +variant?.compareAtPriceV2?.amount, value: +variant?.priceV2?.amount,
currencyCode: variant?.priceV2?.currencyCode, currencyCode: variant?.priceV2?.currencyCode,
retailPrice: variant?.compareAtPriceV2
? +variant?.compareAtPriceV2.amount
: null,
},
}, },
})) ?? [], })) ?? [],
} }

View File

@ -6,7 +6,6 @@ import type { AddItemHook } from '../types/wishlist'
import { import {
getWishlistId, getWishlistId,
normalizeCart,
normalizeWishlist, normalizeWishlist,
throwUserErrors, throwUserErrors,
wishlistCreate, wishlistCreate,
@ -34,7 +33,7 @@ export const handler: MutationHook<AddItemHook> = {
if (!wishlistId) { if (!wishlistId) {
const cart = await wishlistCreate(fetch, lines) const cart = await wishlistCreate(fetch, lines)
return normalizeCart(cart) return normalizeWishlist(cart)
} else { } else {
const { cartLinesAdd } = await fetch< const { cartLinesAdd } = await fetch<
CartLinesAddMutation, CartLinesAddMutation,
@ -46,9 +45,7 @@ export const handler: MutationHook<AddItemHook> = {
lines, lines,
}, },
}) })
throwUserErrors(cartLinesAdd?.userErrors) throwUserErrors(cartLinesAdd?.userErrors)
return normalizeWishlist(cartLinesAdd?.cart) return normalizeWishlist(cartLinesAdd?.cart)
} }
}, },

View File

@ -4,6 +4,8 @@ import useWishlist, { UseWishlist } from '@commerce/wishlist/use-wishlist'
import type { GetWishlistHook } from '../types/wishlist' import type { GetWishlistHook } from '../types/wishlist'
import { getWishlistId, normalizeWishlist, getWishlistQuery } from '../utils' import { getWishlistId, normalizeWishlist, getWishlistQuery } from '../utils'
import { GetCartQueryVariables, QueryRoot } from '../schema' import { GetCartQueryVariables, QueryRoot } from '../schema'
import Cookies from 'js-cookie'
import { SHOPIFY_WHISLIST_ID_COOKIE } from '../const'
export default useWishlist as UseWishlist<typeof handler> export default useWishlist as UseWishlist<typeof handler>
@ -13,16 +15,17 @@ export const handler: SWRHook<GetWishlistHook> = {
}, },
async fetcher({ input: _input, options, fetch }) { async fetcher({ input: _input, options, fetch }) {
const wishListId = getWishlistId() const wishListId = getWishlistId()
if (wishListId) { if (wishListId) {
const { cart } = await fetch<QueryRoot, GetCartQueryVariables>({ const { cart: wishlist } = await fetch<QueryRoot, GetCartQueryVariables>({
...options, ...options,
variables: { cartId: wishListId }, variables: { cartId: wishListId },
}) })
if (wishlist) {
return normalizeWishlist(cart) return normalizeWishlist(wishlist)
} else {
Cookies.remove(SHOPIFY_WHISLIST_ID_COOKIE)
}
} }
return null return null
}, },
useHook: useHook: